Skip to content

Commit

Permalink
feat: parse SNIP-12 compliant enum
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Jun 10, 2024
1 parent 61eb7a9 commit 005d68c
Showing 1 changed file with 71 additions and 3 deletions.
74 changes: 71 additions & 3 deletions crates/torii/libp2p/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,24 @@ pub fn parse_value_to_ty(value: &PrimitiveType, ty: &mut Ty) -> Result<(), Error

*u256 = Some(U256::from_be_slice(&bytes));

Check warning on line 476 in crates/torii/libp2p/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/libp2p/src/server/mod.rs#L469-L476

Added lines #L469 - L476 were not covered by tests
}
// an enum is a SNIP-12 compliant object with a single key
// where the K is the variant name
// and the value is the variant value
Ty::Enum(enum_) => {
let (option_name, value) = object.first().ok_or_else(|| {
Error::InvalidMessageError("Enum variant not found".to_string())
})?;

Check warning on line 484 in crates/torii/libp2p/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/libp2p/src/server/mod.rs#L481-L484

Added lines #L481 - L484 were not covered by tests

enum_.options.iter_mut().for_each(|option| {
if option.name == *option_name {
parse_value_to_ty(value, &mut option.ty).unwrap();
}
});

enum_.set_option(option_name).map_err(|e| {
Error::InvalidMessageError(format!("Failed to set enum option: {}", e))
})?;

Check warning on line 494 in crates/torii/libp2p/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/libp2p/src/server/mod.rs#L486-L494

Added lines #L486 - L494 were not covered by tests
}
_ => {
return Err(Error::InvalidMessageError("Invalid object type".to_string()));

Check warning on line 497 in crates/torii/libp2p/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/libp2p/src/server/mod.rs#L497

Added line #L497 was not covered by tests
}
Expand Down Expand Up @@ -528,9 +546,6 @@ pub fn parse_value_to_ty(value: &PrimitiveType, ty: &mut Ty) -> Result<(), Error
return Err(Error::InvalidMessageError("Invalid number type".to_string()));

Check warning on line 546 in crates/torii/libp2p/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/libp2p/src/server/mod.rs#L546

Added line #L546 was not covered by tests
}
},
Ty::Enum(enum_) => {
enum_.option = Some(number.as_u64().unwrap() as u8);
}
_ => return Err(Error::InvalidMessageError("Invalid number type".to_string())),

Check warning on line 549 in crates/torii/libp2p/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/libp2p/src/server/mod.rs#L549

Added line #L549 was not covered by tests
},
PrimitiveType::Bool(boolean) => {
Expand Down Expand Up @@ -746,11 +761,13 @@ pub fn parse_ty_to_primitive(ty: &Ty) -> Result<PrimitiveType, Error> {

#[cfg(test)]
mod tests {
use dojo_types::schema::{Member, Struct};
use tempfile::tempdir;

use super::*;

fn test_parse_primitive_to_ty() {
// primitives
let mut ty = Ty::Primitive(Primitive::U8(None));
let value = PrimitiveType::Number(Number::from(1u64));
parse_value_to_ty(&value, &mut ty).unwrap();
Expand Down Expand Up @@ -781,7 +798,58 @@ mod tests {
parse_value_to_ty(&value, &mut ty).unwrap();
assert_eq!(ty, Ty::Primitive(Primitive::U128(Some(1))));

Check warning on line 799 in crates/torii/libp2p/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/libp2p/src/server/mod.rs#L796-L799

Added lines #L796 - L799 were not covered by tests

let mut ty = Ty::Primitive(Primitive::U256(None));
let value = PrimitiveType::String("1".to_string());
parse_value_to_ty(&value, &mut ty).unwrap();
assert_eq!(ty, Ty::Primitive(Primitive::U256(Some(U256::ONE))));

Check warning on line 804 in crates/torii/libp2p/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/libp2p/src/server/mod.rs#L801-L804

Added lines #L801 - L804 were not covered by tests

// test u256 with low high
let mut ty = Ty::Primitive(Primitive::U256(None));
let value = PrimitiveType::Object(
vec![
("low".to_string(), PrimitiveType::Number(Number::from(1u64))),
("high".to_string(), PrimitiveType::Number(Number::from(1u64))),
]
.into_iter()
.collect(),
);
parse_value_to_ty(&value, &mut ty).unwrap();
assert_eq!(ty, Ty::Primitive(Primitive::U256(Some(U256::ONE))));

Check warning on line 817 in crates/torii/libp2p/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/libp2p/src/server/mod.rs#L807-L817

Added lines #L807 - L817 were not covered by tests

let mut ty = Ty::Primitive(Primitive::Felt252(None));
let value = PrimitiveType::String("1".to_string());
parse_value_to_ty(&value, &mut ty).unwrap();
assert_eq!(ty, Ty::Primitive(Primitive::Felt252(Some(FieldElement::ONE))));

Check warning on line 822 in crates/torii/libp2p/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/libp2p/src/server/mod.rs#L819-L822

Added lines #L819 - L822 were not covered by tests

let mut ty = Ty::Primitive(Primitive::ClassHash(None));
let value = PrimitiveType::String("1".to_string());
parse_value_to_ty(&value, &mut ty).unwrap();
assert_eq!(ty, Ty::Primitive(Primitive::ClassHash(Some(FieldElement::ONE))));

Check warning on line 827 in crates/torii/libp2p/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/libp2p/src/server/mod.rs#L824-L827

Added lines #L824 - L827 were not covered by tests

let mut ty = Ty::Primitive(Primitive::ContractAddress(None));
let value = PrimitiveType::String("1".to_string());
parse_value_to_ty(&value, &mut ty).unwrap();
assert_eq!(ty, Ty::Primitive(Primitive::ContractAddress(Some(FieldElement::ONE))));

Check warning on line 832 in crates/torii/libp2p/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/libp2p/src/server/mod.rs#L829-L832

Added lines #L829 - L832 were not covered by tests

let mut ty = Ty::Primitive(Primitive::Bool(None));
let value = PrimitiveType::Bool(true);
parse_value_to_ty(&value, &mut ty).unwrap();
assert_eq!(ty, Ty::Primitive(Primitive::Bool(Some(true))));

Check warning on line 837 in crates/torii/libp2p/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/libp2p/src/server/mod.rs#L834-L837

Added lines #L834 - L837 were not covered by tests

// bytearray
let mut ty = Ty::ByteArray("".to_string());
let value = PrimitiveType::String("mimi".to_string());
parse_value_to_ty(&value, &mut ty).unwrap();
assert_eq!(ty, Ty::ByteArray("mimi".to_string()));
}

Check warning on line 844 in crates/torii/libp2p/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/libp2p/src/server/mod.rs#L840-L844

Added lines #L840 - L844 were not covered by tests

fn test_parse_complex_to_ty() {
let mut ty = Ty::Struct(Struct {
name: "PlayerConfig".to_string(),
children: vec![

],
});
}

Check warning on line 853 in crates/torii/libp2p/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/libp2p/src/server/mod.rs#L846-L853

Added lines #L846 - L853 were not covered by tests

#[tokio::test]
Expand Down

0 comments on commit 005d68c

Please sign in to comment.