Skip to content

Commit 24cafde

Browse files
authored
Assorted tweaks to support external playground crate (#322)
* Assorted tweaks to support external playground crate * Fix schemas * Fixed schema again * Add ToHex for RawValue * Add FromHex to RawValue
1 parent 335100d commit 24cafde

File tree

10 files changed

+111
-46
lines changed

10 files changed

+111
-46
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@ backend_plonky2 = ["plonky2"]
4848
zk = []
4949
metrics = []
5050
time = []
51+
examples = []

src/frontend/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{collections::HashMap, convert::From, fmt};
55

66
use itertools::Itertools;
77
use serde::{Deserialize, Serialize};
8-
use serialization::{SerializedMainPod, SerializedSignedPod};
8+
pub use serialization::{SerializedMainPod, SerializedSignedPod};
99

1010
use crate::middleware::{
1111
self, check_st_tmpl, hash_op, hash_str, max_op, prod_op, sum_op, AnchoredKey, Key,

src/frontend/serialization.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub enum SignedPodType {
1717
MockSigned,
1818
}
1919

20-
#[derive(Serialize, Deserialize, JsonSchema)]
20+
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq)]
2121
#[serde(rename_all = "camelCase")]
2222
#[schemars(rename = "SignedPod")]
2323
pub struct SerializedSignedPod {
@@ -27,7 +27,7 @@ pub struct SerializedSignedPod {
2727
data: serde_json::Value,
2828
}
2929

30-
#[derive(Serialize, Deserialize, JsonSchema)]
30+
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq)]
3131
#[serde(rename_all = "camelCase")]
3232
#[schemars(rename = "MainPod")]
3333
pub struct SerializedMainPod {
@@ -154,11 +154,11 @@ mod tests {
154154
]))
155155
.unwrap(),
156156
),
157-
"{\"Dictionary\":{\"max_depth\":32,\"kvs\":{\"\":\"baz\",\"\\u0000\":\"\",\" hi\":false,\"!@£$%^&&*()\":\"\",\"foo\":{\"Int\":\"123\"},\"🥳\":\"party time!\"}}}",
157+
"{\"max_depth\":32,\"kvs\":{\"\":\"baz\",\"\\u0000\":\"\",\" hi\":false,\"!@£$%^&&*()\":\"\",\"foo\":{\"Int\":\"123\"},\"🥳\":\"party time!\"}}",
158158
),
159159
(
160160
TypedValue::Set(Set::new(params.max_depth_mt_containers, HashSet::from(["foo".into(), "bar".into()])).unwrap()),
161-
"{\"Set\":{\"max_depth\":32,\"set\":[\"bar\",\"foo\"]}}",
161+
"{\"max_depth\":32,\"set\":[\"bar\",\"foo\"]}",
162162
),
163163
];
164164

src/lang/processor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,9 @@ fn validate_and_build_statement_template(
336336
| NativePredicate::Lt
337337
| NativePredicate::LtEq
338338
| NativePredicate::SetContains
339-
| NativePredicate::NotContains
340339
| NativePredicate::DictNotContains
341-
| NativePredicate::SetNotContains => 2,
340+
| NativePredicate::SetNotContains
341+
| NativePredicate::NotContains => 2,
342342
NativePredicate::Contains
343343
| NativePredicate::ArrayContains
344344
| NativePredicate::DictContains

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub mod frontend;
77
pub mod lang;
88
pub mod middleware;
99

10-
#[cfg(test)]
10+
#[cfg(any(test, feature = "examples"))]
1111
pub mod examples;
1212

1313
#[cfg(feature = "time")]

src/middleware/basetypes.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,40 @@ impl fmt::Display for RawValue {
125125
}
126126
}
127127

