oneline_template/function_executor/
value.rs

1use serde::{Serialize, Serializer};
2use crate::function_executor::function_error::FunctionError;
3
4/// The value used for templating.
5/// 
6/// ## Automatic convertation into string
7/// 
8/// * `string` -> `string`
9/// * `char`   -> `string`
10/// * `uint`   -> `string` using decimal base.
11/// * `int`    -> `string` using decimal base with sign.
12/// * `bytes`  -> utf-8 encoded string or error
13/// * `bool`   -> error. For convertation boolean type use `bool:to_string`
14/// * `option` -> error. For convertation option use `string:unwrap_or`, `uint:unwrap_or` or others.
15/// * `float`  -> error. For convertation option use `float:to_string`.
16#[derive(Debug)]
17pub enum Value {
18    /// Value that contains string type.
19    String(String),
20    /// Value that contains boolean type.
21    Bool(bool),
22    /// Value that contains array of bytes.
23    Bytes(Vec<u8>),
24    /// Value that contains char.
25    Char(char),
26    /// Value that may contain other value.
27    Option(Option<Box<Value>>),
28    /// Value that contains unsigned 128-bit integer.
29    UInt(u128),
30    /// Value that contains signed 128-bit integer.
31    Int(i128),
32    /// Value that contains float value.
33    Float(f64),
34}
35
36impl Value {
37    /// Trying to cast value as string.
38    pub fn as_string(&self) -> Result<&String, FunctionError> {
39        if let Value::String(ref value) = self {
40            return Ok(value);
41        }
42        return Err(FunctionError::msg(format!("Trying to cast {:?} as string", self)));
43    }
44
45    /// Trying to cast value as mutable string.
46    pub fn as_mut_string(&mut self) -> Result<&mut String, FunctionError> {
47        if let Value::String(ref mut value) = self {
48            return Ok(value);
49        }
50        return Err(FunctionError::msg(format!("Trying to cast {:?} as string", self)));
51    }
52
53    /// Trying to cast value as string.
54    pub fn into_string(self) -> Result<String, FunctionError> {
55        if let Value::String(value) = self {
56            return Ok(value);
57        }
58        return Err(FunctionError::msg(format!("Trying to cast {:?} as string", self)));
59    }
60
61    /// Trying to cast value as bool.
62    pub fn as_bool(&self) -> Result<&bool, FunctionError> {
63        if let Value::Bool(ref value) = self {
64            return Ok(value);
65        }
66        return Err(FunctionError::msg(format!("Trying to cast {:?} as bool", self)));
67    }
68
69    /// Trying to cast value as mutable bool.
70    pub fn as_mut_bool(&mut self) -> Result<&mut bool, FunctionError> {
71        if let Value::Bool(ref mut value) = self {
72            return Ok(value);
73        }
74        return Err(FunctionError::msg(format!("Trying to cast {:?} as bool", self)));
75    }
76
77    /// Trying to cast value as bool.
78    pub fn into_bool(self) -> Result<bool, FunctionError> {
79        if let Value::Bool(value) = self {
80            return Ok(value);
81        }
82        return Err(FunctionError::msg(format!("Trying to cast {:?} as bool", self)));
83    }
84
85    /// Trying to cast value as bytes.
86    pub fn as_bytes(&self) -> Result<&Vec<u8>, FunctionError> {
87        if let Value::Bytes(ref value) = self {
88            return Ok(value);
89        }
90        return Err(FunctionError::msg(format!("Trying to cast {:?} as bytes", self)));
91    }
92
93    /// Trying to cast value as mutable bytes.
94    pub fn as_mut_bytes(&mut self) -> Result<&mut Vec<u8>, FunctionError> {
95        if let Value::Bytes(ref mut value) = self {
96            return Ok(value);
97        }
98        return Err(FunctionError::msg(format!("Trying to cast {:?} as bytes", self)));
99    }
100
101    /// Trying to cast value as bytes.
102    pub fn into_bytes(self) -> Result<Vec<u8>, FunctionError> {
103        if let Value::Bytes(value) = self {
104            return Ok(value);
105        }
106        return Err(FunctionError::msg(format!("Trying to cast {:?} as bytes", self)));
107    }
108
109    /// Trying to cast value as char.
110    pub fn as_char(&self) -> Result<&char, FunctionError> {
111        if let Value::Char(ref value) = self {
112            return Ok(value);
113        }
114        return Err(FunctionError::msg(format!("Trying to cast {:?} as char", self)));
115    }
116
117    /// Trying to cast value as mutable char.
118    pub fn as_mut_char(&mut self) -> Result<&mut char, FunctionError> {
119        if let Value::Char(ref mut value) = self {
120            return Ok(value);
121        }
122        return Err(FunctionError::msg(format!("Trying to cast {:?} as char", self)));
123    }
124
125    /// Trying to cast value as option.
126    pub fn as_option(&self) -> Result<Option<&Value>, FunctionError> {
127        if let Value::Option(ref value) = self {
128            let value = value.as_ref();
129            let value = value.map(Box::as_ref);
130            return Ok(value);
131        }
132        return Err(FunctionError::msg(format!("Trying to cast {:?} as option", self)));
133    }
134
135    /// Trying to cast value as mutable option.
136    pub fn as_mut_option(&mut self) -> Result<Option<&mut Value>, FunctionError> {
137        if let Value::Option(ref mut value) = self {
138            let value = value.as_mut();
139            let value = value.map(Box::as_mut);
140            return Ok(value);
141        }
142        return Err(FunctionError::msg(format!("Trying to cast {:?} as option", self)));
143    }
144
145    /// Trying to cast value as option.
146    pub fn into_option(self) -> Result<Option<Value>, FunctionError> {
147        if let Value::Option(value) = self {
148            let value = value.map(|value| {
149                return *value;
150            });
151            return Ok(value);
152        }
153        return Err(FunctionError::msg(format!("Trying to cast {:?} as option", self)));
154    }
155
156    /// Trying to cast value as uint.
157    pub fn as_uint(&self) -> Result<&u128, FunctionError> {
158        if let Value::UInt(ref value) = self {
159            return Ok(value);
160        }
161        return Err(FunctionError::msg(format!("Trying to cast {:?} as uint", self)));
162    }
163
164    /// Trying to cast value as mutable uint.
165    pub fn as_mut_uint(&mut self) -> Result<&mut u128, FunctionError> {
166        if let Value::UInt(ref mut value) = self {
167            return Ok(value);
168        }
169        return Err(FunctionError::msg(format!("Trying to cast {:?} as uint", self)));
170    }
171
172    /// Trying to cast value as uint.
173    pub fn into_uint(self) -> Result<u128, FunctionError> {
174        if let Value::UInt(value) = self {
175            return Ok(value);
176        }
177        return Err(FunctionError::msg(format!("Trying to cast {:?} as uint", self)));
178    }
179
180    /// Trying to cast value as int.
181    pub fn as_int(&self) -> Result<&i128, FunctionError> {
182        if let Value::Int(ref value) = self {
183            return Ok(value);
184        }
185        return Err(FunctionError::msg(format!("Trying to cast {:?} as int", self)));
186    }
187
188    /// Trying to cast value as mutable int.
189    pub fn as_mut_int(&mut self) -> Result<&mut i128, FunctionError> {
190        if let Value::Int(ref mut value) = self {
191            return Ok(value);
192        }
193        return Err(FunctionError::msg(format!("Trying to cast {:?} as int", self)));
194    }
195
196    /// Trying to cast value as int.
197    pub fn into_int(self) -> Result<i128, FunctionError> {
198        if let Value::Int(value) = self {
199            return Ok(value);
200        }
201        return Err(FunctionError::msg(format!("Trying to cast {:?} as int", self)));
202    }
203
204    /// Trying to cast value as float.
205    pub fn as_float(&self) -> Result<&f64, FunctionError> {
206        if let Value::Float(ref value) = self {
207            return Ok(value);
208        }
209        return Err(FunctionError::msg(format!("Trying to cast {:?} as float", self)));
210    }
211
212    /// Trying to cast value as mutable float.
213    pub fn as_mut_float(&mut self) -> Result<&mut f64, FunctionError> {
214        if let Value::Float(ref mut value) = self {
215            return Ok(value);
216        }
217        return Err(FunctionError::msg(format!("Trying to cast {:?} as float", self)));
218    }
219
220    /// Trying to cast value as float.
221    pub fn floato_float(self) -> Result<f64, FunctionError> {
222        if let Value::Float(value) = self {
223            return Ok(value);
224        }
225        return Err(FunctionError::msg(format!("Trying to cast {:?} as float", self)));
226    }
227}
228
229impl Serialize for Value {
230    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
231        where
232            S: Serializer 
233    {
234        match self {
235            Value::String(ref value) => {
236                return value.serialize(serializer);
237            },
238            Value::Bool(ref value) => {
239                return value.serialize(serializer);
240            },
241            Value::Bytes(ref value) => {
242                return value.serialize(serializer);
243            },
244            Value::Char(ref value) => {
245                return value.serialize(serializer);
246            },
247            Value::Option(ref value) => {
248                return value.serialize(serializer);
249            },
250            Value::UInt(ref value) => {
251                return value.serialize(serializer);
252            },
253            Value::Int(ref value) => {
254                return value.serialize(serializer);
255            },
256            Value::Float(ref value) => {
257                return value.serialize(serializer);
258            },
259        }
260    }
261}