Skip to content

Commit

Permalink
Make felt_from_number report errors properly
Browse files Browse the repository at this point in the history
instead of returning Ok(None) in case of an error
  • Loading branch information
omerfirmak committed Apr 26, 2023
1 parent 327ee7d commit 8782d7e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#### Upcoming Changes

* Fix felt_from_number not properly returning parse errors [#1012](https://github.com/lambdaclass/cairo-rs/pull/1012)

* Add missing hint on vrf.json lib [#1052](https://github.com/lambdaclass/cairo-rs/pull/1052):

`BuiltinHintProcessor` now supports the following hint:
Expand Down
17 changes: 16 additions & 1 deletion src/serde/deserialize_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ where
D: Deserializer<'de>,
{
let n = Number::deserialize(deserializer)?;
Ok(Felt252::parse_bytes(n.to_string().as_bytes(), 10))
match Felt252::parse_bytes(n.to_string().as_bytes(), 10) {
Some(x) => Ok(Some(x)),
None => Err(String::from("felt_from_number parse error")).map_err(de::Error::custom),
}
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
Expand Down Expand Up @@ -1345,4 +1348,16 @@ mod tests {
"()"
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn deserialize_nonbase10_number_errors() {
let valid_json = r#"
{
"value" : 0x123
}"#;

let iden: Result<Identifier, serde_json::Error> = serde_json::from_str(valid_json);
assert!(iden.err().is_some());
}
}

0 comments on commit 8782d7e

Please sign in to comment.