From 23dc9bf30a18dbeca1e2363c8591a4dbf44075a1 Mon Sep 17 00:00:00 2001 From: bit-aloo Date: Tue, 14 Jan 2025 13:39:35 +0530 Subject: [PATCH] add optional argument in struct --- .../examples/derive_macro_example.rs | 10 +- utils/sv2_serde_json/src/value.rs | 118 ++++++++++++++++++ 2 files changed, 126 insertions(+), 2 deletions(-) diff --git a/utils/sv2_serde_json/examples/derive_macro_example.rs b/utils/sv2_serde_json/examples/derive_macro_example.rs index 80e1a2b9a..917170d83 100644 --- a/utils/sv2_serde_json/examples/derive_macro_example.rs +++ b/utils/sv2_serde_json/examples/derive_macro_example.rs @@ -1,4 +1,4 @@ -use sv2_serde_json::value::{Number, Value}; +use sv2_serde_json::value::Value; use sv2_serde_json_macros::{DeJson, SerJson}; #[derive(SerJson, DeJson, Debug, Clone)] @@ -7,6 +7,9 @@ struct Person { age: i64, is_student: bool, here: Vec, + check: sv2_serde_json::value::Value, + optional_check: Option, + non_optional_check: Option } #[derive(SerJson, DeJson, Debug)] @@ -28,6 +31,9 @@ fn main() { age: 25, is_student: true, here: vec!["HEre".to_string()], + check: sv2_serde_json::value::Value::Null, + optional_check: Some(sv2_serde_json::value::Value::Null), + non_optional_check: Some("Hello".to_string()) }; let person2 = Person2 { @@ -50,6 +56,6 @@ fn main() { let status_json = status.to_json_value(); println!("Serialized Enum: {:?}", status_json); - let deserialized_status = Status::from_json_value(status_json).unwrap(); + let deserialized_status = Status::from_json_value(status_json); println!("Deserialized Enum: {:?}", deserialized_status); } diff --git a/utils/sv2_serde_json/src/value.rs b/utils/sv2_serde_json/src/value.rs index 03f353c3f..447dc93ed 100644 --- a/utils/sv2_serde_json/src/value.rs +++ b/utils/sv2_serde_json/src/value.rs @@ -14,6 +14,8 @@ pub enum Value { Array(Vec), Object(HashMap), Null, + None, // Represents Option::None + Some(Box), } impl Value { @@ -35,6 +37,13 @@ impl Value { format!("{{{}}}", members.join(",")) } Value::Null => "null".to_string(), + Value::None => { + // Still dicy on this + "\n".to_string() + }, + Value::Some(x) => { + x.to_json_string() + } } } @@ -155,6 +164,12 @@ impl From for Value { } } +impl From for Value { + fn from(value: u64) -> Self { + Value::Number(Number::I64(value as i64)) + } +} + impl From for Value { fn from(value: f64) -> Self { Value::Number(Number::F64(value)) @@ -222,3 +237,106 @@ impl<'a> TryFrom<&'a Value> for Vec { } } } + + +impl TryFrom<&Value> for Value { + type Error = (); + + fn try_from(value: &Value) -> Result { + Ok(value.clone()) + } +} + + +impl TryFrom<&Value> for u64 { + type Error = (); + + fn try_from(value: &Value) -> Result { + match value { + Value::Number(value) => { + match value { + Number::I64(x) => Ok(*x as u64), + Number::F64(x) => Ok(*x as u64) + } + } + _ => Err(()), + } + } +} + + +impl From> for Value +where + T: Into, +{ + fn from(opt: Option) -> Self { + match opt { + Some(value) => Value::Some(Box::new(value.into())), + None => Value::None, + } + } +} + +impl TryFrom for Option +{ + type Error = (); + + fn try_from(value: Value) -> Result { + match value { + Value::None => Ok(None), + Value::Some(boxed_value) => Ok(Some((*boxed_value).try_into().map_err(|_| ())?)), + _ => Err(()), // Not a valid Option representation + } + } +} + +impl TryFrom for String { + type Error = (); + + fn try_from(value: Value) -> Result { + dbg!(&value); + match value { + Value::String(x) => Ok(x), + _ => Err(()) + } + } +} + +impl TryFrom<&Value> for Option { + type Error = (); + + fn try_from(value: &Value) -> Result { + match value { + Value::Some(x) => { + match x.as_ref() { + Value::String(x) => {Ok(Some(x.to_string()))}, + _ => Err(()) + } + }, + Value::None => { + Ok(None) + } + _ => { + Err(()) + } + } + } +} + +impl TryFrom<&Value> for Option { + type Error = (); + + fn try_from(value: &Value) -> Result { + match value { + Value::Some(x) => { + Ok(Some(x.as_ref().clone())) + }, + Value::None => { + Ok(None) + } + _ => { + Err(()) + } + } + } +} \ No newline at end of file