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
// (c) 2017 Productize SPRL <joost@productize.be>

use std::iter::Peekable;
use std::slice::Iter;

use Sexp;
use error::SexpError;

/// convert an `&Sexp` to something
pub trait FromSexp
where
    Self: Sized,
{
    /// convert from a symbolic-expression to something
    fn from_sexp(&Sexp) -> Result<Self, SexpError>;
}

/// convert from a symbolic-expression to something (dispatcher)
pub fn from_sexp<T: FromSexp>(s: &Sexp) -> Result<T, SexpError> {
    T::from_sexp(s)
}

/// Atom iterator wrapper
pub struct IterAtom<'a> {
    name: String,
    /// containing iterable (pub for now)
    pub iter: Peekable<Iter<'a, Sexp>>,
}


impl<'a> IterAtom<'a> {
    /// deconstruct a `Sexp` that is a list and starts with 'name'
    pub fn new(s: &'a Sexp, name: &str) -> Result<IterAtom<'a>, SexpError> {
        let v = s.list()?;
        let mut i = v.iter();
        let st = match i.next() {
            None => return Err(format!("missing first element {} in list {}", name, s).into()),
            Some(e) => e.string()?,
        };
        if st != name {
            return Err(format!("list {} doesn't start with {}, but with {}", s, name, st).into());
        }
        let i = i.peekable();
        Ok(IterAtom {
            name: name.into(),
            iter: i,
        })
    }

    /// deconstruct a `Sexp` that is a list and doesn't start with a name
    pub fn new_nameless(s: &'a Sexp, name: &str) -> Result<IterAtom<'a>, SexpError> {
        Ok(IterAtom {
            name: name.into(),
            iter: s.list()?.iter().peekable(),
        })
    }

    /// expect a named field, retrieved by `get`
    fn expect<T, F>(&mut self, name: &str, get: F) -> Result<T, SexpError>
    where
        F: Fn(&Sexp) -> Result<T, SexpError>,
    {
        match self.iter.next() {
            Some(x) => get(x),
            None => Err(format!("missing {} field in {}", name, self.name).into()),
        }
    }

    /// expect an integer while iterating a `Sexp` list
    ///
    /// shape: (... ... 42 ...)
    pub fn i(&mut self, name: &str) -> Result<i64, SexpError> {
        self.expect(name, |x| x.i().map_err(From::from))
    }

    /// expect a float while iterating a `Sexp` list
    ///
    /// shape: (... ... 42.7 ...)
    pub fn f(&mut self, name: &str) -> Result<f64, SexpError> {
        self.expect(name, |x| x.f().map_err(From::from))
    }

    /// expect a String while iterating a `Sexp` list
    ///
    /// shape: (... ... hello ...)
    pub fn s(&mut self, name: &str) -> Result<String, SexpError> {
        self.expect(name, |x| x.s().map_err(From::from))
    }

    /// expect a list contained String while iterating a `Sexp` list
    ///
    /// shape: (... ... hello ...)
    pub fn s_in_list(&mut self, name: &str) -> Result<String, SexpError> {
        self.expect(name, |x| x.named_value_s(name).map_err(From::from))
    }

    /// expect a list contained i64 while iterating a `Sexp` list
    ///
    /// shape: (... ... (name 42) ...)
    pub fn i_in_list(&mut self, name: &str) -> Result<i64, SexpError> {
        self.expect(name, |x| x.named_value_i(name).map_err(From::from))
    }

    /// expect a list contained f64 while iterating a `Sexp` list
    ///
    /// shape: (... ... (name 42.7) ...)
    pub fn f_in_list(&mut self, name: &str) -> Result<f64, SexpError> {
        self.expect(name, |x| x.named_value_f(name).map_err(From::from))
    }


    /// expect a `Sexp` while iterating a `Sexp` list
    ///
    /// shape: (... ... (...) ...)
    pub fn t<T: FromSexp>(&mut self, name: &str) -> Result<T, SexpError> {
        self.expect(name, |x| T::from_sexp(x))
    }

    /// expect a list containing a `Sexp` while iterating a `Sexp` list
    ///
    /// shape: (... ... (name (...)) ...)
    pub fn t_in_list<T: FromSexp>(&mut self, name: &str) -> Result<T, SexpError> {
        self.expect(name, |x| T::from_sexp(x.named_value(name)?))
    }

    /// expect remainder of iterator to be a `Vec<T>`
    ///
    /// shape: (... T T T)
    pub fn vec<T: FromSexp>(&mut self) -> Result<Vec<T>, SexpError> {
        let mut res = Vec::new();
        loop {
            match self.iter.next() {
                None => break,
                Some(e) => {
                    let p = from_sexp(e)?;
                    res.push(p);
                }
            }
        }
        Ok(res)
    }

    /// maybe something while iterating a `Sexp` list
    /// returns `None` when the `convert` function fails
    fn maybe<X, F>(&mut self, convert: F) -> Option<X>
    where
        F: Fn(&Sexp) -> Result<X, SexpError>,
    {
        let res = match self.iter.peek() {
            None => None,
            Some(s) => match convert(s) {
                Ok(t) => Some(t),
                Err(_) => None,
            },
        };
        match res {
            Some(x) => {
                let _ = self.iter.next();
                Some(x)
            }
            x => x,
        }
    }

    /// maybe a `FromSexp` while iterating a `Sexp` list
    ///
    /// shape: (... ... (...) ...)
    pub fn maybe_t<T: FromSexp>(&mut self) -> Option<T> {
        self.maybe(|x| T::from_sexp(x))
    }

    /// maybe a `String` while iterating a `Sexp` list
    ///
    /// shape: (... ... hello ...)
    pub fn maybe_s(&mut self) -> Option<String> {
        self.maybe(|x| x.s())
    }

    /// maybe a `String` while iterating a `Sexp` list
    ///
    /// shape: (... ... literal ...)
    pub fn maybe_literal_s(&mut self, l: &str) -> Option<String> {
        self.maybe(|x| {
            let z = x.s()?;
            if z == l {
                Ok(z)
            } else {
                Err("unexpected".into())
            }
        })
    }

    /// maybe an `i64` while iterating a `Sexp` list
    ///
    /// shape: (... ... 42 ...)
    pub fn maybe_i(&mut self) -> Option<i64> {
        self.maybe(|x| x.i())
    }

    /// maybe an `f64` while iterating a `Sexp` list
    ///
    /// shape: (... ... 42.7 ...)
    pub fn maybe_f(&mut self) -> Option<f64> {
        self.maybe(|x| x.f())
    }

    /// maybe a list containing a `String` while iterating a `Sexp` list
    ///
    /// shape: (... ... (name hello) ...)
    pub fn maybe_s_in_list(&mut self, name: &str) -> Option<String> {
        self.maybe(|x| x.named_value_s(name))
    }

    /// maybe a list containing an `i64` while iterating a `Sexp` list
    ///
    /// shape: (... ... (name 42) ...)
    pub fn maybe_i_in_list(&mut self, name: &str) -> Option<i64> {
        self.maybe(|x| x.named_value_i(name))
    }

    /// maybe a list containing an `f64` while iterating a `Sexp` list
    ///
    /// shape: (... ... (name 42.7) ...)
    pub fn maybe_f_in_list(&mut self, name: &str) -> Option<f64> {
        self.maybe(|x| x.named_value_f(name))
    }

    /// make sure we consumed all of the iterator
    ///
    /// shape: )
    pub fn close<T>(&mut self, x: T) -> Result<T, SexpError> {
        match self.iter.next() {
            Some(x) => Err(format!("Unexpected {} in {}", x, self.name).into()),
            None => Ok(x),
        }
    }
}