Skip to content

Commit

Permalink
Support args recursive (Cairo#5230)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciektr committed Mar 11, 2024
1 parent 60f297b commit 4b62f6e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
25 changes: 6 additions & 19 deletions extensions/scarb-cairo-run/src/deserialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl FromStr for Args {
}

impl Args {
fn visit_seq_helper(seq: &[Value]) -> Result<Self, ArgsError> {
fn visit_seq_helper(seq: &[Value]) -> Result<Vec<Arg>, ArgsError> {
let iterator = seq.iter();
let mut args = Vec::new();

Expand All @@ -97,28 +97,13 @@ impl Args {
args.push(Arg::Value(Felt252::from_bytes_be(&n.to_bytes_be())));
}
Value::Array(arr) => {
let mut inner_args = Vec::new();
for a in arr {
match a {
Value::Number(n) => {
let n = n.as_u64().ok_or(ArgsError::NumberOutOfRange)?;
inner_args.push(Arg::Value(Felt252::from(n)));
}
Value::String(n) => {
let n = num_bigint::BigUint::from_str(n)?;
inner_args
.push(Arg::Value(Felt252::from_bytes_be(&n.to_bytes_be())));
}
_ => (),
}
}
args.push(Arg::Array(inner_args));
args.push(Arg::Array(Self::visit_seq_helper(arr)?));
}
_ => (),
}
}

Ok(Self::new(args))
Ok(args)
}
}

Expand All @@ -141,7 +126,9 @@ impl<'de> Visitor<'de> for Args {
}
}

Self::visit_seq_helper(&args).map_err(|e| serde::de::Error::custom(e.to_string()))
Self::visit_seq_helper(&args)
.map(Self::new)
.map_err(|e| serde::de::Error::custom(e.to_string()))
}
}

Expand Down
29 changes: 29 additions & 0 deletions extensions/scarb-cairo-run/tests/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,32 @@ fn invalid_struct_deserialization() {
Function expects arguments of size 3 and received 2 instead.
"#});
}

#[test]
fn can_accept_nested_array() {
let t = TempDir::new().unwrap();
ProjectBuilder::start()
.name("hello")
.version("0.1.0")
.lib_cairo(indoc! {r#"
fn main(a: felt252, b: felt252, n: Array<Array<felt252>>) {
println!("[{}, {}, {:?}]", a, b, n);
}
"#})
.build(&t);

Scarb::quick_snapbox()
.arg("cairo-run")
.arg("--")
.arg(r#"[0, 1, [[17], [1, 15, 3], [42]]]"#)
.current_dir(&t)
.assert()
.success()
.stdout_matches(indoc! {r#"
[..] Compiling hello v0.1.0 ([..]Scarb.toml)
[..] Finished release target(s) in [..]
[..] Running hello
[..][0, 1, [[17], [1, 15, 3], [42]]]
[..]Run completed successfully, returning []
"#});
}

0 comments on commit 4b62f6e

Please sign in to comment.