Skip to content

Commit

Permalink
add optional argument in struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Shourya742 committed Jan 14, 2025
1 parent 2683c44 commit 23dc9bf
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 2 deletions.
10 changes: 8 additions & 2 deletions utils/sv2_serde_json/examples/derive_macro_example.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -7,6 +7,9 @@ struct Person {
age: i64,
is_student: bool,
here: Vec<String>,
check: sv2_serde_json::value::Value,
optional_check: Option<sv2_serde_json::value::Value>,
non_optional_check: Option<String>
}

#[derive(SerJson, DeJson, Debug)]
Expand All @@ -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 {
Expand All @@ -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);
}
118 changes: 118 additions & 0 deletions utils/sv2_serde_json/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub enum Value {
Array(Vec<Value>),
Object(HashMap<String, Value>),
Null,
None, // Represents Option::None
Some(Box<Value>),
}

impl Value {
Expand All @@ -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()
}
}
}

Expand Down Expand Up @@ -155,6 +164,12 @@ impl From<i64> for Value {
}
}

impl From<u64> for Value {
fn from(value: u64) -> Self {
Value::Number(Number::I64(value as i64))
}
}

impl From<f64> for Value {
fn from(value: f64) -> Self {
Value::Number(Number::F64(value))
Expand Down Expand Up @@ -222,3 +237,106 @@ impl<'a> TryFrom<&'a Value> for Vec<bool> {
}
}
}


impl TryFrom<&Value> for Value {
type Error = ();

fn try_from(value: &Value) -> Result<Self, Self::Error> {
Ok(value.clone())
}
}


impl TryFrom<&Value> for u64 {
type Error = ();

fn try_from(value: &Value) -> Result<Self, Self::Error> {
match value {
Value::Number(value) => {
match value {
Number::I64(x) => Ok(*x as u64),
Number::F64(x) => Ok(*x as u64)
}
}
_ => Err(()),
}
}
}


impl<T> From<Option<T>> for Value
where
T: Into<Value>,
{
fn from(opt: Option<T>) -> Self {
match opt {
Some(value) => Value::Some(Box::new(value.into())),
None => Value::None,
}
}
}

impl TryFrom<Value> for Option<String>
{
type Error = ();

fn try_from(value: Value) -> Result<Self, Self::Error> {
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<Value> for String {
type Error = ();

fn try_from(value: Value) -> Result<Self, Self::Error> {
dbg!(&value);
match value {
Value::String(x) => Ok(x),
_ => Err(())
}
}
}

impl TryFrom<&Value> for Option<String> {
type Error = ();

fn try_from(value: &Value) -> Result<Self, Self::Error> {
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<Value> {
type Error = ();

fn try_from(value: &Value) -> Result<Self, Self::Error> {
match value {
Value::Some(x) => {
Ok(Some(x.as_ref().clone()))
},
Value::None => {
Ok(None)
}
_ => {
Err(())
}
}
}
}

0 comments on commit 23dc9bf

Please sign in to comment.