Skip to content

Commit 6eb4ba9

Browse files
committed
Propagate errors from container construction
1 parent 227cfcd commit 6eb4ba9

File tree

3 files changed

+64
-54
lines changed

3 files changed

+64
-54
lines changed

src/examples.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use std::collections::HashMap;
77
use crate::backends::plonky2::mock_signed::MockSigner;
88
use crate::frontend::{
99
containers::{Dictionary, Set},
10-
MainPodBuilder, Operation, OperationArg, SignedPod, SignedPodBuilder, Statement, Value,
10+
MainPodBuilder, SignedPod, SignedPodBuilder, Statement, Value,
1111
};
12-
use crate::middleware::{CustomPredicateRef, NativeOperation, OperationType};
12+
use crate::middleware::CustomPredicateRef;
1313
use crate::middleware::{Params, PodType, KEY_SIGNER, KEY_TYPE};
1414
use crate::op;
1515

@@ -32,7 +32,7 @@ pub fn zu_kyc_sign_pod_builders(
3232

3333
sanction_list.insert(
3434
"sanctionList",
35-
Value::Set(Set::new(sanctions_values.into())),
35+
Value::Set(Set::new(sanctions_values.into()).unwrap()),
3636
);
3737

3838
(gov_id, pay_stub, sanction_list)
@@ -338,7 +338,7 @@ pub fn great_boy_pod_full_flow() -> Result<MainPodBuilder> {
338338
alice_friend_pods.push(friend.sign(&mut bob_signer).unwrap());
339339
alice_friend_pods.push(friend.sign(&mut charlie_signer).unwrap());
340340

341-
let good_boy_issuers_dict = Value::Dictionary(Dictionary::new(HashMap::new())); // empty
341+
let good_boy_issuers_dict = Value::Dictionary(Dictionary::new(HashMap::new()).unwrap()); // empty
342342
great_boy_pod_builder(
343343
&params,
344344
[
@@ -397,6 +397,6 @@ pub fn tickets_pod_full_flow() -> Result<MainPodBuilder> {
397397
&signed_pod,
398398
123,
399399
true,
400-
&Value::Dictionary(Dictionary::new(HashMap::new())),
400+
&Value::Dictionary(Dictionary::new(HashMap::new()).unwrap()),
401401
)
402402
}

src/frontend/containers.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::collections::HashMap;
22

3+
use anyhow::Result;
34
use schemars::JsonSchema;
45
use serde::{Deserialize, Serialize};
56

@@ -17,10 +18,9 @@ use crate::middleware::{
1718
pub struct Set(Vec<Value>, #[serde(skip)] MiddlewareSet);
1819

1920
impl Set {
20-
pub fn new(values: Vec<Value>) -> Self {
21-
let set =
22-
MiddlewareSet::new(&values.iter().map(|v| MiddlewareValue::from(v)).collect()).unwrap();
23-
Self(values, set)
21+
pub fn new(values: Vec<Value>) -> Result<Self> {
22+
let set = MiddlewareSet::new(&values.iter().map(|v| MiddlewareValue::from(v)).collect())?;
23+
Ok(Self(values, set))
2424
}
2525

2626
pub fn middleware_set(&self) -> &MiddlewareSet {
@@ -38,7 +38,7 @@ impl<'de> Deserialize<'de> for Set {
3838
D: serde::Deserializer<'de>,
3939
{
4040
let values: Vec<Value> = Vec::deserialize(deserializer)?;
41-
Ok(Set::new(values))
41+
Set::new(values).map_err(serde::de::Error::custom)
4242
}
4343
}
4444

@@ -50,15 +50,14 @@ pub struct Dictionary(
5050
);
5151

5252
impl Dictionary {
53-
pub fn new(values: HashMap<String, Value>) -> Self {
53+
pub fn new(values: HashMap<String, Value>) -> Result<Self> {
5454
let dict = MiddlewareDictionary::new(
5555
&values
5656
.iter()
5757
.map(|(k, v)| (hash_str(k), MiddlewareValue::from(v)))
5858
.collect::<HashMap<_, _>>(),
59-
)
60-
.unwrap();
61-
Self(values, dict)
59+
)?;
60+
Ok(Self(values, dict))
6261
}
6362

6463
pub fn middleware_dict(&self) -> &MiddlewareDictionary {
@@ -76,7 +75,7 @@ impl<'de> Deserialize<'de> for Dictionary {
7675
D: serde::Deserializer<'de>,
7776
{
7877
let values: HashMap<String, Value> = HashMap::deserialize(deserializer)?;
79-
Ok(Dictionary::new(values))
78+
Dictionary::new(values).map_err(serde::de::Error::custom)
8079
}
8180
}
8281

@@ -85,11 +84,10 @@ impl<'de> Deserialize<'de> for Dictionary {
8584
pub struct Array(Vec<Value>, #[serde(skip)] MiddlewareArray);
8685

8786
impl Array {
88-
pub fn new(values: Vec<Value>) -> Self {
87+
pub fn new(values: Vec<Value>) -> Result<Self> {
8988
let array =
90-
MiddlewareArray::new(&values.iter().map(|v| MiddlewareValue::from(v)).collect())
91-
.unwrap();
92-
Self(values, array)
89+
MiddlewareArray::new(&values.iter().map(|v| MiddlewareValue::from(v)).collect())?;
90+
Ok(Self(values, array))
9391
}
9492

9593
pub fn middleware_array(&self) -> &MiddlewareArray {
@@ -107,6 +105,6 @@ impl<'de> Deserialize<'de> for Array {
107105
D: serde::Deserializer<'de>,
108106
{
109107
let values: Vec<Value> = Vec::deserialize(deserializer)?;
110-
Ok(Array::new(values))
108+
Array::new(values).map_err(serde::de::Error::custom)
111109
}
112110
}

src/frontend/serialization.rs

+46-34
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl TryFrom<SignedPodHelper> for SignedPod {
3131
return Err(anyhow::anyhow!("pod_type is not Mock"));
3232
}
3333

34-
let dict = Dictionary::new(helper.entries.clone())
34+
let dict = Dictionary::new(helper.entries.clone())?
3535
.middleware_dict()
3636
.clone();
3737
let pod = MockSignedPod::new(PodId(dict.commitment()), helper.proof, dict);
@@ -175,24 +175,29 @@ mod tests {
175175
(Value::Int(42), "{\"Int\":\"42\"}"),
176176
(Value::Bool(true), "true"),
177177
(
178-
Value::Array(Array::new(vec![
179-
Value::String("foo".to_string()),
180-
Value::Bool(false),
181-
])),
178+
Value::Array(
179+
Array::new(vec![Value::String("foo".to_string()), Value::Bool(false)]).unwrap(),
180+
),
182181
"[\"foo\",false]",
183182
),
184183
(
185-
Value::Dictionary(Dictionary::new(HashMap::from([
186-
("foo".to_string(), Value::Int(123)),
187-
("bar".to_string(), Value::String("baz".to_string())),
188-
]))),
184+
Value::Dictionary(
185+
Dictionary::new(HashMap::from([
186+
("foo".to_string(), Value::Int(123)),
187+
("bar".to_string(), Value::String("baz".to_string())),
188+
]))
189+
.unwrap(),
190+
),
189191
"{\"Dictionary\":{\"bar\":\"baz\",\"foo\":{\"Int\":\"123\"}}}",
190192
),
191193
(
192-
Value::Set(Set::new(vec![
193-
Value::String("foo".to_string()),
194-
Value::String("bar".to_string()),
195-
])),
194+
Value::Set(
195+
Set::new(vec![
196+
Value::String("foo".to_string()),
197+
Value::String("bar".to_string()),
198+
])
199+
.unwrap(),
200+
),
196201
"{\"Set\":[\"foo\",\"bar\"]}",
197202
),
198203
];
@@ -216,27 +221,34 @@ mod tests {
216221
builder.insert("very_large_int", 1152921504606846976);
217222
builder.insert(
218223
"a_dict_containing_one_key",
219-
Value::Dictionary(Dictionary::new(HashMap::from([
220-
("foo".to_string(), Value::Int(123)),
221-
(
222-
"an_array_containing_three_ints".to_string(),
223-
Value::Array(Array::new(vec![
224-
Value::Int(1),
225-
Value::Int(2),
226-
Value::Int(3),
227-
])),
228-
),
229-
(
230-
"a_set_containing_two_strings".to_string(),
231-
Value::Set(Set::new(vec![
232-
Value::Array(Array::new(vec![
233-
Value::String("foo".to_string()),
234-
Value::String("bar".to_string()),
235-
])),
236-
Value::String("baz".to_string()),
237-
])),
238-
),
239-
]))),
224+
Value::Dictionary(
225+
Dictionary::new(HashMap::from([
226+
("foo".to_string(), Value::Int(123)),
227+
(
228+
"an_array_containing_three_ints".to_string(),
229+
Value::Array(
230+
Array::new(vec![Value::Int(1), Value::Int(2), Value::Int(3)]).unwrap(),
231+
),
232+
),
233+
(
234+
"a_set_containing_two_strings".to_string(),
235+
Value::Set(
236+
Set::new(vec![
237+
Value::Array(
238+
Array::new(vec![
239+
Value::String("foo".to_string()),
240+
Value::String("bar".to_string()),
241+
])
242+
.unwrap(),
243+
),
244+
Value::String("baz".to_string()),
245+
])
246+
.unwrap(),
247+
),
248+
),
249+
]))
250+
.unwrap(),
251+
),
240252
);
241253

242254
let pod = builder.sign(&mut signer).unwrap();

0 commit comments

Comments
 (0)