| 1 | // (c) 2017 Productize SPRL <[email protected]> |
| 2 | |
| 3 | use std::iter::Peekable; |
| 4 | use std::slice::Iter; |
| 5 | |
| 6 | use Sexp; |
| 7 | use error::SexpError; |
| 8 | |
| 9 | /// convert an `&Sexp` to something |
| 10 | pub trait FromSexp |
| 11 | where |
| 12 | Self: Sized, |
| 13 | { |
| 14 | /// convert from a symbolic-expression to something |
| 15 | fn from_sexp(&Sexp) -> Result<Self, SexpError>; |
| 16 | } |
| 17 | |
| 18 | /// convert from a symbolic-expression to something (dispatcher) |
| 19 | pub fn from_sexp<T: FromSexp>(s: &Sexp) -> Result<T, SexpError> { |
| 20 | T::from_sexp(s) |
| 21 | } |
| 22 | |
| 23 | /// Atom iterator wrapper |
| 24 | pub struct IterAtom<'a> { |
| 25 | name: String, |
| 26 | /// containing iterable (pub for now) |
| 27 | pub iter: Peekable<Iter<'a, Sexp>>, |
| 28 | } |
| 29 | |
| 30 | |
| 31 | impl<'a> IterAtom<'a> { |
| 32 | /// deconstruct a `Sexp` that is a list and starts with 'name' |
| 33 | pub fn new(s: &'a Sexp, name: &str) -> Result<IterAtom<'a>, SexpError> { |
| 34 | let v = s.list()?; |
| 35 | let mut i = v.iter(); |
| 36 | let st = match i.next() { |
| 37 | None => return Err(format!("missing first element {} in list {}", name, s).into()), |
| 38 | Some(e) => e.string()?, |
| 39 | }; |
| 40 | if st != name { |
| 41 | return Err(format!("list {} doesn't start with {}, but with {}", s, name, st).into()); |
| 42 | } |
| 43 | let i = i.peekable(); |
| 44 | Ok(IterAtom { |
| 45 | name: name.into(), |
| 46 | iter: i, |
| 47 | }) |
| 48 | } |
| 49 | |
| 50 | /// deconstruct a `Sexp` that is a list and doesn't start with a name |
| 51 | pub fn new_nameless(s: &'a Sexp, name: &str) -> Result<IterAtom<'a>, SexpError> { |
| 52 | Ok(IterAtom { |
| 53 | name: name.into(), |
| 54 | iter: s.list()?.iter().peekable(), |
| 55 | }) |
| 56 | } |
| 57 | |
| 58 | /// expect a named field, retrieved by `get` |
| 59 | fn expect<T, F>(&mut self, name: &str, get: F) -> Result<T, SexpError> |
| 60 | where |
| 61 | F: Fn(&Sexp) -> Result<T, SexpError>, |
| 62 | { |
| 63 | match self.iter.next() { |
| 64 | Some(x) => get(x), |
| 65 | None => Err(format!("missing {} field in {}", name, self.name).into()), |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /// expect an integer while iterating a `Sexp` list |
| 70 | /// |
| 71 | /// shape: (... ... 42 ...) |
| 72 | pub fn i(&mut self, name: &str) -> Result<i64, SexpError> { |
| 73 | self.expect(name, |x| x.i().map_err(From::from)) |
| 74 | } |
| 75 | |
| 76 | /// expect a float while iterating a `Sexp` list |
| 77 | /// |
| 78 | /// shape: (... ... 42.7 ...) |
| 79 | pub fn f(&mut self, name: &str) -> Result<f64, SexpError> { |
| 80 | self.expect(name, |x| x.f().map_err(From::from)) |
| 81 | } |
| 82 | |
| 83 | /// expect a String while iterating a `Sexp` list |
| 84 | /// |
| 85 | /// shape: (... ... hello ...) |
| 86 | pub fn s(&mut self, name: &str) -> Result<String, SexpError> { |
| 87 | self.expect(name, |x| x.s().map_err(From::from)) |
| 88 | } |
| 89 | |
| 90 | /// expect a list contained String while iterating a `Sexp` list |
| 91 | /// |
| 92 | /// shape: (... ... hello ...) |
| 93 | pub fn s_in_list(&mut self, name: &str) -> Result<String, SexpError> { |
| 94 | self.expect(name, |x| x.named_value_s(name).map_err(From::from)) |
| 95 | } |
| 96 | |
| 97 | /// expect a list contained i64 while iterating a `Sexp` list |
| 98 | /// |
| 99 | /// shape: (... ... (name 42) ...) |
| 100 | pub fn i_in_list(&mut self, name: &str) -> Result<i64, SexpError> { |
| 101 | self.expect(name, |x| x.named_value_i(name).map_err(From::from)) |
| 102 | } |
| 103 | |
| 104 | /// expect a list contained f64 while iterating a `Sexp` list |
| 105 | /// |
| 106 | /// shape: (... ... (name 42.7) ...) |
| 107 | pub fn f_in_list(&mut self, name: &str) -> Result<f64, SexpError> { |
| 108 | self.expect(name, |x| x.named_value_f(name).map_err(From::from)) |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /// expect a `Sexp` while iterating a `Sexp` list |
| 113 | /// |
| 114 | /// shape: (... ... (...) ...) |
| 115 | pub fn t<T: FromSexp>(&mut self, name: &str) -> Result<T, SexpError> { |
| 116 | self.expect(name, |x| T::from_sexp(x)) |
| 117 | } |
| 118 | |
| 119 | /// expect a list containing a `Sexp` while iterating a `Sexp` list |
| 120 | /// |
| 121 | /// shape: (... ... (name (...)) ...) |
| 122 | pub fn t_in_list<T: FromSexp>(&mut self, name: &str) -> Result<T, SexpError> { |
| 123 | self.expect(name, |x| T::from_sexp(x.named_value(name)?)) |
| 124 | } |
| 125 | |
| 126 | /// expect remainder of iterator to be a `Vec<T>` |
| 127 | /// |
| 128 | /// shape: (... T T T) |
| 129 | pub fn vec<T: FromSexp>(&mut self) -> Result<Vec<T>, SexpError> { |
| 130 | let mut res = Vec::new(); |
| 131 | loop { |
| 132 | match self.iter.next() { |
| 133 | None => break, |
| 134 | Some(e) => { |
| 135 | let p = from_sexp(e)?; |
| 136 | res.push(p); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | Ok(res) |
| 141 | } |
| 142 | |
| 143 | /// maybe something while iterating a `Sexp` list |
| 144 | /// returns `None` when the `convert` function fails |
| 145 | fn maybe<X, F>(&mut self, convert: F) -> Option<X> |
| 146 | where |
| 147 | F: Fn(&Sexp) -> Result<X, SexpError>, |
| 148 | { |
| 149 | let res = match self.iter.peek() { |
| 150 | None => None, |
| 151 | Some(s) => match convert(s) { |
| 152 | Ok(t) => Some(t), |
| 153 | Err(_) => None, |
| 154 | }, |
| 155 | }; |
| 156 | match res { |
| 157 | Some(x) => { |
| 158 | let _ = self.iter.next(); |
| 159 | Some(x) |
| 160 | } |
| 161 | x => x, |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /// maybe a `FromSexp` while iterating a `Sexp` list |
| 166 | /// |
| 167 | /// shape: (... ... (...) ...) |
| 168 | pub fn maybe_t<T: FromSexp>(&mut self) -> Option<T> { |
| 169 | self.maybe(|x| T::from_sexp(x)) |
| 170 | } |
| 171 | |
| 172 | /// maybe a `String` while iterating a `Sexp` list |
| 173 | /// |
| 174 | /// shape: (... ... hello ...) |
| 175 | pub fn maybe_s(&mut self) -> Option<String> { |
| 176 | self.maybe(|x| x.s()) |
| 177 | } |
| 178 | |
| 179 | /// maybe a `String` while iterating a `Sexp` list |
| 180 | /// |
| 181 | /// shape: (... ... literal ...) |
| 182 | pub fn maybe_literal_s(&mut self, l: &str) -> Option<String> { |
| 183 | self.maybe(|x| { |
| 184 | let z = x.s()?; |
| 185 | if z == l { |
| 186 | Ok(z) |
| 187 | } else { |
| 188 | Err("unexpected".into()) |
| 189 | } |
| 190 | }) |
| 191 | } |
| 192 | |
| 193 | /// maybe an `i64` while iterating a `Sexp` list |
| 194 | /// |
| 195 | /// shape: (... ... 42 ...) |
| 196 | pub fn maybe_i(&mut self) -> Option<i64> { |
| 197 | self.maybe(|x| x.i()) |
| 198 | } |
| 199 | |
| 200 | /// maybe an `f64` while iterating a `Sexp` list |
| 201 | /// |
| 202 | /// shape: (... ... 42.7 ...) |
| 203 | pub fn maybe_f(&mut self) -> Option<f64> { |
| 204 | self.maybe(|x| x.f()) |
| 205 | } |
| 206 | |
| 207 | /// maybe a list containing a `String` while iterating a `Sexp` list |
| 208 | /// |
| 209 | /// shape: (... ... (name hello) ...) |
| 210 | pub fn maybe_s_in_list(&mut self, name: &str) -> Option<String> { |
| 211 | self.maybe(|x| x.named_value_s(name)) |
| 212 | } |
| 213 | |
| 214 | /// maybe a list containing an `i64` while iterating a `Sexp` list |
| 215 | /// |
| 216 | /// shape: (... ... (name 42) ...) |
| 217 | pub fn maybe_i_in_list(&mut self, name: &str) -> Option<i64> { |
| 218 | self.maybe(|x| x.named_value_i(name)) |
| 219 | } |
| 220 | |
| 221 | /// maybe a list containing an `f64` while iterating a `Sexp` list |
| 222 | /// |
| 223 | /// shape: (... ... (name 42.7) ...) |
| 224 | pub fn maybe_f_in_list(&mut self, name: &str) -> Option<f64> { |
| 225 | self.maybe(|x| x.named_value_f(name)) |
| 226 | } |
| 227 | |
| 228 | /// make sure we consumed all of the iterator |
| 229 | /// |
| 230 | /// shape: ) |
| 231 | pub fn close<T>(&mut self, x: T) -> Result<T, SexpError> { |
| 232 | match self.iter.next() { |
| 233 | Some(x) => Err(format!("Unexpected {} in {}", x, self.name).into()), |
| 234 | None => Ok(x), |
| 235 | } |
| 236 | } |
| 237 | } |