jsonc_parser/cst/
input.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/// API user provided value for inserts and replaces.
#[derive(Debug, Clone)]
pub enum CstInputValue {
  Null,
  Bool(bool),
  Number(String),
  String(String),
  Array(Vec<CstInputValue>),
  Object(Vec<(String, CstInputValue)>),
}

impl CstInputValue {
  pub(crate) fn force_multiline(&self) -> bool {
    match self {
      CstInputValue::Null | CstInputValue::Bool(_) | CstInputValue::Number(_) | CstInputValue::String(_) => false,
      CstInputValue::Array(v) => v.iter().any(|v| v.is_object_or_array_with_elements()),
      CstInputValue::Object(v) => !v.is_empty(),
    }
  }

  fn is_object_or_array_with_elements(&self) -> bool {
    match self {
      CstInputValue::Null | CstInputValue::Bool(_) | CstInputValue::Number(_) | CstInputValue::String(_) => false,
      CstInputValue::Array(v) => !v.is_empty(),
      CstInputValue::Object(v) => !v.is_empty(),
    }
  }
}

impl From<bool> for CstInputValue {
  fn from(b: bool) -> Self {
    CstInputValue::Bool(b)
  }
}

impl From<&str> for CstInputValue {
  fn from(s: &str) -> Self {
    CstInputValue::String(s.to_string())
  }
}

impl From<String> for CstInputValue {
  fn from(s: String) -> Self {
    CstInputValue::String(s)
  }
}

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())
  }
}

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),* $(,)? ]) => {
    $crate::cst::CstInputValue::Array(vec![
      $(json!($elems)),*
    ])
  };

  ({ $($key:tt : $value:tt),* $(,)? }) => {
    $crate::cst::CstInputValue::Object(vec![
      $(
         ($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
  };
}