Skip to content

Commit

Permalink
feat: improve cst manipulation implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Oct 19, 2024
1 parent cd79488 commit 2453dd7
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 53 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ let json_text = r#"{
}"#;

let root = CstRootNode::parse(json_text, &ParseOptions::default()).unwrap();
let root_obj = root.root_value().unwrap().as_object().unwrap();
let root_obj = root.object_value_or_create().unwrap();

root_obj.get("data").unwrap().set_value(json!({
"nested": true
Expand Down
108 changes: 91 additions & 17 deletions src/cst/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,85 @@ impl CstInputValue {
}
}

#[macro_export]
macro_rules! json {
(null) => {
$crate::cst::CstInputValue::Null
};
impl From<bool> for CstInputValue {
fn from(b: bool) -> Self {
CstInputValue::Bool(b)
}
}

(true) => {
$crate::cst::CstInputValue::Bool(true)
};
impl From<&str> for CstInputValue {
fn from(s: &str) -> Self {
CstInputValue::String(s.to_string())
}
}

(false) => {
$crate::cst::CstInputValue::Bool(false)
};
impl From<String> for CstInputValue {
fn from(s: String) -> Self {
CstInputValue::String(s)
}
}

($num:literal) => {
$crate::cst::CstInputValue::Number($num.to_string())
};
impl From<f64> for CstInputValue {
fn from(n: f64) -> Self {
CstInputValue::Number(n.to_string())
}
}

impl From<usize> for CstInputValue {
fn from(n: usize) -> Self {
CstInputValue::Number(n.to_string())
}
}

impl From<isize> for CstInputValue {
fn from(n: isize) -> Self {
CstInputValue::Number(n.to_string())
}
}

impl From<u64> for CstInputValue {
fn from(n: u64) -> Self {
CstInputValue::Number(n.to_string())
}
}

($str:literal) => {
$crate::cst::CstInputValue::String($str.to_string())
impl From<i64> for CstInputValue {
fn from(n: i64) -> Self {
CstInputValue::Number(n.to_string())
}
}

impl From<u32> for CstInputValue {
fn from(n: u32) -> Self {
CstInputValue::Number(n.to_string())
}
}

impl From<i32> for CstInputValue {
fn from(n: i32) -> Self {
CstInputValue::Number(n.to_string())
}
}

impl<T> From<Vec<T>> for CstInputValue
where
T: Into<CstInputValue>,
{
fn from(vec: Vec<T>) -> Self {
CstInputValue::Array(vec.into_iter().map(Into::into).collect())
}
}

impl From<Vec<(String, CstInputValue)>> for CstInputValue {
fn from(obj: Vec<(String, CstInputValue)>) -> Self {
CstInputValue::Object(obj)
}
}

#[macro_export]
macro_rules! json {
(null) => {
$crate::cst::CstInputValue::Null
};

([ $($elems:tt),* $(,)? ]) => {
Expand All @@ -56,7 +115,22 @@ macro_rules! json {

({ $($key:tt : $value:tt),* $(,)? }) => {
$crate::cst::CstInputValue::Object(vec![
$(($key.to_string(), json!($value))),*
$(
($crate::json!(private_quote_property $key).to_string(), json!($value))
),*
])
};

($other:expr) => {
$crate::cst::CstInputValue::from($other)
};

// hack to not have another public macro for quoting object key properties
(private_quote_property $key:ident) => {
stringify!($key)
};

(private_quote_property $key:expr) => {
$key
};
}
Loading

0 comments on commit 2453dd7

Please sign in to comment.