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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
use serde::{Deserialize, Serialize};
use std::{
    collections::HashMap,
    fmt::{Debug, Display, Formatter},
};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(
    untagged,
    expecting = "expecting boolean, integer, float, string, list, or map"
)]
pub enum Input {
    Bool(bool),
    Int(isize),
    Float(f64),
    Str(String),
    List(Vec<Input>),
    Map(HashMap<String, Input>),
}

impl Input {
    pub fn new_map() -> Self {
        Self::Map(HashMap::new())
    }

    pub fn new_list() -> Self {
        Self::List(Vec::new())
    }

    pub fn new_str() -> Self {
        Self::Str(String::new())
    }

    pub fn is_bool(&self) -> bool {
        matches!(self, Self::Bool(_))
    }

    pub fn bool_ref(&self) -> Option<&bool> {
        if let Self::Bool(value) = self {
            Some(value)
        } else {
            None
        }
    }

    pub fn into_bool(self) -> Option<bool> {
        if let Self::Bool(value) = self {
            Some(value)
        } else {
            None
        }
    }

    pub fn bool_mut(&mut self) -> Option<&mut bool> {
        if let Self::Bool(value) = self {
            Some(value)
        } else {
            None
        }
    }

    pub fn is_int(&self) -> bool {
        matches!(self, Self::Int(_))
    }

    pub fn int_ref(&self) -> Option<&isize> {
        if let Self::Int(value) = self {
            Some(value)
        } else {
            None
        }
    }

    pub fn into_int(self) -> Option<isize> {
        if let Self::Int(value) = self {
            Some(value)
        } else {
            None
        }
    }

    pub fn int_mut(&mut self) -> Option<&mut isize> {
        if let Self::Int(value) = self {
            Some(value)
        } else {
            None
        }
    }

    pub fn is_float(&self) -> bool {
        matches!(self, Self::Float(_))
    }

    pub fn float_ref(&self) -> Option<&f64> {
        if let Self::Float(value) = self {
            Some(value)
        } else {
            None
        }
    }

    pub fn into_float(self) -> Option<f64> {
        if let Self::Float(value) = self {
            Some(value)
        } else {
            None
        }
    }

    pub fn float_mut(&mut self) -> Option<&mut f64> {
        if let Self::Float(value) = self {
            Some(value)
        } else {
            None
        }
    }

    pub fn is_str(&self) -> bool {
        matches!(self, Self::Str(_))
    }

    pub fn str_ref(&self) -> Option<&String> {
        if let Self::Str(value) = self {
            Some(value)
        } else {
            None
        }
    }

    pub fn into_str(self) -> Option<String> {
        if let Self::Str(value) = self {
            Some(value)
        } else {
            None
        }
    }

    pub fn str_mut(&mut self) -> Option<&mut String> {
        if let Self::Str(value) = self {
            Some(value)
        } else {
            None
        }
    }

    pub fn is_list(&self) -> bool {
        matches!(self, Self::List(_))
    }

    pub fn list_ref(&self) -> Option<&Vec<Input>> {
        if let Self::List(value) = self {
            Some(value)
        } else {
            None
        }
    }

    pub fn into_list(self) -> Option<Vec<Input>> {
        if let Self::List(value) = self {
            Some(value)
        } else {
            None
        }
    }

    pub fn list_mut(&mut self) -> Option<&mut Vec<Input>> {
        if let Self::List(value) = self {
            Some(value)
        } else {
            None
        }
    }

    pub fn is_map(&self) -> bool {
        matches!(self, Self::Map(_))
    }

    pub fn map_ref(&self) -> Option<&HashMap<String, Input>> {
        if let Self::Map(value) = self {
            Some(value)
        } else {
            None
        }
    }

    pub fn into_map(self) -> Option<HashMap<String, Input>> {
        if let Self::Map(value) = self {
            Some(value)
        } else {
            None
        }
    }

    pub fn map_mut(&mut self) -> Option<&mut HashMap<String, Input>> {
        if let Self::Map(value) = self {
            Some(value)
        } else {
            None
        }
    }

    pub fn type_name(&self) -> String {
        match self {
            Self::Bool(_) => Self::bool_type_name(),
            Self::Int(_) => Self::int_type_name(),
            Self::Float(_) => Self::float_type_name(),
            Self::Str(_) => Self::str_type_name(),
            Self::List(_) => Self::list_type_name(),
            Self::Map(_) => Self::map_type_name(),
        }
    }

    pub fn empty_type_name() -> String {
        "empty".to_string()
    }

    pub fn map_type_name() -> String {
        "map".to_string()
    }

