Skip to content

Commit

Permalink
Fixed panic on VariantNotFound (#1487)
Browse files Browse the repository at this point in the history
  • Loading branch information
fishseabowl authored Jan 30, 2024
1 parent ce58ff2 commit f57b78c
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion crates/dojo-types/src/packing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ pub fn parse_ty(data: &[FieldElement]) -> Result<Ty, ParseError> {

fn parse_simple(data: &[FieldElement]) -> Result<Ty, ParseError> {
let ty = parse_cairo_short_string(&data[0])?;
Ok(Ty::Primitive(Primitive::from_str(&ty).expect("must be valid schema")))
let primitive = match Primitive::from_str(&ty) {
Ok(primitive) => primitive,
Err(_) => return Err(ParseError::InvalidSchema),
};

Ok(Ty::Primitive(primitive))
}

fn parse_struct(data: &[FieldElement]) -> Result<Ty, ParseError> {
Expand Down Expand Up @@ -191,3 +196,16 @@ fn parse_tuple(data: &[FieldElement]) -> Result<Ty, ParseError> {

Ok(Ty::Tuple(children))
}

#[cfg(test)]
mod tests {
use starknet::core::types::FieldElement;

use crate::packing::ParseError;

#[test]
fn parse_simple_with_invalid_value() {
let data = &vec![FieldElement::default()];
assert!(matches!(super::parse_simple(data), Err(ParseError::InvalidSchema)));
}
}

0 comments on commit f57b78c

Please sign in to comment.