|
1 | 1 | use super::{parse_str_to_field, InputValue};
|
2 | 2 | use crate::{errors::InputParserError, Abi, AbiType, MAIN_RETURN_NAME};
|
3 | 3 | use acvm::FieldElement;
|
4 |
| -use iter_extended::{btree_map, try_btree_map, try_vecmap, vecmap}; |
| 4 | +use iter_extended::{try_btree_map, try_vecmap, vecmap}; |
5 | 5 | use serde::{Deserialize, Serialize};
|
6 | 6 | use std::collections::BTreeMap;
|
7 | 7 |
|
@@ -37,12 +37,22 @@ pub(crate) fn parse_json(
|
37 | 37 | }
|
38 | 38 |
|
39 | 39 | pub(crate) fn serialize_to_json(
|
40 |
| - w_map: &BTreeMap<String, InputValue>, |
| 40 | + input_map: &BTreeMap<String, InputValue>, |
| 41 | + abi: &Abi, |
41 | 42 | ) -> Result<String, InputParserError> {
|
42 |
| - let to_map: BTreeMap<_, _> = |
43 |
| - w_map.iter().map(|(key, value)| (key, JsonTypes::from(value.clone()))).collect(); |
| 43 | + let mut json_map = try_btree_map(abi.to_btree_map(), |(key, param_type)| { |
| 44 | + JsonTypes::try_from_input_value(&input_map[&key], ¶m_type) |
| 45 | + .map(|value| (key.to_owned(), value)) |
| 46 | + })?; |
| 47 | + |
| 48 | + if let (Some(return_type), Some(return_value)) = |
| 49 | + (&abi.return_type, input_map.get(MAIN_RETURN_NAME)) |
| 50 | + { |
| 51 | + let return_value = JsonTypes::try_from_input_value(return_value, return_type)?; |
| 52 | + json_map.insert(MAIN_RETURN_NAME.to_owned(), return_value); |
| 53 | + } |
44 | 54 |
|
45 |
| - let json_string = serde_json::to_string(&to_map)?; |
| 55 | + let json_string = serde_json::to_string(&json_map)?; |
46 | 56 |
|
47 | 57 | Ok(json_string)
|
48 | 58 | }
|
@@ -71,24 +81,43 @@ enum JsonTypes {
|
71 | 81 | Table(BTreeMap<String, JsonTypes>),
|
72 | 82 | }
|
73 | 83 |
|
74 |
| -impl From<InputValue> for JsonTypes { |
75 |
| - fn from(value: InputValue) -> Self { |
76 |
| - match value { |
77 |
| - InputValue::Field(f) => { |
| 84 | +impl JsonTypes { |
| 85 | + fn try_from_input_value( |
| 86 | + value: &InputValue, |
| 87 | + abi_type: &AbiType, |
| 88 | + ) -> Result<JsonTypes, InputParserError> { |
| 89 | + let json_value = match (value, abi_type) { |
| 90 | + (InputValue::Field(f), AbiType::Field | AbiType::Integer { .. }) => { |
78 | 91 | let f_str = format!("0x{}", f.to_hex());
|
79 | 92 | JsonTypes::String(f_str)
|
80 | 93 | }
|
81 |
| - InputValue::Vec(v) => { |
82 |
| - let array = v.iter().map(|i| format!("0x{}", i.to_hex())).collect(); |
83 |
| - JsonTypes::ArrayString(array) |
84 |
| - } |
85 |
| - InputValue::String(s) => JsonTypes::String(s), |
86 |
| - InputValue::Struct(map) => { |
87 |
| - let map_with_json_types = |
88 |
| - btree_map(map, |(key, value)| (key, JsonTypes::from(value))); |
| 94 | + (InputValue::Field(f), AbiType::Boolean) => JsonTypes::Bool(f.is_one()), |
| 95 | + |
| 96 | + (InputValue::Vec(v), AbiType::Array { typ, .. }) => match typ.as_ref() { |
| 97 | + AbiType::Field | AbiType::Integer { .. } => { |
| 98 | + let array = v.iter().map(|i| format!("0x{}", i.to_hex())).collect(); |
| 99 | + JsonTypes::ArrayString(array) |
| 100 | + } |
| 101 | + AbiType::Boolean => { |
| 102 | + let array = v.iter().map(|i| i.is_one()).collect(); |
| 103 | + JsonTypes::ArrayBool(array) |
| 104 | + } |
| 105 | + _ => return Err(InputParserError::AbiTypeMismatch(abi_type.clone())), |
| 106 | + }, |
| 107 | + |
| 108 | + (InputValue::String(s), AbiType::String { .. }) => JsonTypes::String(s.to_string()), |
| 109 | + |
| 110 | + (InputValue::Struct(map), AbiType::Struct { fields }) => { |
| 111 | + let map_with_json_types = try_btree_map(fields, |(key, field_type)| { |
| 112 | + JsonTypes::try_from_input_value(&map[key], field_type) |
| 113 | + .map(|json_value| (key.to_owned(), json_value)) |
| 114 | + })?; |
89 | 115 | JsonTypes::Table(map_with_json_types)
|
90 | 116 | }
|
91 |
| - } |
| 117 | + |
| 118 | + _ => return Err(InputParserError::AbiTypeMismatch(abi_type.clone())), |
| 119 | + }; |
| 120 | + Ok(json_value) |
92 | 121 | }
|
93 | 122 | }
|
94 | 123 |
|
|
0 commit comments