php_codegen/
literal.rs

1use crate::Generator;
2use crate::Indentation;
3
4#[derive(Debug)]
5pub enum Value {
6    Null,
7    True,
8    False,
9    Float(f64),
10    Integer(i64),
11    String(String),
12    Literal(String),
13    List(Vec<Value>),
14    HashMap(Vec<(Value, Value)>),
15}
16
17impl From<bool> for Value {
18    fn from(value: bool) -> Self {
19        if value {
20            Value::True
21        } else {
22            Value::False
23        }
24    }
25}
26
27impl From<f64> for Value {
28    fn from(value: f64) -> Self {
29        Value::Float(value)
30    }
31}
32
33impl From<i64> for Value {
34    fn from(value: i64) -> Self {
35        Value::Integer(value)
36    }
37}
38
39impl From<String> for Value {
40    fn from(value: String) -> Self {
41        Value::String(value)
42    }
43}
44
45impl From<&str> for Value {
46    fn from(value: &str) -> Self {
47        Value::String(value.to_string())
48    }
49}
50
51impl From<()> for Value {
52    fn from(_: ()) -> Self {
53        Value::Null
54    }
55}
56
57impl<T: Into<Value>> From<Vec<T>> for Value {
58    fn from(value: Vec<T>) -> Self {
59        Value::List(value.into_iter().map(|value| value.into()).collect())
60    }
61}
62
63impl<T: Into<Value>> From<Vec<(T, T)>> for Value {
64    fn from(value: Vec<(T, T)>) -> Self {
65        Value::HashMap(
66            value
67                .into_iter()
68                .map(|(key, value)| (key.into(), value.into()))
69                .collect(),
70        )
71    }
72}
73
74impl Generator for Value {
75    fn generate(&self, _identation: Indentation, _level: usize) -> String {
76        match self {
77            Value::Null => "null".to_string(),
78            Value::True => "true".to_string(),
79            Value::False => "false".to_string(),
80            Value::Integer(value) => value.to_string(),
81            Value::String(value) => format!("\"{}\"", value),
82            Value::Float(value) => value.to_string(),
83            Value::Literal(value) => value.to_string(),
84            Value::List(values) => {
85                let mut result = String::new();
86
87                result.push('[');
88                result.push_str(
89                    &values
90                        .iter()
91                        .map(|value| value.generate(_identation, _level))
92                        .collect::<Vec<String>>()
93                        .join(", "),
94                );
95                result.push(']');
96
97                result
98            }
99            Value::HashMap(values) => {
100                let mut result = String::new();
101
102                result.push('[');
103                result.push_str(
104                    &values
105                        .iter()
106                        .map(|(key, value)| {
107                            format!(
108                                "{} => {}",
109                                key.generate(_identation, _level),
110                                value.generate(_identation, _level)
111                            )
112                        })
113                        .collect::<Vec<String>>()
114                        .join(", "),
115                );
116                result.push(']');
117
118                result
119            }
120        }
121    }
122}