    pub fn list_type_name() -> String {
        "list".to_string()
    }

    pub fn str_type_name() -> String {
        "string".to_string()
    }

    pub fn int_type_name() -> String {
        "integer".to_string()
    }

    pub fn float_type_name() -> String {
        "float".to_string()
    }

    pub fn bool_type_name() -> String {
        "boolean".to_string()
    }
}

impl Display for Input {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Bool(value) => write!(f, "{value}"),
            Self::Int(value) => write!(f, "{value}"),
            Self::Float(value) => write!(f, "{value}"),
            Self::Str(value) => write!(f, "{value:?}"),
            Self::List(value) => {
                write!(f, "[")?;
                let length = value.len();
                for (index, inner_value) in value.iter().enumerate() {
                    write!(f, "{inner_value}")?;
                    if index < length - 1 {
                        write!(f, ", ")?;
                    }
                }
                write!(f, "]")
            }
            Self::Map(value) => {
                write!(f, "{{")?;
                let length = value.len();
                for (index, (key, value)) in value.iter().enumerate() {
                    write!(f, "{key:?}: {value}")?;
                    if index < length - 1 {
                        write!(f, ", ")?;
                    }
                }
                write!(f, "}}")
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;

    #[test]
    fn serde() {
        let de_result = serde_json::from_str::<Input>("true");
        assert!(de_result.is_ok());
        assert_eq!(Input::Bool(true), de_result.unwrap());

        let de_result = serde_json::from_str::<Input>("0");
        assert!(de_result.is_ok());
        assert_eq!(Input::Int(0), de_result.unwrap());
        let de_result = serde_json::from_str::<Input>("1234567890");
        assert!(de_result.is_ok());
        assert_eq!(Input::Int(1234567890), de_result.unwrap());
        let de_result = serde_json::from_str::<Input>("-1234567890");
        assert!(de_result.is_ok());
        assert_eq!(Input::Int(-1234567890), de_result.unwrap());

        let de_result = serde_json::from_str::<Input>("0.0");
        assert!(de_result.is_ok());
        assert_eq!(Input::Float(0.0), de_result.unwrap());
        let de_result = serde_json::from_str::<Input>("1234567890.0");
        assert!(de_result.is_ok());
        assert_eq!(Input::Float(1234567890.0), de_result.unwrap());
        let de_result = serde_json::from_str::<Input>("-1234567890.0");
        assert!(de_result.is_ok());
        assert_eq!(Input::Float(-1234567890.0), de_result.unwrap());

        let de_result = serde_json::from_str::<Input>("\"hello\"");
        assert!(de_result.is_ok());
        assert_eq!(Input::Str("hello".to_string()), de_result.unwrap());
        let de_result = serde_json::from_str::<Input>("\"false\"");
        assert!(de_result.is_ok());
        assert_eq!(Input::Str("false".to_string()), de_result.unwrap());
        let de_result = serde_json::from_str::<Input>("\"1\"");
        assert!(de_result.is_ok());
        assert_eq!(Input::Str("1".to_string()), de_result.unwrap());
        let de_result = serde_json::from_str::<Input>("\"3.14\"");
        assert!(de_result.is_ok());
        assert_eq!(Input::Str("3.14".to_string()), de_result.unwrap());

        let de_result = serde_json::from_str::<Input>("[false, 0, 0.0, \"hello\", [[]], {}]");
        assert!(de_result.is_ok());
        let list = de_result.unwrap();
        assert!(list.is_list());
        assert_eq!(
            Input::List(
                [
                    Input::from(false),
                    Input::from(0),
                    Input::from(0.0),
                    Input::from("hello".to_string()),
                    Input::from([Input::List([].to_vec())].to_vec()),
                    Input::from(Input::new_map())
                ]
                .to_vec()
            ),
            list
        );

        let de_result = serde_json::from_str::<Input>(
            "{\"foo\": 0, \"bar\": 0.0, \"baz\": false, \"qux\": {\"hello\": \"world\"}}",
        );
        assert!(de_result.is_ok());
        let map = de_result.unwrap();
        assert!(map.is_map());
        assert_eq!(
            Input::Map(HashMap::from([
                ("foo".to_string(), Input::Int(0)),
                ("bar".to_string(), Input::Float(0.0)),
                ("baz".to_string(), Input::Bool(false)),
                (
                    "qux".to_string(),
                    Input::Map(HashMap::from([(
                        "hello".to_string(),
                        Input::Str("world".to_string())
                    )]))
                ),
            ])),
            map
        );
    }
}