128+
impl ToHex for RawValue {
129+
fn encode_hex<T: std::iter::FromIterator<char>>(&self) -> T {
130+
self.0
131+
.iter()
132+
.rev()
133+
.fold(String::with_capacity(64), |mut s, limb| {
134+
write!(s, "{:016x}", limb.0).unwrap();
135+
s
136+
})
137+
.chars()
138+
.collect()
139+
}
140+
141+
fn encode_hex_upper<T: std::iter::FromIterator<char>>(&self) -> T {
142+
self.0
143+
.iter()
144+
.rev()
145+
.fold(String::with_capacity(64), |mut s, limb| {
146+
write!(s, "{:016X}", limb.0).unwrap();
147+
s
148+
})
149+
.chars()
150+
.collect()
151+
}
152+
}
153+
154+
impl FromHex for RawValue {
155+
type Error = FromHexError;
156+
157+
fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
158+
Hash::from_hex(hex).map(|h| RawValue(h.0))
159+
}
160+
}
161+
128162
#[derive(Clone, Copy, Debug, Default, Hash, Eq, PartialEq, Serialize, Deserialize, JsonSchema)]
129163
pub struct Hash(
130164
#[serde(

src/middleware/custom.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::middleware::{
99
EMPTY_HASH, F, VALUE_SIZE,
1010
};
1111

12-
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
12+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
1313
pub struct Wildcard {
1414
pub name: String,
1515
pub index: usize,
@@ -37,7 +37,7 @@ impl ToFields for Wildcard {
3737
}
3838
}
3939

40-
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
40+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
4141
#[serde(tag = "type", content = "value")]
4242
pub enum StatementTmplArg {
4343
None,
@@ -122,7 +122,7 @@ impl fmt::Display for StatementTmplArg {
122122
}
123123

124124
/// Statement Template for a Custom Predicate
125-
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
125+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
126126
pub struct StatementTmpl {
127127
pub pred: Predicate,
128128
pub args: Vec<StatementTmplArg>,
@@ -179,7 +179,7 @@ impl ToFields for StatementTmpl {
179179
}
180180
}
181181

182-
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
182+
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
183183
#[serde(rename_all = "camelCase")]
184184
/// NOTE: fields are not public (outside of crate) to enforce the struct instantiation through
185185
/// the `::and/or` methods, which performs checks on the values.
@@ -275,6 +275,21 @@ impl CustomPredicate {
275275
args: vec![],
276276
}
277277
}
278+
pub fn is_conjunction(&self) -> bool {
279+
self.conjunction
280+
}
281+
pub fn is_disjunction(&self) -> bool {
282+
!self.conjunction
283+
}
284+
pub fn statements(&self) -> &[StatementTmpl] {
285+
&self.statements
286+
}
287+
pub fn args_len(&self) -> usize {
288+
self.args_len
289+
}
290+
pub fn wildcard_names(&self) -> &[String] {
291+
&self.wildcard_names
292+
}
278293
}
279294

280295
impl ToFields for CustomPredicate {
@@ -341,13 +356,19 @@ impl fmt::Display for CustomPredicate {
341356
}
342357
}
343358

344-
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
359+
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
345360
pub struct CustomPredicateBatch {
346361
id: Hash,
347362
pub name: String,
348363
pub(crate) predicates: Vec<CustomPredicate>,
349364
}
350365

