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
use std::fmt;
use std::result;
use std::str::FromStr;
use str_error;
use Result;
pub trait IntoSexp {
fn into_sexp(&self) -> Sexp;
}
#[derive(Debug, Clone)]
pub enum Sexp {
String(String),
List(Vec<Sexp>),
Empty,
}
pub fn encode_string(s: &str) -> String {
if s.contains('(') || s.contains(' ') || s.contains(')') || s.contains('\t') ||
s.contains('{') || s.contains('}') || s.contains('}') || s.contains('%') ||
s.is_empty()
{
format!("\"{}\"", s)
} else {
String::from(s)
}
}
impl Sexp {
pub fn new_empty() -> Sexp {
Sexp::Empty
}
pub fn new_string<T>(s: T) -> Sexp
where T: fmt::Display
{
Sexp::String(format!("{}", s))
}
pub fn new_list(v: Vec<Sexp>) -> Sexp {
Sexp::List(v)
}
pub fn new_named_list<F>(name: &str, fill: F) -> Sexp
where F: Fn(&mut Vec<Sexp>)
{
let mut v = vec![];
v.push(Sexp::new_string(name));
fill(&mut v);
Sexp::List(v)
}
pub fn new_named<T>(name: &str, value: T) -> Sexp
where T: fmt::Display
{
let mut v = vec![];
v.push(Sexp::new_string(name));
v.push(Sexp::new_string(value));
Sexp::List(v)
}
pub fn new_named_sexp<T>(name: &str, value: &T) -> Sexp
where T: IntoSexp
{
let mut v = vec![];
v.push(Sexp::new_string(name));
v.push(value.into_sexp());
Sexp::List(v)
}
pub fn from<T: IntoSexp>(t: &T) -> Sexp {
t.into_sexp()
}
pub fn list(&self) -> Result<&Vec<Sexp>> {
match *self {
Sexp::List(ref v) => Ok(v),
_ => str_error(format!("not a list: {}", self)),
}
}
pub fn string(&self) -> Result<&String> {
match *self {
Sexp::String(ref s) => Ok(s),
_ => str_error(format!("not a string: {}", self)),
}
}
pub fn f(&self) -> Result<f64> {
let s = try!(self.string());
match f64::from_str(s) {
Ok(f) => Ok(f),
_ => str_error("Error parsing float".to_string()),
}
}
pub fn i(&self) -> Result<i64> {
let s = try!(self.string());
match i64::from_str(s) {
Ok(f) => Ok(f),
_ => str_error("Error parsing int".to_string()),
}
}
pub fn list_name(&self) -> Result<&String> {
let l = try!(self.list());
let l = &l[..];
let a = try!(l[0].string());
Ok(a)
}
pub fn slice_atom(&self, s: &str) -> Result<&[Sexp]> {
let v = try!(self.list());
let v2 = &v[..];
let st = try!(v2[0].string());
if st != s {
return str_error(format!("list {} doesn't start with {}, but with {}", self, s, st));
};
Ok(&v[1..])
}
pub fn named_value(&self, s: &str) -> Result<&Sexp> {
let v = try!(self.list());
if v.len() != 2 {
return str_error(format!("list {} is not a named_value", s));
}
let l = try!(self.slice_atom(s));
Ok(&l[0])
}
pub fn named_value_i(&self, s: &str) -> Result<i64> {
try!(self.named_value(s)).i()
}
pub fn named_value_f(&self, s: &str) -> Result<f64> {
try!(self.named_value(s)).f()
}
pub fn named_value_string(&self, s: &str) -> Result<&String> {
try!(self.named_value(s)).string()
}
pub fn slice_atom_num(&self, s: &str, num: usize) -> Result<&[Sexp]> {
let v = try!(self.list());
let v2 = &v[..];
let st = try!(v2[0].string());
if st != s {
return str_error(format!("list doesn't start with {}, but with {}", s, st));
};
if v.len() != (num + 1) {
return str_error(format!("list ({}) doesn't have {} elements but {}",
s,
num,
v.len() - 1));
}
Ok(&v[1..])
}
}
impl fmt::Display for Sexp {
fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
match *self {
Sexp::String(ref s) => write!(f, "{}", encode_string(s)),
Sexp::List(ref v) => {
try!(write!(f, "("));
let l = v.len();
for (i, x) in v.iter().enumerate() {
if i < l - 1 {
try!(write!(f, "{} ", x));
} else {
try!(write!(f, "{}", x));
}
}
write!(f, ")")
}
Sexp::Empty => Ok(()),
}
}
}