Skip to content

Commit

Permalink
Make felt_from_number report errors properly (lambdaclass#1012)
Browse files Browse the repository at this point in the history
instead of returning Ok(None) in case of an error

Co-authored-by: Mario Rugiero <mario.rugiero@lambdaclass.com>
Co-authored-by: fmoletta <99273364+fmoletta@users.noreply.github.com>
  • Loading branch information
3 people authored and kariy committed Jun 23, 2023
1 parent 5175661 commit 3cb24c8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#### Upcoming Changes

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

* fix: Fix felt sqrt and Signed impl [#1150](https://github.com/lambdaclass/cairo-rs/pull/1150)

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 @@ -169,7 +169,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 @@ -1386,4 +1389,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 3cb24c8

Please sign in to comment.