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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
pub mod de;
pub mod from;
pub mod index;
pub mod into;
pub mod macros;
pub mod number;
pub mod ser;

#[cfg(not(feature = "preserve_order"))]
pub type MapImpl<K, V> = std::collections::HashMap<K, V>;
#[cfg(feature = "preserve_order")]
pub type MapImpl<K, V> = indexmap::IndexMap<K, V>;

/// Represents any valid JSON with comments value.
///
/// # Examples
/// see [`crate`] document also.
/// ```
/// use json_with_comments::{jsonc_generics, value::JsoncValue};
///
/// let mut value: JsoncValue<u32, f32> = jsonc_generics!({
///     "name": "json-with-comments",
///     "keywords": [
///         "JSON with comments",
///         "JSONC",
///         "trailing comma",
///     ],
/// });
///
/// // index access
/// assert_eq!(value["name"], JsoncValue::String("json-with-comments".to_string()));
/// assert_eq!(
///     value["keywords"].get(..=1),
///     Some(
///         &[JsoncValue::String("JSON with comments".to_string()), JsoncValue::String("JSONC".to_string())][..]
///     )
/// );
///
/// // mutable access
/// value["name"] = "json_with_comments".into();
/// if let Some(JsoncValue::String(jsonc)) = value["keywords"].get_mut(1) {
///     *jsonc = jsonc.to_lowercase();
/// }
/// assert_eq!(value, jsonc_generics!({
///     "name": "json_with_comments",
///     "keywords": [
///         "JSON with comments",
///         "jsonc",
///         "trailing comma",
///     ],
/// }));
///
/// // as rust value
/// let v = value["keywords"].as_vec().unwrap();
/// let mut iter = v.iter();
/// assert_eq!(iter.next().unwrap().as_str().unwrap(), "JSON with comments");
/// assert_eq!(iter.next().unwrap().as_str().unwrap(), "jsonc");
/// assert_eq!(iter.next().unwrap().as_str().unwrap(), "trailing comma");
/// assert_eq!(iter.next(), None);
/// ```
#[derive(Debug, Clone, PartialEq)]
// if JsoncValue<'a, I, F>, cannot implement FromStr
pub enum JsoncValue<I, F> {
    /// Represents any valid JSON with comments object.
    /// Default implementation is `HashMap`. If `preserve_order` feature is enabled, that will be `IndexMap`.
    /// ```
    /// let v = json_with_comments::jsonc!({"key": "value"});
    /// ```
    Object(MapImpl<String, JsoncValue<I, F>>),

    /// Represents any valid JSON with comments array.
    /// ```
    /// let v = json_with_comments::jsonc!([1, 2, 3]);
    /// ```
    Array(Vec<JsoncValue<I, F>>),

    /// Represents any valid JSON with comments boolean.
    /// ```
    /// let v = json_with_comments::jsonc!(true);
    /// ```
    Bool(bool),

    /// Represents any valid JSON with comments null.
    /// ```
    /// let v = json_with_comments::jsonc!(null);
    /// ```
    Null,

    /// Represents any valid JSON with comments string.
    /// ```
    /// let v = json_with_comments::jsonc!("string");
    /// ```
    String(String),

    /// Represents any valid JSON with comments number, whether integer or float.
    /// ```
    /// let v = json_with_comments::jsonc!(123.45);
    /// ```
    Number(number::Number<I, F>),
}

impl<I, F> Default for JsoncValue<I, F> {
    fn default() -> Self {
        Self::Null
    }
}
impl<I: num::FromPrimitive, F: num::FromPrimitive> std::str::FromStr for JsoncValue<I, F> {
    type Err = crate::Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        crate::from_str(s)
    }
}
impl<I, F> JsoncValue<I, F> {
    /// TODO doc
    pub fn query(&self, query: &str) -> Option<&JsoncValue<I, F>> {
        // TODO better implement, tests
        query.split('.').try_fold(self, |value, key| match value {
            JsoncValue::Object(map) => map.get(key),
            JsoncValue::Array(vec) => vec.get(key.parse::<usize>().ok()?),
            _ => None,
        })
    }