366+
impl std::hash::Hash for CustomPredicateBatch {
367+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
368+
self.id.hash(state);
369+
}
370+
}
371+
351372
impl ToFields for CustomPredicateBatch {
352373
fn to_fields(&self, params: &Params) -> Vec<F> {
353374
// all the custom predicates in order
@@ -401,7 +422,7 @@ impl CustomPredicateBatch {
401422
}
402423
}
403424

404-
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
425+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
405426
pub struct CustomPredicateRef {
406427
pub batch: Arc<CustomPredicateBatch>,
407428
pub index: usize,

src/middleware/mod.rs

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ pub enum TypedValue {
5353
// 53-bit precision for integers, integers are represented as tagged
5454
// strings, with a custom serializer and deserializer.
5555
// TAGGED TYPES:
56-
Set(Set),
57-
Dictionary(Dictionary),
5856
Int(
5957
#[serde(serialize_with = "serialize_i64", deserialize_with = "deserialize_i64")]
6058
// #[schemars(with = "String", regex(pattern = r"^\d+$"))]
@@ -67,6 +65,10 @@ pub enum TypedValue {
6765
PodId(PodId),
6866
// UNTAGGED TYPES:
6967
#[serde(untagged)]
68+
Set(Set),
69+
#[serde(untagged)]
70+
Dictionary(Dictionary),
71+
#[serde(untagged)]
7072
Array(Array),
7173
#[serde(untagged)]
7274
String(String),
@@ -170,6 +172,20 @@ impl TryFrom<TypedValue> for Key {
170172
}
171173
}
172174

175+
impl TryFrom<&TypedValue> for PodId {
176+
type Error = Error;
177+
fn try_from(v: &TypedValue) -> Result<Self> {
178+
match v {
179+
TypedValue::PodId(id) => Ok(*id),
180+
TypedValue::Raw(v) => Ok(PodId(Hash(v.0))),
181+
_ => Err(Error::custom(format!(
182+
"Value {} cannot be converted to a PodId.",
183+
v
184+
))),
185+
}
186+
}
187+
}
188+
173189
impl fmt::Display for TypedValue {
174190
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
175191
match self {
@@ -216,30 +232,6 @@ impl JsonSchema for TypedValue {
216232
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
217233
use schemars::schema::{InstanceType, Schema, SchemaObject, SingleOrVec};
218234

219-
let set_schema = schemars::schema::SchemaObject {
220-
instance_type: Some(SingleOrVec::Single(Box::new(InstanceType::Object))),
221-
object: Some(Box::new(schemars::schema::ObjectValidation {
222-
properties: [("Set".to_string(), gen.subschema_for::<Set>())]
223-
.into_iter()
224-
.collect(),
225-
required: ["Set".to_string()].into_iter().collect(),
226-
..Default::default()
227-
})),
228-
..Default::default()
229-
};
230-
231-
let dictionary_schema = schemars::schema::SchemaObject {
232-
instance_type: Some(SingleOrVec::Single(Box::new(InstanceType::Object))),
233-
object: Some(Box::new(schemars::schema::ObjectValidation {
234-
properties: [("Dictionary".to_string(), gen.subschema_for::<Dictionary>())]
235-
.into_iter()
236-
.collect(),
237-
required: ["Dictionary".to_string()].into_iter().collect(),
238-
..Default::default()
239-
})),
240-
..Default::default()
241-
};
242-
243235
// Int is serialized/deserialized as a tagged string
244236
let int_schema = schemars::schema::SchemaObject {
245237
instance_type: Some(SingleOrVec::Single(Box::new(InstanceType::Object))),
@@ -275,20 +267,36 @@ impl JsonSchema for TypedValue {
275267
..Default::default()
276268
};
277269

270+
let public_key_schema = schemars::schema::SchemaObject {
271+
instance_type: Some(SingleOrVec::Single(Box::new(InstanceType::Object))),
272+
object: Some(Box::new(schemars::schema::ObjectValidation {
273+
// PublicKey is serialized as a string
274+
properties: [("PublicKey".to_string(), gen.subschema_for::<String>())]
275+
.into_iter()
276+
.collect(),
277+
required: ["PublicKey".to_string()].into_iter().collect(),
278+
..Default::default()
279+
})),
280+
..Default::default()
281+
};
282+
278283
// This is the part that Schemars can't generate automatically:
279284
let untagged_array_schema = gen.subschema_for::<Array>();
285+
let untagged_set_schema = gen.subschema_for::<Set>();
286+
let untagged_dictionary_schema = gen.subschema_for::<Dictionary>();
280287
let untagged_string_schema = gen.subschema_for::<String>();
281288
let untagged_bool_schema = gen.subschema_for::<bool>();
282289

283290
Schema::Object(SchemaObject {
284291
subschemas: Some(Box::new(schemars::schema::SubschemaValidation {
285292
any_of: Some(vec![
286-
Schema::Object(set_schema),
287-
Schema::Object(dictionary_schema),
288293
Schema::Object(int_schema),
289294
Schema::Object(raw_schema),
295+
Schema::Object(public_key_schema),
290296
untagged_array_schema,
297+
untagged_dictionary_schema,
291298
untagged_string_schema,
299+
untagged_set_schema,
292300
untagged_bool_schema,
293301
]),
294302
..Default::default()

src/middleware/operation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl ToFields for OperationType {
5656
}
5757
}
5858

59-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
59+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
6060
pub enum NativeOperation {
6161
None = 0,
6262
NewEntry = 1,

src/middleware/statement.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl ToFields for NativePredicate {
5050
}
5151
}
5252

53-
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
53+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
5454
#[serde(tag = "type", content = "value")]
5555
pub enum Predicate {
5656
Native(NativePredicate),
@@ -129,7 +129,7 @@ impl fmt::Display for Predicate {
129129
}
130130

131131
/// Type encapsulating statements with their associated arguments.
132-
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
132+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
133133
#[serde(tag = "predicate", content = "args")]
134134
pub enum Statement {
135135
None,
@@ -368,7 +368,8 @@ impl ToFields for StatementArg {
368368
}
369369
}
370370

371-
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
371+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
372+
#[serde(tag = "type", content = "value")]
372373
pub enum ValueRef {
373374
Literal(Value),
374375
Key(AnchoredKey),

0 commit comments

Comments
 (0)