Skip to content

Commit a83da21

Browse files
committed
Convert frontend CustomPredicateRef to a named field struct
1 parent fac4ee7 commit a83da21

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

src/examples/custom.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub fn eth_friend_batch(params: &Params) -> Result<Arc<CustomPredicateBatch>> {
4545

4646
/// Instantiates an ETHDoS batch
4747
pub fn eth_dos_batch(params: &Params) -> Result<Arc<CustomPredicateBatch>> {
48-
let eth_friend = Predicate::Custom(CustomPredicateRef(eth_friend_batch(params)?, 0));
48+
let eth_friend = Predicate::Custom(CustomPredicateRef::new(eth_friend_batch(params)?, 0));
4949
let mut builder = CustomPredicateBatchBuilder::new("eth_dos_distance_base".into());
5050

5151
// eth_dos_distance_base(src_or, src_key, dst_or, dst_key, distance_or, distance_key) = and<

src/examples/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ pub fn eth_dos_pod_builder(
8383
bob_pubkey: &Value,
8484
) -> Result<MainPodBuilder> {
8585
// Will need ETH friend and ETH DoS custom predicate batches.
86-
let eth_friend = CustomPredicateRef(eth_friend_batch(params)?, 0);
86+
let eth_friend = CustomPredicateRef::new(eth_friend_batch(params)?, 0);
8787
let eth_dos_batch = eth_dos_batch(params)?;
88-
let eth_dos_base = CustomPredicateRef(eth_dos_batch.clone(), 0);
89-
let eth_dos_ind = CustomPredicateRef(eth_dos_batch.clone(), 1);
90-
let eth_dos = CustomPredicateRef(eth_dos_batch.clone(), 2);
88+
let eth_dos_base = CustomPredicateRef::new(eth_dos_batch.clone(), 0);
89+
let eth_dos_ind = CustomPredicateRef::new(eth_dos_batch.clone(), 1);
90+
let eth_dos = CustomPredicateRef::new(eth_dos_batch.clone(), 2);
9191

9292
// ETHDoS POD builder
9393
let mut alice_bob_ethdos = MainPodBuilder::new(params);

src/frontend/custom.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ impl From<Predicate> for middleware::Predicate {
131131
match v {
132132
Predicate::Native(p) => p.into(),
133133
Predicate::BatchSelf(i) => middleware::Predicate::BatchSelf(i),
134-
Predicate::Custom(CustomPredicateRef(pb, i)) => {
134+
Predicate::Custom(CustomPredicateRef {
135+
batch: pb,
136+
index: i,
137+
}) => {
135138
let cpb: middleware::CustomPredicateBatch = Arc::unwrap_or_clone(pb).into();
136139
middleware::Predicate::Custom(middleware::CustomPredicateRef(Arc::new(cpb), i))
137140
}
@@ -144,31 +147,40 @@ impl fmt::Display for Predicate {
144147
match self {
145148
Self::Native(p) => write!(f, "{:?}", p),
146149
Self::BatchSelf(i) => write!(f, "self.{}", i),
147-
Self::Custom(CustomPredicateRef(pb, i)) => write!(f, "{}.{}", pb.name, i),
150+
Self::Custom(CustomPredicateRef { batch, index }) => {
151+
write!(f, "{}.{}", batch.name, index)
152+
}
148153
}
149154
}
150155
}
151156

152157
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
153-
pub struct CustomPredicateRef(pub Arc<CustomPredicateBatch>, pub usize);
158+
pub struct CustomPredicateRef {
159+
pub batch: Arc<CustomPredicateBatch>,
160+
pub index: usize,
161+
}
154162

155163
impl From<CustomPredicateRef> for middleware::CustomPredicateRef {
156164
fn from(v: CustomPredicateRef) -> Self {
157-
let cpb: middleware::CustomPredicateBatch = Arc::unwrap_or_clone(v.0).into();
158-
middleware::CustomPredicateRef(Arc::new(cpb), v.1)
165+
let cpb: middleware::CustomPredicateBatch = Arc::unwrap_or_clone(v.batch).into();
166+
middleware::CustomPredicateRef(Arc::new(cpb), v.index)
159167
}
160168
}
161169

162170
impl CustomPredicateRef {
171+
pub fn new(batch: Arc<CustomPredicateBatch>, index: usize) -> Self {
172+
Self { batch, index }
173+
}
174+
163175
pub fn arg_len(&self) -> usize {
164-
self.0.predicates[self.1].args_len
176+
self.batch.predicates[self.index].args_len
165177
}
166178
pub fn match_against(&self, statements: &[Statement]) -> Result<HashMap<usize, Value>> {
167179
let mut bindings = HashMap::new();
168180
// Single out custom predicate, replacing batch-self
169181
// references with custom predicate references.
170182
let custom_predicate = {
171-
let cp = &Arc::unwrap_or_clone(self.0.clone()).predicates[self.1];
183+
let cp = &Arc::unwrap_or_clone(self.batch.clone()).predicates[self.index];
172184
CustomPredicate {
173185
conjunction: cp.conjunction,
174186
statements: cp
@@ -177,9 +189,9 @@ impl CustomPredicateRef {
177189
.map(|StatementTmpl(p, args)| {
178190
StatementTmpl(
179191
match p {
180-
Predicate::BatchSelf(i) => {
181-
Predicate::Custom(CustomPredicateRef(self.0.clone(), *i))
182-
}
192+
Predicate::BatchSelf(i) => Predicate::Custom(
193+
CustomPredicateRef::new(self.batch.clone(), *i),
194+
),
183195
_ => p.clone(),
184196
},
185197
args.to_vec(),
@@ -526,7 +538,7 @@ mod tests {
526538
let eth_friend = eth_friend_batch(&params)?;
527539

528540
// This batch only has 1 predicate, so we pick it already for convenience
529-
let eth_friend = Predicate::Custom(CustomPredicateRef(eth_friend, 0));
541+
let eth_friend = Predicate::Custom(CustomPredicateRef::new(eth_friend, 0));
530542

531543
let eth_dos_batch = eth_dos_batch(&params)?;
532544
let eth_dos_batch_mw: middleware::CustomPredicateBatch =

0 commit comments

Comments
 (0)