1use error::*;
2use lisp::ExecutionTreeNode;
3
4#[derive(Clone)]
5pub enum ExecutionTreeObject {
6 Bool(bool),
7 Char(char),
8 F32(f32),
9 F64(f64),
10 I8(i8),
11 I16(i16),
12 I32(i32),
13 I64(i64),
14 ISize(isize),
15 Node(ExecutionTreeNode),
16 Symbol(String),
17 String(String),
18 U8(u8),
19 U16(u16),
20 U32(u32),
21 U64(u64),
22 USize(usize),
23}
24
25impl ExecutionTreeObject {
26 pub fn nil() -> Self {
27 ExecutionTreeObject::Node(ExecutionTreeNode::nil())
28 }
29
30 pub fn bool_str() -> &'static str {
31 "ExecutionTreeObject::Bool"
32 }
33
34 pub fn char_str() -> &'static str {
35 "ExecutionTreeObject::Char"
36 }
37
38 pub fn f32_str() -> &'static str {
39 "ExecutionTreeObject::F32"
40 }
41
42 pub fn f64_str() -> &'static str {
43 "ExecutionTreeObject::F64"
44 }
45
46 pub fn i8_str() -> &'static str {
47 "ExecutionTreeObject::I8"
48 }
49
50 pub fn i16_str() -> &'static str {
51 "ExecutionTreeObject::I16"
52 }
53
54 pub fn i32_str() -> &'static str {
55 "ExecutionTreeObject::I32"
56 }
57
58 pub fn i64_str() -> &'static str {
59 "ExecutionTreeObject::I64"
60 }
61
62 pub fn isize_str() -> &'static str {
63 "ExecutionTreeObject::ISize"
64 }
65
66 pub fn node_str() -> &'static str {
67 "ExecutionTreeObject::Node"
68 }
69
70 pub fn symbol_str() -> &'static str {
71 "ExecutionTreeObject::Symbol"
72 }
73
74 pub fn string_str() -> &'static str {
75 "ExecutionTreeObject::String"
76 }
77
78 pub fn u8_str() -> &'static str {
79 "ExecutionTreeObject::U8"
80 }
81
82 pub fn u16_str() -> &'static str {
83 "ExecutionTreeObject::U16"
84 }
85
86 pub fn u32_str() -> &'static str {
87 "ExecutionTreeObject::U32"
88 }
89
90 pub fn u64_str() -> &'static str {
91 "ExecutionTreeObject::U64"
92 }
93
94 pub fn usize_str() -> &'static str {
95 "ExecutionTreeObject::USize"
96 }
97
98 pub fn enum_to_string(&self) -> &'static str {
99 match self {
100 &ExecutionTreeObject::Bool(_) => Self::bool_str(),
101 &ExecutionTreeObject::Char(_) => Self::char_str(),
102 &ExecutionTreeObject::F32(_) => Self::f32_str(),
103 &ExecutionTreeObject::F64(_) => Self::f64_str(),
104 &ExecutionTreeObject::I8(_) => Self::i8_str(),
105 &ExecutionTreeObject::I16(_) => Self::i16_str(),
106 &ExecutionTreeObject::I32(_) => Self::i32_str(),
107 &ExecutionTreeObject::I64(_) => Self::i64_str(),
108 &ExecutionTreeObject::ISize(_) => Self::isize_str(),
109 &ExecutionTreeObject::Node(_) => Self::node_str(),
110 &ExecutionTreeObject::Symbol(_) => Self::symbol_str(),
111 &ExecutionTreeObject::String(_) => Self::string_str(),
112 &ExecutionTreeObject::U8(_) => Self::u8_str(),
113 &ExecutionTreeObject::U16(_) => Self::u16_str(),
114 &ExecutionTreeObject::U32(_) => Self::u32_str(),
115 &ExecutionTreeObject::U64(_) => Self::u64_str(),
116 &ExecutionTreeObject::USize(_) => Self::usize_str(),
117 }
118 }
119
120 pub fn to_string(&self) -> Result<String> {
121 let result =
122 match self {
123 &ExecutionTreeObject::Bool(ref some) => some.to_string(),
124 &ExecutionTreeObject::Char(ref some) => format!("'{}'", some.to_string()),
125 &ExecutionTreeObject::F32(ref some) => format!("{}f32", some.to_string()),
126 &ExecutionTreeObject::F64(ref some) => format!("{}f64", some.to_string()),
127 &ExecutionTreeObject::I8(ref some) => format!("{}i8", some.to_string()),
128 &ExecutionTreeObject::I16(ref some) => format!("{}i16", some.to_string()),
129 &ExecutionTreeObject::I32(ref some) => format!("{}i32", some.to_string()),
130 &ExecutionTreeObject::I64(ref some) => format!("{}i64", some.to_string()),
131 &ExecutionTreeObject::ISize(ref some) => format!("{}isize", some.to_string()),
132 &ExecutionTreeObject::Node(ref some) => try!(some.to_string()),
133 &ExecutionTreeObject::Symbol(ref some) => some.to_string(),
134 &ExecutionTreeObject::String(ref some) => format!("\"{}\"", some),
135 &ExecutionTreeObject::U8(ref some) => format!("{}u8", some.to_string()),
136 &ExecutionTreeObject::U16(ref some) => format!("{}u16", some.to_string()),
137 &ExecutionTreeObject::U32(ref some) => format!("{}u32", some.to_string()),
138 &ExecutionTreeObject::U64(ref some) => format!("{}u64", some.to_string()),
139 &ExecutionTreeObject::USize(ref some) => format!("{}usize", some.to_string()),
140 };
141 Ok(result)
142 }
143}
144
145impl From<ExecutionTreeObject> for Result<bool> {
146 fn from(object: ExecutionTreeObject) -> Result<bool> {
147 match object {
148 ExecutionTreeObject::Bool(result) => Ok(result),
149 object => {
150 Err(ErrorKind::InvalidExecutionTreeObjectConversion(object.enum_to_string().to_string(), ExecutionTreeObject::bool_str().to_string()).into())
151 },
152 }
153 }
154}
155
156impl From<ExecutionTreeObject> for Result<char> {
157 fn from(object: ExecutionTreeObject) -> Result<char> {
158 match object {
159 ExecutionTreeObject::Char(result) => Ok(result),
160 object => {
161 Err(ErrorKind::InvalidExecutionTreeObjectConversion(object.enum_to_string().to_string(), ExecutionTreeObject::char_str().to_string()).into())
162 },
163 }
164 }
165}
166
167impl From<ExecutionTreeObject> for Result<f32> {
168 fn from(object: ExecutionTreeObject) -> Result<f32> {
169 match object {
170 ExecutionTreeObject::F32(result) => Ok(result),
171 object => {
172 Err(ErrorKind::InvalidExecutionTreeObjectConversion(object.enum_to_string().to_string(), ExecutionTreeObject::f32_str().to_string()).into())
173 },
174 }
175 }
176}
177
178impl From<ExecutionTreeObject> for Result<f64> {
179 fn from(object: ExecutionTreeObject) -> Result<f64> {
180 match object {
181 ExecutionTreeObject::F64(result) => Ok(result),
182 object => {
183 Err(ErrorKind::InvalidExecutionTreeObjectConversion(object.enum_to_string().to_string(), ExecutionTreeObject::f64_str().to_string()).into())
184 },
185 }
186 }
187}
188
189impl From<ExecutionTreeObject> for Result<i8> {
190 fn from(object: ExecutionTreeObject) -> Result<i8> {
191 match object {
192 ExecutionTreeObject::I8(result) => Ok(result),
193 object => {
194 Err(ErrorKind::InvalidExecutionTreeObjectConversion(object.enum_to_string().to_string(), ExecutionTreeObject::i8_str().to_string()).into())
195 },
196 }
197 }
198}
199
200impl From<ExecutionTreeObject> for Result<i16> {
201 fn from(object: ExecutionTreeObject) -> Result<i16> {
202 match object {
203 ExecutionTreeObject::I16(result) => Ok(result),
204 object => {
205 Err(ErrorKind::InvalidExecutionTreeObjectConversion(object.enum_to_string().to_string(), ExecutionTreeObject::i16_str().to_string()).into())
206 },
207 }
208 }
209}
210
211impl From<ExecutionTreeObject> for Result<i32> {
212 fn from(object: ExecutionTreeObject) -> Result<i32> {
213 match object {
214 ExecutionTreeObject::I32(result) => Ok(result),
215 object => {
216 Err(ErrorKind::InvalidExecutionTreeObjectConversion(object.enum_to_string().to_string(), ExecutionTreeObject::i32_str().to_string()).into())
217 },
218 }
219 }
220}
221
222impl From<ExecutionTreeObject> for Result<i64> {
223 fn from(object: ExecutionTreeObject) -> Result<i64> {
224 match object {
225 ExecutionTreeObject::I64(result) => Ok(result),
226 object => {
227 Err(ErrorKind::InvalidExecutionTreeObjectConversion(object.enum_to_string().to_string(), ExecutionTreeObject::i64_str().to_string()).into())
228 },
229 }
230 }
231}
232
233impl From<ExecutionTreeObject> for Result<isize> {
234 fn from(object: ExecutionTreeObject) -> Result<isize> {
235 match object {
236 ExecutionTreeObject::ISize(result) => Ok(result),
237 object => {
238 Err(ErrorKind::InvalidExecutionTreeObjectConversion(object.enum_to_string().to_string(), ExecutionTreeObject::isize_str().to_string()).into())
239 },
240 }
241 }
242}
243
244impl From<ExecutionTreeObject> for Result<ExecutionTreeNode> {
245 fn from(object: ExecutionTreeObject) -> Result<ExecutionTreeNode> {
246 match object {
247 ExecutionTreeObject::Node(result) => Ok(result),
248 object => {
249 Err(ErrorKind::InvalidExecutionTreeObjectConversion(object.enum_to_string().to_string(), ExecutionTreeObject::node_str().to_string()).into())
250 },
251 }
252 }
253}
254
255impl From<ExecutionTreeObject> for Result<String> {
256 fn from(object: ExecutionTreeObject) -> Result<String> {
257 match object {
258 ExecutionTreeObject::String(result) => Ok(result),
259 ExecutionTreeObject::Symbol(result) => Ok(result),
260 object => {
261 Err(ErrorKind::InvalidExecutionTreeObjectConversion(object.enum_to_string().to_string(), ExecutionTreeObject::string_str().to_string()).into())
262 },
263 }
264 }
265}
266
267impl From<ExecutionTreeObject> for Result<u8> {
268 fn from(object: ExecutionTreeObject) -> Result<u8> {
269 match object {
270 ExecutionTreeObject::U8(result) => Ok(result),
271 object => {
272 Err(ErrorKind::InvalidExecutionTreeObjectConversion(object.enum_to_string().to_string(), ExecutionTreeObject::u8_str().to_string()).into())
273 },
274 }
275 }
276}
277
278impl From<ExecutionTreeObject> for Result<u16> {
279 fn from(object: ExecutionTreeObject) -> Result<u16> {
280 match object {
281 ExecutionTreeObject::U16(result) => Ok(result),
282 object => {
283 Err(ErrorKind::InvalidExecutionTreeObjectConversion(object.enum_to_string().to_string(), ExecutionTreeObject::u16_str().to_string()).into())
284 },
285 }
286 }
287}
288
289impl From<ExecutionTreeObject> for Result<u32> {
290 fn from(object: ExecutionTreeObject) -> Result<u32> {
291 match object {
292 ExecutionTreeObject::U32(result) => Ok(result),
293 object => {
294 Err(ErrorKind::InvalidExecutionTreeObjectConversion(object.enum_to_string().to_string(), ExecutionTreeObject::u32_str().to_string()).into())
295 },
296 }
297 }
298}
299
300impl From<ExecutionTreeObject> for Result<u64> {
301 fn from(object: ExecutionTreeObject) -> Result<u64> {
302 match object {
303 ExecutionTreeObject::U64(result) => Ok(result),
304 object => {
305 Err(ErrorKind::InvalidExecutionTreeObjectConversion(object.enum_to_string().to_string(), ExecutionTreeObject::u64_str().to_string()).into())
306 },
307 }
308 }
309}
310
311impl From<ExecutionTreeObject> for Result<usize> {
312 fn from(object: ExecutionTreeObject) -> Result<usize> {
313 match object {
314 ExecutionTreeObject::USize(result) => Ok(result),
315 object => {
316 Err(ErrorKind::InvalidExecutionTreeObjectConversion(object.enum_to_string().to_string(), ExecutionTreeObject::usize_str().to_string()).into())
317 },
318 }
319 }
320}