Skip to content

Commit

Permalink
chore: improve functions to convert to InputValue
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Jun 13, 2023
1 parent fd7c241 commit 68207a4
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 84 deletions.
79 changes: 37 additions & 42 deletions crates/noirc_abi/src/input_parser/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,52 +99,47 @@ impl InputValue {
param_type: &AbiType,
arg_name: &str,
) -> Result<InputValue, InputParserError> {
let input_value = match value {
JsonTypes::String(string) => match param_type {
AbiType::String { .. } => InputValue::String(string),
AbiType::Field | AbiType::Integer { .. } | AbiType::Boolean => {
InputValue::Field(parse_str_to_field(&string)?)
}

AbiType::Array { .. } | AbiType::Struct { .. } => {
return Err(InputParserError::AbiTypeMismatch(param_type.clone()))
}
},
JsonTypes::Integer(integer) => {
let input_value = match (value, param_type) {
(JsonTypes::String(string), AbiType::String { .. }) => InputValue::String(string),
(
JsonTypes::String(string),
AbiType::Field | AbiType::Integer { .. } | AbiType::Boolean,
) => InputValue::Field(parse_str_to_field(&string)?),

(JsonTypes::Integer(integer), AbiType::Field | AbiType::Integer { .. }) => {
let new_value = FieldElement::from(i128::from(integer));

InputValue::Field(new_value)
}
JsonTypes::Bool(boolean) => InputValue::Field(boolean.into()),
JsonTypes::Array(array) => match param_type {
AbiType::Array { typ, .. }
if matches!(
typ.as_ref(),
AbiType::Field | AbiType::Integer { .. } | AbiType::Boolean
) =>
{
let array_elements =
try_vecmap(array, |value| InputValue::try_from_json(value, typ, arg_name))?;
InputValue::Vec(array_elements)
}
_ => return Err(InputParserError::AbiTypeMismatch(param_type.clone())),
},
JsonTypes::Table(table) => match param_type {
AbiType::Struct { fields } => {
let native_table = try_btree_map(fields, |(field_name, abi_type)| {
// Check that json contains a value for each field of the struct.
let field_id = format!("{arg_name}.{field_name}");
let value = table
.get(field_name)
.ok_or_else(|| InputParserError::MissingArgument(field_id.clone()))?;
InputValue::try_from_json(value.clone(), abi_type, &field_id)
.map(|input_value| (field_name.to_string(), input_value))
})?;

InputValue::Struct(native_table)
}
_ => return Err(InputParserError::AbiTypeMismatch(param_type.clone())),
},

(JsonTypes::Bool(boolean), AbiType::Boolean) => InputValue::Field(boolean.into()),

(JsonTypes::Array(array), AbiType::Array { typ, .. })
if matches!(
typ.as_ref(),
AbiType::Field | AbiType::Integer { .. } | AbiType::Boolean
) =>
{
let array_elements =
try_vecmap(array, |value| InputValue::try_from_json(value, typ, arg_name))?;
InputValue::Vec(array_elements)
}

(JsonTypes::Table(table), AbiType::Struct { fields }) => {
let native_table = try_btree_map(fields, |(field_name, abi_type)| {
// Check that json contains a value for each field of the struct.
let field_id = format!("{arg_name}.{field_name}");
let value = table
.get(field_name)
.ok_or_else(|| InputParserError::MissingArgument(field_id.clone()))?;
InputValue::try_from_json(value.clone(), abi_type, &field_id)
.map(|input_value| (field_name.to_string(), input_value))
})?;

InputValue::Struct(native_table)
}

(_, _) => return Err(InputParserError::AbiTypeMismatch(param_type.clone())),
};

Ok(input_value)
Expand Down
79 changes: 37 additions & 42 deletions crates/noirc_abi/src/input_parser/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,52 +96,47 @@ impl InputValue {
param_type: &AbiType,
arg_name: &str,
) -> Result<InputValue, InputParserError> {
let input_value = match value {
TomlTypes::String(string) => match param_type {
AbiType::String { .. } => InputValue::String(string),
AbiType::Field | AbiType::Integer { .. } | AbiType::Boolean => {
InputValue::Field(parse_str_to_field(&string)?)
}

AbiType::Array { .. } | AbiType::Struct { .. } => {
return Err(InputParserError::AbiTypeMismatch(param_type.clone()))
}
},
TomlTypes::Integer(integer) => {
let input_value = match (value, param_type) {
(TomlTypes::String(string), AbiType::String { .. }) => InputValue::String(string),
(
TomlTypes::String(string),
AbiType::Field | AbiType::Integer { .. } | AbiType::Boolean,
) => InputValue::Field(parse_str_to_field(&string)?),

(TomlTypes::Integer(integer), AbiType::Field | AbiType::Integer { .. }) => {
let new_value = FieldElement::from(i128::from(integer));

InputValue::Field(new_value)
}
TomlTypes::Bool(boolean) => InputValue::Field(boolean.into()),
TomlTypes::Array(array) => match param_type {
AbiType::Array { typ, .. }
if matches!(
typ.as_ref(),
AbiType::Field | AbiType::Integer { .. } | AbiType::Boolean
) =>
{
let array_elements =
try_vecmap(array, |value| InputValue::try_from_toml(value, typ, arg_name))?;
InputValue::Vec(array_elements)
}
_ => return Err(InputParserError::AbiTypeMismatch(param_type.clone())),
},
TomlTypes::Table(table) => match param_type {
AbiType::Struct { fields } => {
let native_table = try_btree_map(fields, |(field_name, abi_type)| {
// Check that toml contains a value for each field of the struct.
let field_id = format!("{arg_name}.{field_name}");
let value = table
.get(field_name)
.ok_or_else(|| InputParserError::MissingArgument(field_id.clone()))?;
InputValue::try_from_toml(value.clone(), abi_type, &field_id)
.map(|input_value| (field_name.to_string(), input_value))
})?;

InputValue::Struct(native_table)
}
_ => return Err(InputParserError::AbiTypeMismatch(param_type.clone())),
},

(TomlTypes::Bool(boolean), AbiType::Boolean) => InputValue::Field(boolean.into()),

(TomlTypes::Array(array), AbiType::Array { typ, .. })
if matches!(
typ.as_ref(),
AbiType::Field | AbiType::Integer { .. } | AbiType::Boolean
) =>
{
let array_elements =
try_vecmap(array, |value| InputValue::try_from_toml(value, typ, arg_name))?;
InputValue::Vec(array_elements)
}

(TomlTypes::Table(table), AbiType::Struct { fields }) => {
let native_table = try_btree_map(fields, |(field_name, abi_type)| {
// Check that json contains a value for each field of the struct.
let field_id = format!("{arg_name}.{field_name}");
let value = table
.get(field_name)
.ok_or_else(|| InputParserError::MissingArgument(field_id.clone()))?;
InputValue::try_from_toml(value.clone(), abi_type, &field_id)
.map(|input_value| (field_name.to_string(), input_value))
})?;

InputValue::Struct(native_table)
}

(_, _) => return Err(InputParserError::AbiTypeMismatch(param_type.clone())),
};

Ok(input_value)
Expand Down

0 comments on commit 68207a4

Please sign in to comment.