| 1 | use crate::tokens::{Error as TokenError, Span, Token, Tokenizer}; |
| 2 | use serde::de; |
| 3 | use serde::de::IntoDeserializer; |
| 4 | use std::borrow::Cow; |
| 5 | use std::collections::{HashMap, HashSet}; |
| 6 | use std::error; |
| 7 | use std::f64; |
| 8 | use std::fmt::{self, Display}; |
| 9 | use std::iter; |
| 10 | use std::str; |
| 11 | use std::vec; |
| 12 | |
| 13 | type TablePair<'a> = ((Span, Cow<'a, str>), Value<'a>); |
| 14 | |
| 15 | /// Deserializes a byte slice into a type. |
| 16 | /// |
| 17 | /// This function will attempt to interpret `bytes` as UTF-8 data and then |
| 18 | /// deserialize `T` from the TOML document provided. |
| 19 | pub fn from_slice<'de, T>(bytes: &'de [u8]) -> Result<T, crate::Error> |
| 20 | where |
| 21 | T: de::Deserialize<'de>, |
| 22 | { |
| 23 | match str::from_utf8(bytes) { |
| 24 | Ok(s: &str) => from_str(s), |
| 25 | Err(e: Utf8Error) => Err(crate::Error::from(*Error::custom(at:None, s:e.to_string()))), |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | /// Deserializes a string into a type. |
| 30 | /// |
| 31 | /// This function will attempt to interpret `s` as a TOML document and |
| 32 | /// deserialize `T` from the document. |
| 33 | pub fn from_str<'de, T>(s: &'de str) -> Result<T, crate::Error> |
| 34 | where |
| 35 | T: de::Deserialize<'de>, |
| 36 | { |
| 37 | let mut d: Deserializer<'_> = Deserializer::new(input:s); |
| 38 | T::deserialize(&mut d).map_err(|e: Box| crate::Error::from(*e)) |
| 39 | } |
| 40 | |
| 41 | #[derive (Debug)] |
| 42 | pub(crate) struct Error { |
| 43 | kind: ErrorKind, |
| 44 | line: Option<usize>, |
| 45 | col: usize, |
| 46 | at: Option<usize>, |
| 47 | message: String, |
| 48 | key: Vec<String>, |
| 49 | } |
| 50 | |
| 51 | /// Errors that can occur when deserializing a type. |
| 52 | #[derive (Debug)] |
| 53 | enum ErrorKind { |
| 54 | /// EOF was reached when looking for a value. |
| 55 | UnexpectedEof, |
| 56 | |
| 57 | /// An invalid character not allowed in a string was found. |
| 58 | InvalidCharInString(char), |
| 59 | |
| 60 | /// An invalid character was found as an escape. |
| 61 | InvalidEscape(char), |
| 62 | |
| 63 | /// An invalid character was found in a hex escape. |
| 64 | InvalidHexEscape(char), |
| 65 | |
| 66 | /// An invalid escape value was specified in a hex escape in a string. |
| 67 | /// |
| 68 | /// Valid values are in the plane of unicode codepoints. |
| 69 | InvalidEscapeValue(u32), |
| 70 | |
| 71 | /// A newline in a string was encountered when one was not allowed. |
| 72 | NewlineInString, |
| 73 | |
| 74 | /// An unexpected character was encountered, typically when looking for a |
| 75 | /// value. |
| 76 | Unexpected(char), |
| 77 | |
| 78 | /// An unterminated string was found where EOF was found before the ending |
| 79 | /// EOF mark. |
| 80 | UnterminatedString, |
| 81 | |
| 82 | /// A newline was found in a table key. |
| 83 | NewlineInTableKey, |
| 84 | |
| 85 | /// A number failed to parse. |
| 86 | NumberInvalid, |
| 87 | |
| 88 | /// Wanted one sort of token, but found another. |
| 89 | Wanted { |
| 90 | /// Expected token type. |
| 91 | expected: &'static str, |
| 92 | /// Actually found token type. |
| 93 | found: &'static str, |
| 94 | }, |
| 95 | |
| 96 | /// A duplicate table definition was found. |
| 97 | DuplicateTable(String), |
| 98 | |
| 99 | /// Duplicate key in table. |
| 100 | DuplicateKey(String), |
| 101 | |
| 102 | /// A previously defined table was redefined as an array. |
| 103 | RedefineAsArray, |
| 104 | |
| 105 | /// Multiline strings are not allowed for key. |
| 106 | MultilineStringKey, |
| 107 | |
| 108 | /// A custom error which could be generated when deserializing a particular |
| 109 | /// type. |
| 110 | Custom, |
| 111 | |
| 112 | /// A tuple with a certain number of elements was expected but something |
| 113 | /// else was found. |
| 114 | ExpectedTuple(usize), |
| 115 | |
| 116 | /// Expected table keys to be in increasing tuple index order, but something |
| 117 | /// else was found. |
| 118 | ExpectedTupleIndex { |
| 119 | /// Expected index. |
| 120 | expected: usize, |
| 121 | /// Key that was specified. |
| 122 | found: String, |
| 123 | }, |
| 124 | |
| 125 | /// An empty table was expected but entries were found. |
| 126 | ExpectedEmptyTable, |
| 127 | |
| 128 | /// Dotted key attempted to extend something that is not a table. |
| 129 | DottedKeyInvalidType, |
| 130 | |
| 131 | /// An unexpected key was encountered. |
| 132 | /// |
| 133 | /// Used when deserializing a struct with a limited set of fields. |
| 134 | UnexpectedKeys { |
| 135 | /// The unexpected keys. |
| 136 | keys: Vec<String>, |
| 137 | /// Keys that may be specified. |
| 138 | available: &'static [&'static str], |
| 139 | }, |
| 140 | |
| 141 | /// Unquoted string was found when quoted one was expected. |
| 142 | UnquotedString, |
| 143 | } |
| 144 | |
| 145 | struct Deserializer<'a> { |
| 146 | input: &'a str, |
| 147 | tokens: Tokenizer<'a>, |
| 148 | } |
| 149 | |
| 150 | impl<'de, 'b> de::Deserializer<'de> for &'b mut Deserializer<'de> { |
| 151 | type Error = Box<Error>; |
| 152 | |
| 153 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Box<Error>> |
| 154 | where |
| 155 | V: de::Visitor<'de>, |
| 156 | { |
| 157 | let mut tables = self.tables()?; |
| 158 | let table_indices = build_table_indices(&tables); |
| 159 | let table_pindices = build_table_pindices(&tables); |
| 160 | |
| 161 | let res = visitor.visit_map(MapVisitor { |
| 162 | values: Vec::new().into_iter().peekable(), |
| 163 | next_value: None, |
| 164 | depth: 0, |
| 165 | cur: 0, |
| 166 | cur_parent: 0, |
| 167 | max: tables.len(), |
| 168 | table_indices: &table_indices, |
| 169 | table_pindices: &table_pindices, |
| 170 | tables: &mut tables, |
| 171 | array: false, |
| 172 | de: self, |
| 173 | keys: HashSet::new(), |
| 174 | }); |
| 175 | res.map_err(|mut err| { |
| 176 | // Errors originating from this library (toml), have an offset |
| 177 | // attached to them already. Other errors, like those originating |
| 178 | // from serde (like "missing field") or from a custom deserializer, |
| 179 | // do not have offsets on them. Here, we do a best guess at their |
| 180 | // location, by attributing them to the "current table" (the last |
| 181 | // item in `tables`). |
| 182 | err.fix_offset(|| tables.last().map(|table| table.at)); |
| 183 | err.fix_linecol(|at| self.to_linecol(at)); |
| 184 | err |
| 185 | }) |
| 186 | } |
| 187 | |
| 188 | serde::forward_to_deserialize_any! { |
| 189 | bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq |
| 190 | bytes byte_buf map unit newtype_struct |
| 191 | ignored_any unit_struct tuple_struct tuple option identifier struct enum |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // Builds a datastructure that allows for efficient sublinear lookups. The |
| 196 | // returned HashMap contains a mapping from table header (like [a.b.c]) to list |
| 197 | // of tables with that precise name. The tables are being identified by their |
| 198 | // index in the passed slice. We use a list as the implementation uses this data |
| 199 | // structure for arrays as well as tables, so if any top level [[name]] array |
| 200 | // contains multiple entries, there are multiple entries in the list. The lookup |
| 201 | // is performed in the `SeqAccess` implementation of `MapVisitor`. The lists are |
| 202 | // ordered, which we exploit in the search code by using bisection. |
| 203 | fn build_table_indices<'de>(tables: &[Table<'de>]) -> HashMap<Vec<Cow<'de, str>>, Vec<usize>> { |
| 204 | let mut res: HashMap>, …> = HashMap::new(); |
| 205 | for (i: usize, table: &Table<'_>) in tables.iter().enumerate() { |
| 206 | let header: Vec> = table.header.iter().map(|v: &(Span, Cow<'_, str>)| v.1.clone()).collect::<Vec<_>>(); |
| 207 | res.entry(header).or_insert_with(default:Vec::new).push(i); |
| 208 | } |
| 209 | res |
| 210 | } |
| 211 | |
| 212 | // Builds a datastructure that allows for efficient sublinear lookups. The |
| 213 | // returned HashMap contains a mapping from table header (like [a.b.c]) to list |
| 214 | // of tables whose name at least starts with the specified name. So searching |
| 215 | // for [a.b] would give both [a.b.c.d] as well as [a.b.e]. The tables are being |
| 216 | // identified by their index in the passed slice. |
| 217 | // |
| 218 | // A list is used for two reasons: First, the implementation also stores arrays |
| 219 | // in the same data structure and any top level array of size 2 or greater |
| 220 | // creates multiple entries in the list with the same shared name. Second, there |
| 221 | // can be multiple tables sharing the same prefix. |
| 222 | // |
| 223 | // The lookup is performed in the `MapAccess` implementation of `MapVisitor`. |
| 224 | // The lists are ordered, which we exploit in the search code by using |
| 225 | // bisection. |
| 226 | fn build_table_pindices<'de>(tables: &[Table<'de>]) -> HashMap<Vec<Cow<'de, str>>, Vec<usize>> { |
| 227 | let mut res: HashMap>, …> = HashMap::new(); |
| 228 | for (i: usize, table: &Table<'_>) in tables.iter().enumerate() { |
| 229 | let header: Vec> = table.header.iter().map(|v: &(Span, Cow<'_, str>)| v.1.clone()).collect::<Vec<_>>(); |
| 230 | for len: usize in 0..=header.len() { |
| 231 | res&mut Vec.entry(header[..len].to_owned()) |
| 232 | .or_insert_with(default:Vec::new) |
| 233 | .push(i); |
| 234 | } |
| 235 | } |
| 236 | res |
| 237 | } |
| 238 | |
| 239 | fn headers_equal(hdr_a: &[(Span, Cow<str>)], hdr_b: &[(Span, Cow<str>)]) -> bool { |
| 240 | if hdr_a.len() != hdr_b.len() { |
| 241 | return false; |
| 242 | } |
| 243 | hdr_a.iter().zip(hdr_b.iter()).all(|(h1: &(Span, Cow<'_, str>), h2: &(Span, Cow<'_, str>))| h1.1 == h2.1) |
| 244 | } |
| 245 | |
| 246 | struct Table<'a> { |
| 247 | at: usize, |
| 248 | header: Vec<(Span, Cow<'a, str>)>, |
| 249 | values: Option<Vec<TablePair<'a>>>, |
| 250 | array: bool, |
| 251 | } |
| 252 | |
| 253 | struct MapVisitor<'de, 'b> { |
| 254 | values: iter::Peekable<vec::IntoIter<TablePair<'de>>>, |
| 255 | next_value: Option<TablePair<'de>>, |
| 256 | depth: usize, |
| 257 | cur: usize, |
| 258 | cur_parent: usize, |
| 259 | max: usize, |
| 260 | table_indices: &'b HashMap<Vec<Cow<'de, str>>, Vec<usize>>, |
| 261 | table_pindices: &'b HashMap<Vec<Cow<'de, str>>, Vec<usize>>, |
| 262 | tables: &'b mut [Table<'de>], |
| 263 | array: bool, |
| 264 | de: &'b mut Deserializer<'de>, |
| 265 | keys: HashSet<Cow<'de, str>>, |
| 266 | } |
| 267 | |
| 268 | impl<'de, 'b> de::MapAccess<'de> for MapVisitor<'de, 'b> { |
| 269 | type Error = Box<Error>; |
| 270 | |
| 271 | fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Box<Error>> |
| 272 | where |
| 273 | K: de::DeserializeSeed<'de>, |
| 274 | { |
| 275 | if self.cur_parent == self.max || self.cur == self.max { |
| 276 | return Ok(None); |
| 277 | } |
| 278 | |
| 279 | loop { |
| 280 | assert!(self.next_value.is_none()); |
| 281 | if let Some(((span, key), value)) = self.values.next() { |
| 282 | if !self.keys.insert(key.clone()) { |
| 283 | return Err(Error::from_kind( |
| 284 | Some(span.start), |
| 285 | ErrorKind::DuplicateKey(key.into_owned()), |
| 286 | )); |
| 287 | } |
| 288 | let ret = seed.deserialize(StrDeserializer::new(key.clone()))?; |
| 289 | self.next_value = Some(((span, key), value)); |
| 290 | return Ok(Some(ret)); |
| 291 | } |
| 292 | |
| 293 | let next_table = { |
| 294 | let prefix_stripped = self.tables[self.cur_parent].header[..self.depth] |
| 295 | .iter() |
| 296 | .map(|v| v.1.clone()) |
| 297 | .collect::<Vec<_>>(); |
| 298 | self.table_pindices |
| 299 | .get(&prefix_stripped) |
| 300 | .and_then(|entries| { |
| 301 | let start = entries.binary_search(&self.cur).unwrap_or_else(|v| v); |
| 302 | if start == entries.len() || entries[start] < self.cur { |
| 303 | return None; |
| 304 | } |
| 305 | entries[start..] |
| 306 | .iter() |
| 307 | .filter_map(|i| if *i < self.max { Some(*i) } else { None }) |
| 308 | .map(|i| (i, &self.tables[i])) |
| 309 | .find(|(_, table)| table.values.is_some()) |
| 310 | .map(|p| p.0) |
| 311 | }) |
| 312 | }; |
| 313 | |
| 314 | let pos = match next_table { |
| 315 | Some(pos) => pos, |
| 316 | None => return Ok(None), |
| 317 | }; |
| 318 | self.cur = pos; |
| 319 | |
| 320 | // Test to see if we're duplicating our parent's table, and if so |
| 321 | // then this is an error in the toml format |
| 322 | if self.cur_parent != pos { |
| 323 | if headers_equal( |
| 324 | &self.tables[self.cur_parent].header, |
| 325 | &self.tables[pos].header, |
| 326 | ) { |
| 327 | let at = self.tables[pos].at; |
| 328 | let name = self.tables[pos] |
| 329 | .header |
| 330 | .iter() |
| 331 | .map(|k| k.1.clone()) |
| 332 | .collect::<Vec<_>>() |
| 333 | .join("." ); |
| 334 | return Err(self.de.error(at, ErrorKind::DuplicateTable(name))); |
| 335 | } |
| 336 | |
| 337 | // If we're here we know we should share the same prefix, and if |
| 338 | // the longer table was defined first then we want to narrow |
| 339 | // down our parent's length if possible to ensure that we catch |
| 340 | // duplicate tables defined afterwards. |
| 341 | let parent_len = self.tables[self.cur_parent].header.len(); |
| 342 | let cur_len = self.tables[pos].header.len(); |
| 343 | if cur_len < parent_len { |
| 344 | self.cur_parent = pos; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | let table = &mut self.tables[pos]; |
| 349 | |
| 350 | // If we're not yet at the appropriate depth for this table then we |
| 351 | // just next the next portion of its header and then continue |
| 352 | // decoding. |
| 353 | if self.depth != table.header.len() { |
| 354 | let (span, key) = &table.header[self.depth]; |
| 355 | if !self.keys.insert(key.clone()) { |
| 356 | return Err(Error::from_kind( |
| 357 | Some(span.start), |
| 358 | ErrorKind::DuplicateKey(key.clone().into_owned()), |
| 359 | )); |
| 360 | } |
| 361 | let key = seed.deserialize(StrDeserializer::new(key.clone()))?; |
| 362 | return Ok(Some(key)); |
| 363 | } |
| 364 | |
| 365 | // Rule out cases like: |
| 366 | // |
| 367 | // [[foo.bar]] |
| 368 | // [[foo]] |
| 369 | if table.array { |
| 370 | let kind = ErrorKind::RedefineAsArray; |
| 371 | return Err(self.de.error(table.at, kind)); |
| 372 | } |
| 373 | |
| 374 | self.values = table |
| 375 | .values |
| 376 | .take() |
| 377 | .expect("Unable to read table values" ) |
| 378 | .into_iter() |
| 379 | .peekable(); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Box<Error>> |
| 384 | where |
| 385 | V: de::DeserializeSeed<'de>, |
| 386 | { |
| 387 | if let Some((k, v)) = self.next_value.take() { |
| 388 | match seed.deserialize(ValueDeserializer::new(v)) { |
| 389 | Ok(v) => return Ok(v), |
| 390 | Err(mut e) => { |
| 391 | e.add_key_context(&k.1); |
| 392 | return Err(e); |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | let array = |
| 398 | self.tables[self.cur].array && self.depth == self.tables[self.cur].header.len() - 1; |
| 399 | self.cur += 1; |
| 400 | let res = seed.deserialize(MapVisitor { |
| 401 | values: Vec::new().into_iter().peekable(), |
| 402 | next_value: None, |
| 403 | depth: self.depth + if array { 0 } else { 1 }, |
| 404 | cur_parent: self.cur - 1, |
| 405 | cur: 0, |
| 406 | max: self.max, |
| 407 | array, |
| 408 | table_indices: self.table_indices, |
| 409 | table_pindices: self.table_pindices, |
| 410 | tables: &mut *self.tables, |
| 411 | de: &mut *self.de, |
| 412 | keys: HashSet::new(), |
| 413 | }); |
| 414 | res.map_err(|mut e| { |
| 415 | e.add_key_context(&self.tables[self.cur - 1].header[self.depth].1); |
| 416 | e |
| 417 | }) |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | impl<'de, 'b> de::SeqAccess<'de> for MapVisitor<'de, 'b> { |
| 422 | type Error = Box<Error>; |
| 423 | |
| 424 | fn next_element_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Box<Error>> |
| 425 | where |
| 426 | K: de::DeserializeSeed<'de>, |
| 427 | { |
| 428 | assert!(self.next_value.is_none()); |
| 429 | assert!(self.values.next().is_none()); |
| 430 | |
| 431 | if self.cur_parent == self.max { |
| 432 | return Ok(None); |
| 433 | } |
| 434 | |
| 435 | let header_stripped = self.tables[self.cur_parent] |
| 436 | .header |
| 437 | .iter() |
| 438 | .map(|v| v.1.clone()) |
| 439 | .collect::<Vec<_>>(); |
| 440 | let start_idx = self.cur_parent + 1; |
| 441 | let next = self |
| 442 | .table_indices |
| 443 | .get(&header_stripped) |
| 444 | .and_then(|entries| { |
| 445 | let start = entries.binary_search(&start_idx).unwrap_or_else(|v| v); |
| 446 | if start == entries.len() || entries[start] < start_idx { |
| 447 | return None; |
| 448 | } |
| 449 | entries[start..] |
| 450 | .iter() |
| 451 | .filter_map(|i| if *i < self.max { Some(*i) } else { None }) |
| 452 | .map(|i| (i, &self.tables[i])) |
| 453 | .find(|(_, table)| table.array) |
| 454 | .map(|p| p.0) |
| 455 | }) |
| 456 | .unwrap_or(self.max); |
| 457 | |
| 458 | let ret = seed.deserialize(MapVisitor { |
| 459 | values: self.tables[self.cur_parent] |
| 460 | .values |
| 461 | .take() |
| 462 | .expect("Unable to read table values" ) |
| 463 | .into_iter() |
| 464 | .peekable(), |
| 465 | next_value: None, |
| 466 | depth: self.depth + 1, |
| 467 | cur_parent: self.cur_parent, |
| 468 | max: next, |
| 469 | cur: 0, |
| 470 | array: false, |
| 471 | table_indices: self.table_indices, |
| 472 | table_pindices: self.table_pindices, |
| 473 | tables: self.tables, |
| 474 | de: self.de, |
| 475 | keys: HashSet::new(), |
| 476 | })?; |
| 477 | self.cur_parent = next; |
| 478 | Ok(Some(ret)) |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | impl<'de, 'b> de::Deserializer<'de> for MapVisitor<'de, 'b> { |
| 483 | type Error = Box<Error>; |
| 484 | |
| 485 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Box<Error>> |
| 486 | where |
| 487 | V: de::Visitor<'de>, |
| 488 | { |
| 489 | if self.array { |
| 490 | visitor.visit_seq(self) |
| 491 | } else { |
| 492 | visitor.visit_map(self) |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | // `None` is interpreted as a missing field so be sure to implement `Some` |
| 497 | // as a present field. |
| 498 | fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Box<Error>> |
| 499 | where |
| 500 | V: de::Visitor<'de>, |
| 501 | { |
| 502 | visitor.visit_some(self) |
| 503 | } |
| 504 | |
| 505 | fn deserialize_newtype_struct<V>( |
| 506 | self, |
| 507 | _name: &'static str, |
| 508 | visitor: V, |
| 509 | ) -> Result<V::Value, Box<Error>> |
| 510 | where |
| 511 | V: de::Visitor<'de>, |
| 512 | { |
| 513 | visitor.visit_newtype_struct(self) |
| 514 | } |
| 515 | |
| 516 | serde::forward_to_deserialize_any! { |
| 517 | bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq |
| 518 | bytes byte_buf map unit identifier |
| 519 | ignored_any unit_struct tuple_struct tuple struct enum |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | struct StrDeserializer<'a> { |
| 524 | key: Cow<'a, str>, |
| 525 | } |
| 526 | |
| 527 | impl<'a> StrDeserializer<'a> { |
| 528 | fn new(key: Cow<'a, str>) -> StrDeserializer<'a> { |
| 529 | StrDeserializer { key } |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | impl<'a> de::IntoDeserializer<'a, Box<Error>> for StrDeserializer<'a> { |
| 534 | type Deserializer = Self; |
| 535 | |
| 536 | fn into_deserializer(self) -> Self::Deserializer { |
| 537 | self |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | impl<'de> de::Deserializer<'de> for StrDeserializer<'de> { |
| 542 | type Error = Box<Error>; |
| 543 | |
| 544 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Box<Error>> |
| 545 | where |
| 546 | V: de::Visitor<'de>, |
| 547 | { |
| 548 | match self.key { |
| 549 | Cow::Borrowed(s: &str) => visitor.visit_borrowed_str(s), |
| 550 | Cow::Owned(s: String) => visitor.visit_string(s), |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | serde::forward_to_deserialize_any! { |
| 555 | bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq |
| 556 | bytes byte_buf map option unit newtype_struct |
| 557 | ignored_any unit_struct tuple_struct tuple enum identifier struct |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | struct ValueDeserializer<'a> { |
| 562 | value: Value<'a>, |
| 563 | validate_struct_keys: bool, |
| 564 | } |
| 565 | |
| 566 | impl<'a> ValueDeserializer<'a> { |
| 567 | fn new(value: Value<'a>) -> ValueDeserializer<'a> { |
| 568 | ValueDeserializer { |
| 569 | value, |
| 570 | validate_struct_keys: false, |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | fn with_struct_key_validation(mut self) -> Self { |
| 575 | self.validate_struct_keys = true; |
| 576 | self |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | impl<'de> de::Deserializer<'de> for ValueDeserializer<'de> { |
| 581 | type Error = Box<Error>; |
| 582 | |
| 583 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Box<Error>> |
| 584 | where |
| 585 | V: de::Visitor<'de>, |
| 586 | { |
| 587 | let start = self.value.start; |
| 588 | let res = match self.value.e { |
| 589 | E::Integer(i) => visitor.visit_i64(i), |
| 590 | E::Boolean(b) => visitor.visit_bool(b), |
| 591 | E::Float(f) => visitor.visit_f64(f), |
| 592 | E::String(Cow::Borrowed(s)) => visitor.visit_borrowed_str(s), |
| 593 | E::String(Cow::Owned(s)) => visitor.visit_string(s), |
| 594 | E::Array(values) => { |
| 595 | let mut s = de::value::SeqDeserializer::new(values.into_iter()); |
| 596 | let ret = visitor.visit_seq(&mut s)?; |
| 597 | s.end()?; |
| 598 | Ok(ret) |
| 599 | } |
| 600 | E::InlineTable(values) | E::DottedTable(values) => { |
| 601 | visitor.visit_map(InlineTableDeserializer { |
| 602 | values: values.into_iter(), |
| 603 | next_value: None, |
| 604 | keys: HashSet::new(), |
| 605 | }) |
| 606 | } |
| 607 | }; |
| 608 | res.map_err(|mut err| { |
| 609 | // Attribute the error to whatever value returned the error. |
| 610 | err.fix_offset(|| Some(start)); |
| 611 | err |
| 612 | }) |
| 613 | } |
| 614 | |
| 615 | fn deserialize_struct<V>( |
| 616 | self, |
| 617 | _name: &'static str, |
| 618 | fields: &'static [&'static str], |
| 619 | visitor: V, |
| 620 | ) -> Result<V::Value, Box<Error>> |
| 621 | where |
| 622 | V: de::Visitor<'de>, |
| 623 | { |
| 624 | if self.validate_struct_keys { |
| 625 | match self.value.e { |
| 626 | E::InlineTable(ref values) | E::DottedTable(ref values) => { |
| 627 | let extra_fields = values |
| 628 | .iter() |
| 629 | .filter_map(|key_value| { |
| 630 | let (ref key, ref _val) = *key_value; |
| 631 | if fields.contains(&&*(key.1)) { |
| 632 | None |
| 633 | } else { |
| 634 | Some(key.clone()) |
| 635 | } |
| 636 | }) |
| 637 | .collect::<Vec<_>>(); |
| 638 | |
| 639 | if !extra_fields.is_empty() { |
| 640 | return Err(Error::from_kind( |
| 641 | Some(self.value.start), |
| 642 | ErrorKind::UnexpectedKeys { |
| 643 | keys: extra_fields |
| 644 | .iter() |
| 645 | .map(|k| k.1.to_string()) |
| 646 | .collect::<Vec<_>>(), |
| 647 | available: fields, |
| 648 | }, |
| 649 | )); |
| 650 | } |
| 651 | } |
| 652 | _ => {} |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | self.deserialize_any(visitor) |
| 657 | } |
| 658 | |
| 659 | // `None` is interpreted as a missing field so be sure to implement `Some` |
| 660 | // as a present field. |
| 661 | fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Box<Error>> |
| 662 | where |
| 663 | V: de::Visitor<'de>, |
| 664 | { |
| 665 | visitor.visit_some(self) |
| 666 | } |
| 667 | |
| 668 | fn deserialize_enum<V>( |
| 669 | self, |
| 670 | _name: &'static str, |
| 671 | _variants: &'static [&'static str], |
| 672 | visitor: V, |
| 673 | ) -> Result<V::Value, Box<Error>> |
| 674 | where |
| 675 | V: de::Visitor<'de>, |
| 676 | { |
| 677 | match self.value.e { |
| 678 | E::String(val) => visitor.visit_enum(val.into_deserializer()), |
| 679 | e => Err(Error::from_kind( |
| 680 | Some(self.value.start), |
| 681 | ErrorKind::Wanted { |
| 682 | expected: "string" , |
| 683 | found: e.type_name(), |
| 684 | }, |
| 685 | )), |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | fn deserialize_newtype_struct<V>( |
| 690 | self, |
| 691 | _name: &'static str, |
| 692 | visitor: V, |
| 693 | ) -> Result<V::Value, Box<Error>> |
| 694 | where |
| 695 | V: de::Visitor<'de>, |
| 696 | { |
| 697 | visitor.visit_newtype_struct(self) |
| 698 | } |
| 699 | |
| 700 | serde::forward_to_deserialize_any! { |
| 701 | bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq |
| 702 | bytes byte_buf map unit identifier |
| 703 | ignored_any unit_struct tuple_struct tuple |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | impl<'de, 'b> de::IntoDeserializer<'de, Box<Error>> for MapVisitor<'de, 'b> { |
| 708 | type Deserializer = MapVisitor<'de, 'b>; |
| 709 | |
| 710 | fn into_deserializer(self) -> Self::Deserializer { |
| 711 | self |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | impl<'de, 'b> de::IntoDeserializer<'de, Box<Error>> for &'b mut Deserializer<'de> { |
| 716 | type Deserializer = Self; |
| 717 | |
| 718 | fn into_deserializer(self) -> Self::Deserializer { |
| 719 | self |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | impl<'de> de::IntoDeserializer<'de, Box<Error>> for Value<'de> { |
| 724 | type Deserializer = ValueDeserializer<'de>; |
| 725 | |
| 726 | fn into_deserializer(self) -> Self::Deserializer { |
| 727 | ValueDeserializer::new(self) |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | struct InlineTableDeserializer<'de> { |
| 732 | values: vec::IntoIter<TablePair<'de>>, |
| 733 | next_value: Option<Value<'de>>, |
| 734 | keys: HashSet<Cow<'de, str>>, |
| 735 | } |
| 736 | |
| 737 | impl<'de> de::MapAccess<'de> for InlineTableDeserializer<'de> { |
| 738 | type Error = Box<Error>; |
| 739 | |
| 740 | fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Box<Error>> |
| 741 | where |
| 742 | K: de::DeserializeSeed<'de>, |
| 743 | { |
| 744 | let ((span, key), value) = match self.values.next() { |
| 745 | Some(pair) => pair, |
| 746 | None => return Ok(None), |
| 747 | }; |
| 748 | self.next_value = Some(value); |
| 749 | if !self.keys.insert(key.clone()) { |
| 750 | return Err(Error::from_kind( |
| 751 | Some(span.start), |
| 752 | ErrorKind::DuplicateKey(key.into_owned()), |
| 753 | )); |
| 754 | } |
| 755 | seed.deserialize(StrDeserializer::new(key)).map(Some) |
| 756 | } |
| 757 | |
| 758 | fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Box<Error>> |
| 759 | where |
| 760 | V: de::DeserializeSeed<'de>, |
| 761 | { |
| 762 | let value = self.next_value.take().expect("Unable to read table values" ); |
| 763 | seed.deserialize(ValueDeserializer::new(value)) |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | impl<'de> de::EnumAccess<'de> for InlineTableDeserializer<'de> { |
| 768 | type Error = Box<Error>; |
| 769 | type Variant = TableEnumDeserializer<'de>; |
| 770 | |
| 771 | fn variant_seed<V>(mut self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error> |
| 772 | where |
| 773 | V: de::DeserializeSeed<'de>, |
| 774 | { |
| 775 | let (key, value) = match self.values.next() { |
| 776 | Some(pair) => pair, |
| 777 | None => { |
| 778 | return Err(Error::from_kind( |
| 779 | None, // FIXME: How do we get an offset here? |
| 780 | ErrorKind::Wanted { |
| 781 | expected: "table with exactly 1 entry" , |
| 782 | found: "empty table" , |
| 783 | }, |
| 784 | )); |
| 785 | } |
| 786 | }; |
| 787 | |
| 788 | seed.deserialize(StrDeserializer::new(key.1)) |
| 789 | .map(|val| (val, TableEnumDeserializer { value })) |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | /// Deserializes table values into enum variants. |
| 794 | struct TableEnumDeserializer<'a> { |
| 795 | value: Value<'a>, |
| 796 | } |
| 797 | |
| 798 | impl<'de> de::VariantAccess<'de> for TableEnumDeserializer<'de> { |
| 799 | type Error = Box<Error>; |
| 800 | |
| 801 | fn unit_variant(self) -> Result<(), Self::Error> { |
| 802 | match self.value.e { |
| 803 | E::InlineTable(values) | E::DottedTable(values) => { |
| 804 | if values.is_empty() { |
| 805 | Ok(()) |
| 806 | } else { |
| 807 | Err(Error::from_kind( |
| 808 | Some(self.value.start), |
| 809 | ErrorKind::ExpectedEmptyTable, |
| 810 | )) |
| 811 | } |
| 812 | } |
| 813 | e => Err(Error::from_kind( |
| 814 | Some(self.value.start), |
| 815 | ErrorKind::Wanted { |
| 816 | expected: "table" , |
| 817 | found: e.type_name(), |
| 818 | }, |
| 819 | )), |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error> |
| 824 | where |
| 825 | T: de::DeserializeSeed<'de>, |
| 826 | { |
| 827 | seed.deserialize(ValueDeserializer::new(self.value)) |
| 828 | } |
| 829 | |
| 830 | fn tuple_variant<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error> |
| 831 | where |
| 832 | V: de::Visitor<'de>, |
| 833 | { |
| 834 | match self.value.e { |
| 835 | E::InlineTable(values) | E::DottedTable(values) => { |
| 836 | let tuple_values = values |
| 837 | .into_iter() |
| 838 | .enumerate() |
| 839 | .map(|(index, (key, value))| match key.1.parse::<usize>() { |
| 840 | Ok(key_index) if key_index == index => Ok(value), |
| 841 | Ok(_) | Err(_) => Err(Error::from_kind( |
| 842 | Some(key.0.start), |
| 843 | ErrorKind::ExpectedTupleIndex { |
| 844 | expected: index, |
| 845 | found: key.1.to_string(), |
| 846 | }, |
| 847 | )), |
| 848 | }) |
| 849 | .collect::<Result<Vec<_>, _>>()?; |
| 850 | |
| 851 | if tuple_values.len() == len { |
| 852 | de::Deserializer::deserialize_seq( |
| 853 | ValueDeserializer::new(Value { |
| 854 | e: E::Array(tuple_values), |
| 855 | start: self.value.start, |
| 856 | end: self.value.end, |
| 857 | }), |
| 858 | visitor, |
| 859 | ) |
| 860 | } else { |
| 861 | Err(Error::from_kind( |
| 862 | Some(self.value.start), |
| 863 | ErrorKind::ExpectedTuple(len), |
| 864 | )) |
| 865 | } |
| 866 | } |
| 867 | e => Err(Error::from_kind( |
| 868 | Some(self.value.start), |
| 869 | ErrorKind::Wanted { |
| 870 | expected: "table" , |
| 871 | found: e.type_name(), |
| 872 | }, |
| 873 | )), |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | fn struct_variant<V>( |
| 878 | self, |
| 879 | fields: &'static [&'static str], |
| 880 | visitor: V, |
| 881 | ) -> Result<V::Value, Self::Error> |
| 882 | where |
| 883 | V: de::Visitor<'de>, |
| 884 | { |
| 885 | de::Deserializer::deserialize_struct( |
| 886 | ValueDeserializer::new(self.value).with_struct_key_validation(), |
| 887 | "" , // TODO: this should be the variant name |
| 888 | fields, |
| 889 | visitor, |
| 890 | ) |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | impl<'a> Deserializer<'a> { |
| 895 | fn new(input: &'a str) -> Deserializer<'a> { |
| 896 | Deserializer { |
| 897 | tokens: Tokenizer::new(input), |
| 898 | input, |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | fn tables(&mut self) -> Result<Vec<Table<'a>>, Box<Error>> { |
| 903 | let mut tables = Vec::new(); |
| 904 | let mut cur_table = Table { |
| 905 | at: 0, |
| 906 | header: Vec::new(), |
| 907 | values: None, |
| 908 | array: false, |
| 909 | }; |
| 910 | |
| 911 | while let Some(line) = self.line()? { |
| 912 | match line { |
| 913 | Line::Table { |
| 914 | at, |
| 915 | mut header, |
| 916 | array, |
| 917 | } => { |
| 918 | if !cur_table.header.is_empty() || cur_table.values.is_some() { |
| 919 | tables.push(cur_table); |
| 920 | } |
| 921 | cur_table = Table { |
| 922 | at, |
| 923 | header: Vec::new(), |
| 924 | values: Some(Vec::new()), |
| 925 | array, |
| 926 | }; |
| 927 | loop { |
| 928 | let part = header.next().map_err(|e| self.token_error(e)); |
| 929 | match part? { |
| 930 | Some(part) => cur_table.header.push(part), |
| 931 | None => break, |
| 932 | } |
| 933 | } |
| 934 | } |
| 935 | Line::KeyValue(key, value) => { |
| 936 | if cur_table.values.is_none() { |
| 937 | cur_table.values = Some(Vec::new()); |
| 938 | } |
| 939 | self.add_dotted_key(key, value, cur_table.values.as_mut().unwrap())?; |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | if !cur_table.header.is_empty() || cur_table.values.is_some() { |
| 944 | tables.push(cur_table); |
| 945 | } |
| 946 | Ok(tables) |
| 947 | } |
| 948 | |
| 949 | fn line(&mut self) -> Result<Option<Line<'a>>, Box<Error>> { |
| 950 | loop { |
| 951 | self.eat_whitespace(); |
| 952 | if self.eat_comment()? { |
| 953 | continue; |
| 954 | } |
| 955 | if self.eat(Token::Newline)? { |
| 956 | continue; |
| 957 | } |
| 958 | break; |
| 959 | } |
| 960 | |
| 961 | match self.peek()? { |
| 962 | Some((_, Token::LeftBracket)) => self.table_header().map(Some), |
| 963 | Some(_) => self.key_value().map(Some), |
| 964 | None => Ok(None), |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | fn table_header(&mut self) -> Result<Line<'a>, Box<Error>> { |
| 969 | let start = self.tokens.current(); |
| 970 | self.expect(Token::LeftBracket)?; |
| 971 | let array = self.eat(Token::LeftBracket)?; |
| 972 | let ret = Header::new(self.tokens.clone(), array); |
| 973 | self.tokens.skip_to_newline(); |
| 974 | Ok(Line::Table { |
| 975 | at: start, |
| 976 | header: ret, |
| 977 | array, |
| 978 | }) |
| 979 | } |
| 980 | |
| 981 | fn key_value(&mut self) -> Result<Line<'a>, Box<Error>> { |
| 982 | let key = self.dotted_key()?; |
| 983 | self.eat_whitespace(); |
| 984 | self.expect(Token::Equals)?; |
| 985 | self.eat_whitespace(); |
| 986 | |
| 987 | let value = self.value()?; |
| 988 | self.eat_whitespace(); |
| 989 | if !self.eat_comment()? { |
| 990 | self.eat_newline_or_eof()?; |
| 991 | } |
| 992 | |
| 993 | Ok(Line::KeyValue(key, value)) |
| 994 | } |
| 995 | |
| 996 | fn value(&mut self) -> Result<Value<'a>, Box<Error>> { |
| 997 | let at = self.tokens.current(); |
| 998 | let value = match self.next()? { |
| 999 | Some((Span { start, end }, Token::String { val, .. })) => Value { |
| 1000 | e: E::String(val), |
| 1001 | start, |
| 1002 | end, |
| 1003 | }, |
| 1004 | Some((Span { start, end }, Token::Keylike("true" ))) => Value { |
| 1005 | e: E::Boolean(true), |
| 1006 | start, |
| 1007 | end, |
| 1008 | }, |
| 1009 | Some((Span { start, end }, Token::Keylike("false" ))) => Value { |
| 1010 | e: E::Boolean(false), |
| 1011 | start, |
| 1012 | end, |
| 1013 | }, |
| 1014 | Some((span, Token::Keylike(key))) => self.parse_keylike(at, span, key)?, |
| 1015 | Some((span, Token::Plus)) => self.number_leading_plus(span)?, |
| 1016 | Some((Span { start, .. }, Token::LeftBrace)) => { |
| 1017 | self.inline_table().map(|(Span { end, .. }, table)| Value { |
| 1018 | e: E::InlineTable(table), |
| 1019 | start, |
| 1020 | end, |
| 1021 | })? |
| 1022 | } |
| 1023 | Some((Span { start, .. }, Token::LeftBracket)) => { |
| 1024 | self.array().map(|(Span { end, .. }, array)| Value { |
| 1025 | e: E::Array(array), |
| 1026 | start, |
| 1027 | end, |
| 1028 | })? |
| 1029 | } |
| 1030 | Some(token) => { |
| 1031 | return Err(self.error( |
| 1032 | at, |
| 1033 | ErrorKind::Wanted { |
| 1034 | expected: "a value" , |
| 1035 | found: token.1.describe(), |
| 1036 | }, |
| 1037 | )); |
| 1038 | } |
| 1039 | None => return Err(self.eof()), |
| 1040 | }; |
| 1041 | Ok(value) |
| 1042 | } |
| 1043 | |
| 1044 | fn parse_keylike( |
| 1045 | &mut self, |
| 1046 | at: usize, |
| 1047 | span: Span, |
| 1048 | key: &'a str, |
| 1049 | ) -> Result<Value<'a>, Box<Error>> { |
| 1050 | if key == "inf" || key == "nan" { |
| 1051 | return self.number(span, key); |
| 1052 | } |
| 1053 | |
| 1054 | let first_char = key.chars().next().expect("key should not be empty here" ); |
| 1055 | match first_char { |
| 1056 | '-' | '0' ..='9' => self.number(span, key), |
| 1057 | _ => Err(self.error(at, ErrorKind::UnquotedString)), |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | fn number(&mut self, Span { start, end }: Span, s: &'a str) -> Result<Value<'a>, Box<Error>> { |
| 1062 | let to_integer = |f| Value { |
| 1063 | e: E::Integer(f), |
| 1064 | start, |
| 1065 | end, |
| 1066 | }; |
| 1067 | if let Some(s) = s.strip_prefix("0x" ) { |
| 1068 | self.integer(s, 16).map(to_integer) |
| 1069 | } else if let Some(s) = s.strip_prefix("0o" ) { |
| 1070 | self.integer(s, 8).map(to_integer) |
| 1071 | } else if let Some(s) = s.strip_prefix("0b" ) { |
| 1072 | self.integer(s, 2).map(to_integer) |
| 1073 | } else if s.contains('e' ) || s.contains('E' ) { |
| 1074 | self.float(s, None).map(|f| Value { |
| 1075 | e: E::Float(f), |
| 1076 | start, |
| 1077 | end, |
| 1078 | }) |
| 1079 | } else if self.eat(Token::Period)? { |
| 1080 | let at = self.tokens.current(); |
| 1081 | match self.next()? { |
| 1082 | Some((Span { start, end }, Token::Keylike(after))) => { |
| 1083 | self.float(s, Some(after)).map(|f| Value { |
| 1084 | e: E::Float(f), |
| 1085 | start, |
| 1086 | end, |
| 1087 | }) |
| 1088 | } |
| 1089 | _ => Err(self.error(at, ErrorKind::NumberInvalid)), |
| 1090 | } |
| 1091 | } else if s == "inf" { |
| 1092 | Ok(Value { |
| 1093 | e: E::Float(f64::INFINITY), |
| 1094 | start, |
| 1095 | end, |
| 1096 | }) |
| 1097 | } else if s == "-inf" { |
| 1098 | Ok(Value { |
| 1099 | e: E::Float(f64::NEG_INFINITY), |
| 1100 | start, |
| 1101 | end, |
| 1102 | }) |
| 1103 | } else if s == "nan" { |
| 1104 | Ok(Value { |
| 1105 | e: E::Float(f64::NAN.copysign(1.0)), |
| 1106 | start, |
| 1107 | end, |
| 1108 | }) |
| 1109 | } else if s == "-nan" { |
| 1110 | Ok(Value { |
| 1111 | e: E::Float(f64::NAN.copysign(-1.0)), |
| 1112 | start, |
| 1113 | end, |
| 1114 | }) |
| 1115 | } else { |
| 1116 | self.integer(s, 10).map(to_integer) |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | fn number_leading_plus(&mut self, Span { start, .. }: Span) -> Result<Value<'a>, Box<Error>> { |
| 1121 | let start_token = self.tokens.current(); |
| 1122 | match self.next()? { |
| 1123 | Some((Span { end, .. }, Token::Keylike(s))) => self.number(Span { start, end }, s), |
| 1124 | _ => Err(self.error(start_token, ErrorKind::NumberInvalid)), |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | fn integer(&self, s: &'a str, radix: u32) -> Result<i64, Box<Error>> { |
| 1129 | let allow_sign = radix == 10; |
| 1130 | let allow_leading_zeros = radix != 10; |
| 1131 | let (prefix, suffix) = self.parse_integer(s, allow_sign, allow_leading_zeros, radix)?; |
| 1132 | let start = self.tokens.substr_offset(s); |
| 1133 | if !suffix.is_empty() { |
| 1134 | return Err(self.error(start, ErrorKind::NumberInvalid)); |
| 1135 | } |
| 1136 | i64::from_str_radix(prefix.replace('_' , "" ).trim_start_matches('+' ), radix) |
| 1137 | .map_err(|_e| self.error(start, ErrorKind::NumberInvalid)) |
| 1138 | } |
| 1139 | |
| 1140 | fn parse_integer( |
| 1141 | &self, |
| 1142 | s: &'a str, |
| 1143 | allow_sign: bool, |
| 1144 | allow_leading_zeros: bool, |
| 1145 | radix: u32, |
| 1146 | ) -> Result<(&'a str, &'a str), Box<Error>> { |
| 1147 | let start = self.tokens.substr_offset(s); |
| 1148 | |
| 1149 | let mut first = true; |
| 1150 | let mut first_zero = false; |
| 1151 | let mut underscore = false; |
| 1152 | let mut end = s.len(); |
| 1153 | for (i, c) in s.char_indices() { |
| 1154 | let at = i + start; |
| 1155 | if i == 0 && (c == '+' || c == '-' ) && allow_sign { |
| 1156 | continue; |
| 1157 | } |
| 1158 | |
| 1159 | if c == '0' && first { |
| 1160 | first_zero = true; |
| 1161 | } else if c.is_digit(radix) { |
| 1162 | if !first && first_zero && !allow_leading_zeros { |
| 1163 | return Err(self.error(at, ErrorKind::NumberInvalid)); |
| 1164 | } |
| 1165 | underscore = false; |
| 1166 | } else if c == '_' && first { |
| 1167 | return Err(self.error(at, ErrorKind::NumberInvalid)); |
| 1168 | } else if c == '_' && !underscore { |
| 1169 | underscore = true; |
| 1170 | } else { |
| 1171 | end = i; |
| 1172 | break; |
| 1173 | } |
| 1174 | first = false; |
| 1175 | } |
| 1176 | if first || underscore { |
| 1177 | return Err(self.error(start, ErrorKind::NumberInvalid)); |
| 1178 | } |
| 1179 | Ok((&s[..end], &s[end..])) |
| 1180 | } |
| 1181 | |
| 1182 | fn float(&mut self, s: &'a str, after_decimal: Option<&'a str>) -> Result<f64, Box<Error>> { |
| 1183 | let (integral, mut suffix) = self.parse_integer(s, true, false, 10)?; |
| 1184 | let start = self.tokens.substr_offset(integral); |
| 1185 | |
| 1186 | let mut fraction = None; |
| 1187 | if let Some(after) = after_decimal { |
| 1188 | if !suffix.is_empty() { |
| 1189 | return Err(self.error(start, ErrorKind::NumberInvalid)); |
| 1190 | } |
| 1191 | let (a, b) = self.parse_integer(after, false, true, 10)?; |
| 1192 | fraction = Some(a); |
| 1193 | suffix = b; |
| 1194 | } |
| 1195 | |
| 1196 | let mut exponent = None; |
| 1197 | if suffix.starts_with('e' ) || suffix.starts_with('E' ) { |
| 1198 | let (a, b) = if suffix.len() == 1 { |
| 1199 | self.eat(Token::Plus)?; |
| 1200 | match self.next()? { |
| 1201 | Some((_, Token::Keylike(s))) => self.parse_integer(s, false, true, 10)?, |
| 1202 | _ => return Err(self.error(start, ErrorKind::NumberInvalid)), |
| 1203 | } |
| 1204 | } else { |
| 1205 | self.parse_integer(&suffix[1..], true, true, 10)? |
| 1206 | }; |
| 1207 | if !b.is_empty() { |
| 1208 | return Err(self.error(start, ErrorKind::NumberInvalid)); |
| 1209 | } |
| 1210 | exponent = Some(a); |
| 1211 | } else if !suffix.is_empty() { |
| 1212 | return Err(self.error(start, ErrorKind::NumberInvalid)); |
| 1213 | } |
| 1214 | |
| 1215 | let mut number = integral |
| 1216 | .trim_start_matches('+' ) |
| 1217 | .chars() |
| 1218 | .filter(|c| *c != '_' ) |
| 1219 | .collect::<String>(); |
| 1220 | if let Some(fraction) = fraction { |
| 1221 | number.push('.' ); |
| 1222 | number.extend(fraction.chars().filter(|c| *c != '_' )); |
| 1223 | } |
| 1224 | if let Some(exponent) = exponent { |
| 1225 | number.push('E' ); |
| 1226 | number.extend(exponent.chars().filter(|c| *c != '_' )); |
| 1227 | } |
| 1228 | number |
| 1229 | .parse() |
| 1230 | .map_err(|_e| self.error(start, ErrorKind::NumberInvalid)) |
| 1231 | .and_then(|n: f64| { |
| 1232 | if n.is_finite() { |
| 1233 | Ok(n) |
| 1234 | } else { |
| 1235 | Err(self.error(start, ErrorKind::NumberInvalid)) |
| 1236 | } |
| 1237 | }) |
| 1238 | } |
| 1239 | |
| 1240 | // TODO(#140): shouldn't buffer up this entire table in memory, it'd be |
| 1241 | // great to defer parsing everything until later. |
| 1242 | fn inline_table(&mut self) -> Result<(Span, Vec<TablePair<'a>>), Box<Error>> { |
| 1243 | let mut ret = Vec::new(); |
| 1244 | self.eat_whitespace(); |
| 1245 | if let Some(span) = self.eat_spanned(Token::RightBrace)? { |
| 1246 | return Ok((span, ret)); |
| 1247 | } |
| 1248 | loop { |
| 1249 | let key = self.dotted_key()?; |
| 1250 | self.eat_whitespace(); |
| 1251 | self.expect(Token::Equals)?; |
| 1252 | self.eat_whitespace(); |
| 1253 | let value = self.value()?; |
| 1254 | self.add_dotted_key(key, value, &mut ret)?; |
| 1255 | |
| 1256 | self.eat_whitespace(); |
| 1257 | if let Some(span) = self.eat_spanned(Token::RightBrace)? { |
| 1258 | return Ok((span, ret)); |
| 1259 | } |
| 1260 | self.expect(Token::Comma)?; |
| 1261 | self.eat_whitespace(); |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | // TODO(#140): shouldn't buffer up this entire array in memory, it'd be |
| 1266 | // great to defer parsing everything until later. |
| 1267 | fn array(&mut self) -> Result<(Span, Vec<Value<'a>>), Box<Error>> { |
| 1268 | let mut ret = Vec::new(); |
| 1269 | |
| 1270 | let intermediate = |me: &mut Deserializer| -> Result<(), Box<Error>> { |
| 1271 | loop { |
| 1272 | me.eat_whitespace(); |
| 1273 | if !me.eat(Token::Newline)? && !me.eat_comment()? { |
| 1274 | break; |
| 1275 | } |
| 1276 | } |
| 1277 | Ok(()) |
| 1278 | }; |
| 1279 | |
| 1280 | loop { |
| 1281 | intermediate(self)?; |
| 1282 | if let Some(span) = self.eat_spanned(Token::RightBracket)? { |
| 1283 | return Ok((span, ret)); |
| 1284 | } |
| 1285 | let value = self.value()?; |
| 1286 | ret.push(value); |
| 1287 | intermediate(self)?; |
| 1288 | if !self.eat(Token::Comma)? { |
| 1289 | break; |
| 1290 | } |
| 1291 | } |
| 1292 | intermediate(self)?; |
| 1293 | let span = self.expect_spanned(Token::RightBracket)?; |
| 1294 | Ok((span, ret)) |
| 1295 | } |
| 1296 | |
| 1297 | fn table_key(&mut self) -> Result<(Span, Cow<'a, str>), Box<Error>> { |
| 1298 | self.tokens.table_key().map_err(|e| self.token_error(e)) |
| 1299 | } |
| 1300 | |
| 1301 | fn dotted_key(&mut self) -> Result<Vec<(Span, Cow<'a, str>)>, Box<Error>> { |
| 1302 | let mut result = Vec::new(); |
| 1303 | result.push(self.table_key()?); |
| 1304 | self.eat_whitespace(); |
| 1305 | while self.eat(Token::Period)? { |
| 1306 | self.eat_whitespace(); |
| 1307 | result.push(self.table_key()?); |
| 1308 | self.eat_whitespace(); |
| 1309 | } |
| 1310 | Ok(result) |
| 1311 | } |
| 1312 | |
| 1313 | /// Stores a value in the appropriate hierarchical structure positioned based on the dotted key. |
| 1314 | /// |
| 1315 | /// Given the following definition: `multi.part.key = "value"`, `multi` and `part` are |
| 1316 | /// intermediate parts which are mapped to the relevant fields in the deserialized type's data |
| 1317 | /// hierarchy. |
| 1318 | /// |
| 1319 | /// # Parameters |
| 1320 | /// |
| 1321 | /// * `key_parts`: Each segment of the dotted key, e.g. `part.one` maps to |
| 1322 | /// `vec![Cow::Borrowed("part"), Cow::Borrowed("one")].` |
| 1323 | /// * `value`: The parsed value. |
| 1324 | /// * `values`: The `Vec` to store the value in. |
| 1325 | fn add_dotted_key( |
| 1326 | &self, |
| 1327 | mut key_parts: Vec<(Span, Cow<'a, str>)>, |
| 1328 | value: Value<'a>, |
| 1329 | values: &mut Vec<TablePair<'a>>, |
| 1330 | ) -> Result<(), Box<Error>> { |
| 1331 | let key = key_parts.remove(0); |
| 1332 | if key_parts.is_empty() { |
| 1333 | values.push((key, value)); |
| 1334 | return Ok(()); |
| 1335 | } |
| 1336 | match values.iter_mut().find(|&&mut (ref k, _)| *k.1 == key.1) { |
| 1337 | Some(&mut ( |
| 1338 | _, |
| 1339 | Value { |
| 1340 | e: E::DottedTable(ref mut v), |
| 1341 | .. |
| 1342 | }, |
| 1343 | )) => { |
| 1344 | return self.add_dotted_key(key_parts, value, v); |
| 1345 | } |
| 1346 | Some(&mut (_, Value { start, .. })) => { |
| 1347 | return Err(self.error(start, ErrorKind::DottedKeyInvalidType)); |
| 1348 | } |
| 1349 | None => {} |
| 1350 | } |
| 1351 | // The start/end value is somewhat misleading here. |
| 1352 | let table_values = Value { |
| 1353 | e: E::DottedTable(Vec::new()), |
| 1354 | start: value.start, |
| 1355 | end: value.end, |
| 1356 | }; |
| 1357 | values.push((key, table_values)); |
| 1358 | let last_i = values.len() - 1; |
| 1359 | if let ( |
| 1360 | _, |
| 1361 | Value { |
| 1362 | e: E::DottedTable(ref mut v), |
| 1363 | .. |
| 1364 | }, |
| 1365 | ) = values[last_i] |
| 1366 | { |
| 1367 | self.add_dotted_key(key_parts, value, v)?; |
| 1368 | } |
| 1369 | Ok(()) |
| 1370 | } |
| 1371 | |
| 1372 | fn eat_whitespace(&mut self) { |
| 1373 | self.tokens.eat_whitespace(); |
| 1374 | } |
| 1375 | |
| 1376 | fn eat_comment(&mut self) -> Result<bool, Box<Error>> { |
| 1377 | self.tokens.eat_comment().map_err(|e| self.token_error(e)) |
| 1378 | } |
| 1379 | |
| 1380 | fn eat_newline_or_eof(&mut self) -> Result<(), Box<Error>> { |
| 1381 | self.tokens |
| 1382 | .eat_newline_or_eof() |
| 1383 | .map_err(|e| self.token_error(e)) |
| 1384 | } |
| 1385 | |
| 1386 | fn eat(&mut self, expected: Token<'a>) -> Result<bool, Box<Error>> { |
| 1387 | self.tokens.eat(expected).map_err(|e| self.token_error(e)) |
| 1388 | } |
| 1389 | |
| 1390 | fn eat_spanned(&mut self, expected: Token<'a>) -> Result<Option<Span>, Box<Error>> { |
| 1391 | self.tokens |
| 1392 | .eat_spanned(expected) |
| 1393 | .map_err(|e| self.token_error(e)) |
| 1394 | } |
| 1395 | |
| 1396 | fn expect(&mut self, expected: Token<'a>) -> Result<(), Box<Error>> { |
| 1397 | self.tokens |
| 1398 | .expect(expected) |
| 1399 | .map_err(|e| self.token_error(e)) |
| 1400 | } |
| 1401 | |
| 1402 | fn expect_spanned(&mut self, expected: Token<'a>) -> Result<Span, Box<Error>> { |
| 1403 | self.tokens |
| 1404 | .expect_spanned(expected) |
| 1405 | .map_err(|e| self.token_error(e)) |
| 1406 | } |
| 1407 | |
| 1408 | fn next(&mut self) -> Result<Option<(Span, Token<'a>)>, Box<Error>> { |
| 1409 | self.tokens.next().map_err(|e| self.token_error(e)) |
| 1410 | } |
| 1411 | |
| 1412 | fn peek(&mut self) -> Result<Option<(Span, Token<'a>)>, Box<Error>> { |
| 1413 | self.tokens.peek().map_err(|e| self.token_error(e)) |
| 1414 | } |
| 1415 | |
| 1416 | fn eof(&self) -> Box<Error> { |
| 1417 | self.error(self.input.len(), ErrorKind::UnexpectedEof) |
| 1418 | } |
| 1419 | |
| 1420 | fn token_error(&self, error: TokenError) -> Box<Error> { |
| 1421 | match error { |
| 1422 | TokenError::InvalidCharInString(at, ch) => { |
| 1423 | self.error(at, ErrorKind::InvalidCharInString(ch)) |
| 1424 | } |
| 1425 | TokenError::InvalidEscape(at, ch) => self.error(at, ErrorKind::InvalidEscape(ch)), |
| 1426 | TokenError::InvalidEscapeValue(at, v) => { |
| 1427 | self.error(at, ErrorKind::InvalidEscapeValue(v)) |
| 1428 | } |
| 1429 | TokenError::InvalidHexEscape(at, ch) => self.error(at, ErrorKind::InvalidHexEscape(ch)), |
| 1430 | TokenError::NewlineInString(at) => self.error(at, ErrorKind::NewlineInString), |
| 1431 | TokenError::Unexpected(at, ch) => self.error(at, ErrorKind::Unexpected(ch)), |
| 1432 | TokenError::UnterminatedString(at) => self.error(at, ErrorKind::UnterminatedString), |
| 1433 | TokenError::NewlineInTableKey(at) => self.error(at, ErrorKind::NewlineInTableKey), |
| 1434 | TokenError::Wanted { |
| 1435 | at, |
| 1436 | expected, |
| 1437 | found, |
| 1438 | } => self.error(at, ErrorKind::Wanted { expected, found }), |
| 1439 | TokenError::MultilineStringKey(at) => self.error(at, ErrorKind::MultilineStringKey), |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | fn error(&self, at: usize, kind: ErrorKind) -> Box<Error> { |
| 1444 | let mut err = Error::from_kind(Some(at), kind); |
| 1445 | err.fix_linecol(|at| self.to_linecol(at)); |
| 1446 | err |
| 1447 | } |
| 1448 | |
| 1449 | /// Converts a byte offset from an error message to a (line, column) pair |
| 1450 | /// |
| 1451 | /// All indexes are 0-based. |
| 1452 | fn to_linecol(&self, offset: usize) -> (usize, usize) { |
| 1453 | let mut cur = 0; |
| 1454 | // Use split_terminator instead of lines so that if there is a `\r`, it |
| 1455 | // is included in the offset calculation. The `+1` values below account |
| 1456 | // for the `\n`. |
| 1457 | for (i, line) in self.input.split_terminator(' \n' ).enumerate() { |
| 1458 | if cur + line.len() + 1 > offset { |
| 1459 | return (i, offset - cur); |
| 1460 | } |
| 1461 | cur += line.len() + 1; |
| 1462 | } |
| 1463 | (self.input.lines().count(), 0) |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | impl Error { |
| 1468 | pub(crate) fn line_col(&self) -> Option<(usize, usize)> { |
| 1469 | self.line.map(|line| (line, self.col)) |
| 1470 | } |
| 1471 | |
| 1472 | fn from_kind(at: Option<usize>, kind: ErrorKind) -> Box<Self> { |
| 1473 | Box::new(Error { |
| 1474 | kind, |
| 1475 | line: None, |
| 1476 | col: 0, |
| 1477 | at, |
| 1478 | message: String::new(), |
| 1479 | key: Vec::new(), |
| 1480 | }) |
| 1481 | } |
| 1482 | |
| 1483 | fn custom(at: Option<usize>, s: String) -> Box<Self> { |
| 1484 | Box::new(Error { |
| 1485 | kind: ErrorKind::Custom, |
| 1486 | line: None, |
| 1487 | col: 0, |
| 1488 | at, |
| 1489 | message: s, |
| 1490 | key: Vec::new(), |
| 1491 | }) |
| 1492 | } |
| 1493 | |
| 1494 | pub(crate) fn add_key_context(&mut self, key: &str) { |
| 1495 | self.key.insert(0, key.to_string()); |
| 1496 | } |
| 1497 | |
| 1498 | fn fix_offset<F>(&mut self, f: F) |
| 1499 | where |
| 1500 | F: FnOnce() -> Option<usize>, |
| 1501 | { |
| 1502 | // An existing offset is always better positioned than anything we might |
| 1503 | // want to add later. |
| 1504 | if self.at.is_none() { |
| 1505 | self.at = f(); |
| 1506 | } |
| 1507 | } |
| 1508 | |
| 1509 | fn fix_linecol<F>(&mut self, f: F) |
| 1510 | where |
| 1511 | F: FnOnce(usize) -> (usize, usize), |
| 1512 | { |
| 1513 | if let Some(at) = self.at { |
| 1514 | let (line, col) = f(at); |
| 1515 | self.line = Some(line); |
| 1516 | self.col = col; |
| 1517 | } |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | impl std::convert::From<Error> for std::io::Error { |
| 1522 | fn from(e: Error) -> Self { |
| 1523 | std::io::Error::new(kind:std::io::ErrorKind::InvalidData, error:e.to_string()) |
| 1524 | } |
| 1525 | } |
| 1526 | |
| 1527 | impl Display for Error { |
| 1528 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 1529 | match &self.kind { |
| 1530 | ErrorKind::UnexpectedEof => "unexpected eof encountered" .fmt(f)?, |
| 1531 | ErrorKind::InvalidCharInString(c) => write!( |
| 1532 | f, |
| 1533 | "invalid character in string: ` {}`" , |
| 1534 | c.escape_default().collect::<String>() |
| 1535 | )?, |
| 1536 | ErrorKind::InvalidEscape(c) => write!( |
| 1537 | f, |
| 1538 | "invalid escape character in string: ` {}`" , |
| 1539 | c.escape_default().collect::<String>() |
| 1540 | )?, |
| 1541 | ErrorKind::InvalidHexEscape(c) => write!( |
| 1542 | f, |
| 1543 | "invalid hex escape character in string: ` {}`" , |
| 1544 | c.escape_default().collect::<String>() |
| 1545 | )?, |
| 1546 | ErrorKind::InvalidEscapeValue(c) => write!(f, "invalid escape value: ` {}`" , c)?, |
| 1547 | ErrorKind::NewlineInString => "newline in string found" .fmt(f)?, |
| 1548 | ErrorKind::Unexpected(ch) => write!( |
| 1549 | f, |
| 1550 | "unexpected character found: ` {}`" , |
| 1551 | ch.escape_default().collect::<String>() |
| 1552 | )?, |
| 1553 | ErrorKind::UnterminatedString => "unterminated string" .fmt(f)?, |
| 1554 | ErrorKind::NewlineInTableKey => "found newline in table key" .fmt(f)?, |
| 1555 | ErrorKind::Wanted { expected, found } => { |
| 1556 | write!(f, "expected {}, found {}" , expected, found)?; |
| 1557 | } |
| 1558 | ErrorKind::NumberInvalid => "invalid number" .fmt(f)?, |
| 1559 | ErrorKind::DuplicateTable(ref s) => { |
| 1560 | write!(f, "redefinition of table ` {}`" , s)?; |
| 1561 | } |
| 1562 | ErrorKind::DuplicateKey(ref s) => { |
| 1563 | write!(f, "duplicate key: ` {}`" , s)?; |
| 1564 | } |
| 1565 | ErrorKind::RedefineAsArray => "table redefined as array" .fmt(f)?, |
| 1566 | ErrorKind::MultilineStringKey => "multiline strings are not allowed for key" .fmt(f)?, |
| 1567 | ErrorKind::Custom => self.message.fmt(f)?, |
| 1568 | ErrorKind::ExpectedTuple(l) => write!(f, "expected table with length {}" , l)?, |
| 1569 | ErrorKind::ExpectedTupleIndex { |
| 1570 | expected, |
| 1571 | ref found, |
| 1572 | } => write!(f, "expected table key ` {}`, but was ` {}`" , expected, found)?, |
| 1573 | ErrorKind::ExpectedEmptyTable => "expected empty table" .fmt(f)?, |
| 1574 | ErrorKind::DottedKeyInvalidType => { |
| 1575 | "dotted key attempted to extend non-table type" .fmt(f)?; |
| 1576 | } |
| 1577 | ErrorKind::UnexpectedKeys { |
| 1578 | ref keys, |
| 1579 | available, |
| 1580 | } => write!( |
| 1581 | f, |
| 1582 | "unexpected keys in table: ` {:?}`, available keys: ` {:?}`" , |
| 1583 | keys, available |
| 1584 | )?, |
| 1585 | ErrorKind::UnquotedString => write!( |
| 1586 | f, |
| 1587 | "invalid TOML value, did you mean to use a quoted string?" |
| 1588 | )?, |
| 1589 | } |
| 1590 | |
| 1591 | if !self.key.is_empty() { |
| 1592 | write!(f, " for key `" )?; |
| 1593 | for (i, k) in self.key.iter().enumerate() { |
| 1594 | if i > 0 { |
| 1595 | write!(f, "." )?; |
| 1596 | } |
| 1597 | write!(f, " {}" , k)?; |
| 1598 | } |
| 1599 | write!(f, "`" )?; |
| 1600 | } |
| 1601 | |
| 1602 | if let Some(line) = self.line { |
| 1603 | write!(f, " at line {} column {}" , line + 1, self.col + 1)?; |
| 1604 | } |
| 1605 | |
| 1606 | Ok(()) |
| 1607 | } |
| 1608 | } |
| 1609 | |
| 1610 | impl error::Error for Error {} |
| 1611 | |
| 1612 | impl de::Error for Box<Error> { |
| 1613 | fn custom<T: Display>(msg: T) -> Self { |
| 1614 | Error::custom(at:None, s:msg.to_string()) |
| 1615 | } |
| 1616 | } |
| 1617 | |
| 1618 | enum Line<'a> { |
| 1619 | Table { |
| 1620 | at: usize, |
| 1621 | header: Header<'a>, |
| 1622 | array: bool, |
| 1623 | }, |
| 1624 | KeyValue(Vec<(Span, Cow<'a, str>)>, Value<'a>), |
| 1625 | } |
| 1626 | |
| 1627 | struct Header<'a> { |
| 1628 | first: bool, |
| 1629 | array: bool, |
| 1630 | tokens: Tokenizer<'a>, |
| 1631 | } |
| 1632 | |
| 1633 | impl<'a> Header<'a> { |
| 1634 | fn new(tokens: Tokenizer<'a>, array: bool) -> Header<'a> { |
| 1635 | Header { |
| 1636 | first: true, |
| 1637 | array, |
| 1638 | tokens, |
| 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | fn next(&mut self) -> Result<Option<(Span, Cow<'a, str>)>, TokenError> { |
| 1643 | self.tokens.eat_whitespace(); |
| 1644 | |
| 1645 | if self.first || self.tokens.eat(Token::Period)? { |
| 1646 | self.first = false; |
| 1647 | self.tokens.eat_whitespace(); |
| 1648 | self.tokens.table_key().map(Some) |
| 1649 | } else { |
| 1650 | self.tokens.expect(Token::RightBracket)?; |
| 1651 | if self.array { |
| 1652 | self.tokens.expect(Token::RightBracket)?; |
| 1653 | } |
| 1654 | |
| 1655 | self.tokens.eat_whitespace(); |
| 1656 | if !self.tokens.eat_comment()? { |
| 1657 | self.tokens.eat_newline_or_eof()?; |
| 1658 | } |
| 1659 | Ok(None) |
| 1660 | } |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | #[derive (Debug)] |
| 1665 | struct Value<'a> { |
| 1666 | e: E<'a>, |
| 1667 | start: usize, |
| 1668 | end: usize, |
| 1669 | } |
| 1670 | |
| 1671 | #[derive (Debug)] |
| 1672 | enum E<'a> { |
| 1673 | Integer(i64), |
| 1674 | Float(f64), |
| 1675 | Boolean(bool), |
| 1676 | String(Cow<'a, str>), |
| 1677 | Array(Vec<Value<'a>>), |
| 1678 | InlineTable(Vec<TablePair<'a>>), |
| 1679 | DottedTable(Vec<TablePair<'a>>), |
| 1680 | } |
| 1681 | |
| 1682 | impl<'a> E<'a> { |
| 1683 | fn type_name(&self) -> &'static str { |
| 1684 | match *self { |
| 1685 | E::String(..) => "string" , |
| 1686 | E::Integer(..) => "integer" , |
| 1687 | E::Float(..) => "float" , |
| 1688 | E::Boolean(..) => "boolean" , |
| 1689 | E::Array(..) => "array" , |
| 1690 | E::InlineTable(..) => "inline table" , |
| 1691 | E::DottedTable(..) => "dotted table" , |
| 1692 | } |
| 1693 | } |
| 1694 | } |
| 1695 | |