Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Nov 28, 2024
1 parent 5b8c77c commit bc56e52
Showing 1 changed file with 34 additions and 31 deletions.
65 changes: 34 additions & 31 deletions crates/dojo/types/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use std::{any::type_name, str::FromStr};
use std::any::type_name;
use std::str::FromStr;

use cainome::cairo_serde::{ByteArray, CairoSerde};
use crypto_bigint::{Encoding, U256};
use indexmap::IndexMap;
use itertools::Itertools;
use num_traits::ToPrimitive;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value as JsonValue};
use starknet::core::types::Felt;
use strum_macros::AsRefStr;
use serde_json::{Value as JsonValue, json};
use indexmap::IndexMap;

use crate::primitive::{Primitive, PrimitiveError};

Expand Down Expand Up @@ -348,21 +349,21 @@ impl Ty {
obj.insert(member.name.clone(), member.ty.to_json_value()?);
}
Ok(json!(obj))
},
}
Ty::Enum(e) => {
let option = e.option().map_err(|_| PrimitiveError::MissingFieldElement)?;
Ok(json!({
option.name.clone(): option.ty.to_json_value()?
}))
},
}
Ty::Array(items) => {
let values: Result<Vec<_>, _> = items.iter().map(|ty| ty.to_json_value()).collect();
Ok(json!(values?))
},
}
Ty::Tuple(items) => {
let values: Result<Vec<_>, _> = items.iter().map(|ty| ty.to_json_value()).collect();
Ok(json!(values?))
},
}
Ty::ByteArray(bytes) => Ok(json!(bytes.clone())),
}
}
Expand All @@ -375,106 +376,108 @@ impl Ty {
if let JsonValue::Bool(b) = value {
*v = Some(b);
}
},
}
Primitive::I8(v) => {
if let JsonValue::Number(n) = value {
*v = n.as_i64().map(|n| n as i8);
}
},
}
Primitive::I16(v) => {
if let JsonValue::Number(n) = value {
*v = n.as_i64().map(|n| n as i16);
}
},
}
Primitive::I32(v) => {
if let JsonValue::Number(n) = value {
*v = n.as_i64().map(|n| n as i32);
}
},
}
Primitive::I64(v) => {
if let JsonValue::String(s) = value {
*v = s.parse().ok();
}
},
}
Primitive::I128(v) => {
if let JsonValue::String(s) = value {
*v = s.parse().ok();
}
},
}
Primitive::U8(v) => {
if let JsonValue::Number(n) = value {
*v = n.as_u64().map(|n| n as u8);
}
},
}
Primitive::U16(v) => {
if let JsonValue::Number(n) = value {
*v = n.as_u64().map(|n| n as u16);
}
},
}
Primitive::U32(v) => {
if let JsonValue::Number(n) = value {
*v = n.as_u64().map(|n| n as u32);
}
},
}
Primitive::U64(v) => {
if let JsonValue::String(s) = value {
*v = s.parse().ok();
}
},
}
Primitive::U128(v) => {
if let JsonValue::String(s) = value {
*v = s.parse().ok();
}
},
}
Primitive::USize(v) => {
if let JsonValue::Number(n) = value {
*v = n.as_u64().map(|n| n as u32);
}
},
}
Primitive::U256(v) => {
if let JsonValue::Object(obj) = value {
if let (Some(JsonValue::String(high)), Some(JsonValue::String(low))) =
(obj.get("high"), obj.get("low")) {
if let (Ok(high), Ok(low)) = (high.parse::<u128>(), low.parse::<u128>()) {
if let (Some(JsonValue::String(high)), Some(JsonValue::String(low))) =
(obj.get("high"), obj.get("low"))
{
if let (Ok(high), Ok(low)) = (high.parse::<u128>(), low.parse::<u128>())
{
let mut bytes = [0u8; 32];
bytes[..16].copy_from_slice(&high.to_be_bytes());
bytes[16..].copy_from_slice(&low.to_be_bytes());
*v = Some(U256::from_be_slice(&bytes));
}
}
}
},
}
Primitive::Felt252(v) => {
if let JsonValue::String(s) = value {
*v = Felt::from_str(&s).ok();
}
},
}
Primitive::ClassHash(v) => {
if let JsonValue::String(s) = value {
*v = Felt::from_str(&s).ok();
}
},
}
Primitive::ContractAddress(v) => {
if let JsonValue::String(s) = value {
*v = Felt::from_str(&s).ok();
}
},
}
},
(Ty::Struct(s), JsonValue::Object(obj)) => {
for member in &mut s.children {
if let Some(value) = obj.get(&member.name) {
member.ty.from_json_value(value.clone())?;
}
}
},
}
(Ty::Enum(e), JsonValue::Object(obj)) => {
if let Some((name, value)) = obj.into_iter().next() {
e.set_option(&name).map_err(|_| PrimitiveError::TypeMismatch)?;
if let Some(option) = e.option {
e.options[option as usize].ty.from_json_value(value)?;
}
}
},
}
(Ty::Array(items), JsonValue::Array(values)) => {
let template = items[0].clone();
items.clear();
Expand All @@ -483,18 +486,18 @@ impl Ty {
item.from_json_value(value)?;
items.push(item);
}
},
}
(Ty::Tuple(items), JsonValue::Array(values)) => {
if items.len() != values.len() {
return Err(PrimitiveError::TypeMismatch);
}
for (item, value) in items.iter_mut().zip(values) {
item.from_json_value(value)?;
}
},
}
(Ty::ByteArray(bytes), JsonValue::String(s)) => {
*bytes = s;
},
}
_ => return Err(PrimitiveError::TypeMismatch),
}
Ok(())
Expand Down

0 comments on commit bc56e52

Please sign in to comment.