    /// Replaces value with the default value `Null`, returning the previous value.
    /// - If you want to replace the values of two variables, see [`Self::swap`].
    /// - If you want to replace with a passed value instead of the default value, see [`Self::replace`].
    ///
    /// # Examples
    /// ```
    /// use json_with_comments::jsonc;
    /// let mut value = jsonc!({
    ///     "name": "json-with-comments",
    ///     "keywords": [
    ///         "JSON with comments",
    ///         "JSONC",
    ///         "trailing comma",
    ///     ],
    /// });
    ///
    /// let name = value["name"].take();
    /// assert_eq!(name, "json-with-comments".into());
    /// assert_eq!(value, jsonc!({
    ///     "name": null,
    ///     "keywords": [
    ///         "JSON with comments",
    ///         "JSONC",
    ///         "trailing comma"
    ///     ]
    /// }));
    /// ```
    pub fn take(&mut self) -> Self {
        std::mem::take(self)
    }

    /// Swaps `self` value and `other` value.
    /// - If you want to swap with a default or dummy value, see [`Self::take`].
    /// - If you want to replace with a passed value instead of the default value, see [`Self::replace`].
    ///
    /// # Examples
    /// ```
    /// use json_with_comments::jsonc;
    /// let mut value = jsonc!({
    ///     "name": "json-with-comments",
    ///     "keywords": [
    ///         "JSON with comments",
    ///         "JSONC",
    ///         "trailing comma",
    ///     ],
    /// });
    ///
    /// let mut lower = "jsonc".into();
    /// let name = value["keywords"][1].swap(&mut lower);
    /// assert_eq!(lower, "JSONC".into());
    /// assert_eq!(value, jsonc!({
    ///     "name": "json-with-comments",
    ///     "keywords": [
    ///         "JSON with comments",
    ///         "jsonc",
    ///         "trailing comma"
    ///     ]
    /// }));
    /// ```
    pub fn swap(&mut self, other: &mut Self) {
        std::mem::swap(self, other)
    }

    /// Moves `other` value into `self` value, returning the previous `self` value.
    /// - If you want to swap with a default or dummy value, see [`Self::take`].
    /// - If you want to replace the values of two variables, see [`Self::swap`].
    ///
    /// # Examples
    /// ```
    /// use json_with_comments::jsonc;
    /// let mut value = jsonc!({
    ///     "name": "json-with-comments",
    ///     "keywords": [
    ///         "JSON with comments",
    ///         "JSONC",
    ///         "trailing comma",
    ///     ],
    /// });
    ///
    /// let upper = "JSON WITH COMMENTS".into();
    /// let original = value["keywords"][0].replace(upper);
    /// assert_eq!(original, "JSON with comments".into());
    /// assert_eq!(value, jsonc!({
    ///     "name": "json-with-comments",
    ///     "keywords": [
    ///         "JSON WITH COMMENTS",
    ///         "JSONC",
    ///         "trailing comma"
    ///     ]
    /// }));
    /// ```
    pub fn replace(&mut self, other: Self) -> Self {
        std::mem::replace(self, other)
    }

    /// Get the value type representation of [`JsoncValue`].
    /// Main use case is for error reporting.
    ///
    /// # Examples
    /// ```
    /// use json_with_comments::jsonc;
    /// assert_eq!(jsonc!({"key": "value"}).value_type(), "Object");
    /// assert_eq!(jsonc!([1, 2, 3]).value_type(), "Array");
    /// assert_eq!(jsonc!(true).value_type(), "Boolean");
    /// assert_eq!(jsonc!(null).value_type(), "Null");
    /// assert_eq!(jsonc!("string").value_type(), "String");
    /// assert_eq!(jsonc!(123).value_type(), "Number");
    /// assert_eq!(jsonc!(123.45).value_type(), "Number");
    /// ```
    pub fn value_type(&self) -> String {
        match self {
            JsoncValue::Object(_) => "Object",
            JsoncValue::Array(_) => "Array",
            JsoncValue::Bool(_) => "Boolean",
            JsoncValue::Null => "Null",
            JsoncValue::String(_) => "String",
            JsoncValue::Number(_) => "Number",
        }
        .to_string()
    }
}