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
use crate::Generator;
use crate::Indentation;

#[derive(Debug)]
pub enum Value {
    Null,
    True,
    False,
    Float(f64),
    Integer(i64),
    String(String),
    Literal(String),
    List(Vec<Value>),
    HashMap(Vec<(Value, Value)>),
}

impl From<bool> for Value {
    fn from(value: bool) -> Self {
        if value {
            Value::True
        } else {
            Value::False
        }
    }
}

impl From<f64> for Value {
    fn from(value: f64) -> Self {
        Value::Float(value)
    }
}

impl From<i64> for Value {
    fn from(value: i64) -> Self {
        Value::Integer(value)
    }
}

impl From<String> for Value {
    fn from(value: String) -> Self {
        Value::String(value)
    }
}

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

impl From<()> for Value {
    fn from(_: ()) -> Self {
        Value::Null
    }
}

impl<T: Into<Value>> From<Vec<T>> for Value {
    fn from(value: Vec<T>) -> Self {
        Value::List(value.into_iter().map(|value| value.into()).collect())
    }
}

impl<T: Into<Value>> From<Vec<(T, T)>> for Value {
    fn from(value: Vec<(T, T)>) -> Self {
        Value::HashMap(
            value
                .into_iter()
                .map(|(key, value)| (key.into(), value.into()))
                .collect(),
        )
    }
}

impl Generator for Value {
    fn generate(&self, _identation: Indentation, _level: usize) -> String {
        match self {
            Value::Null => "null".to_string(),
            Value::True => "true".to_string(),
            Value::False => "false".to_string(),
            Value::Integer(value) => value.to_string(),
            Value::String(value) => format!("\"{}\"", value),
            Value::Float(value) => value.to_string(),
            Value::Literal(value) => value.to_string(),
            Value::List(values) => {
                let mut result = String::new();

                result.push('[');
                result.push_str(
                    &values
                        .iter()
                        .map(|value| value.generate(_identation, _level))
                        .collect::<Vec<String>>()
                        .join(", "),
                );
                result.push(']');

                result
            }
            Value::HashMap(values) => {
                let mut result = String::new();

                result.push('[');
                result.push_str(
                    &values
                        .iter()
                        .map(|(key, value)| {
                            format!(
                                "{} => {}",
                                key.generate(_identation, _level),
                                value.generate(_identation, _level)
                            )
                        })
                        .collect::<Vec<String>>()
                        .join(", "),
                );
                result.push(']');

                result
            }
        }
    }
}