1 | //! Deserializing TOML into Rust structures. |
2 | //! |
3 | //! This module contains all the Serde support for deserializing TOML documents |
4 | //! into Rust structures. Note that some top-level functions here are also |
5 | //! provided at the top of the crate. |
6 | |
7 | use std::borrow::Cow; |
8 | use std::collections::HashMap; |
9 | use std::error; |
10 | use std::f64; |
11 | use std::fmt; |
12 | use std::iter; |
13 | use std::marker::PhantomData; |
14 | use std::str; |
15 | use std::vec; |
16 | |
17 | use serde::de; |
18 | use serde::de::value::BorrowedStrDeserializer; |
19 | use serde::de::IntoDeserializer; |
20 | |
21 | use crate::datetime; |
22 | use crate::spanned; |
23 | use crate::tokens::{Error as TokenError, Span, Token, Tokenizer}; |
24 | |
25 | /// Type Alias for a TOML Table pair |
26 | type TablePair<'a> = ((Span, Cow<'a, str>), Value<'a>); |
27 | |
28 | /// Deserializes a byte slice into a type. |
29 | /// |
30 | /// This function will attempt to interpret `bytes` as UTF-8 data and then |
31 | /// deserialize `T` from the TOML document provided. |
32 | pub fn from_slice<'de, T>(bytes: &'de [u8]) -> Result<T, Error> |
33 | where |
34 | T: de::Deserialize<'de>, |
35 | { |
36 | match str::from_utf8(bytes) { |
37 | Ok(s: &str) => from_str(s), |
38 | Err(e: Utf8Error) => Err(Error::custom(at:None, s:e.to_string())), |
39 | } |
40 | } |
41 | |
42 | /// Deserializes a string into a type. |
43 | /// |
44 | /// This function will attempt to interpret `s` as a TOML document and |
45 | /// deserialize `T` from the document. |
46 | /// |
47 | /// # Examples |
48 | /// |
49 | /// ``` |
50 | /// use serde_derive::Deserialize; |
51 | /// |
52 | /// #[derive(Deserialize)] |
53 | /// struct Config { |
54 | /// title: String, |
55 | /// owner: Owner, |
56 | /// } |
57 | /// |
58 | /// #[derive(Deserialize)] |
59 | /// struct Owner { |
60 | /// name: String, |
61 | /// } |
62 | /// |
63 | /// let config: Config = toml::from_str(r#" |
64 | /// title = 'TOML Example' |
65 | /// |
66 | /// [owner] |
67 | /// name = 'Lisa' |
68 | /// "# ).unwrap(); |
69 | /// |
70 | /// assert_eq!(config.title, "TOML Example" ); |
71 | /// assert_eq!(config.owner.name, "Lisa" ); |
72 | /// ``` |
73 | pub fn from_str<'de, T>(s: &'de str) -> Result<T, Error> |
74 | where |
75 | T: de::Deserialize<'de>, |
76 | { |
77 | let mut d: Deserializer<'_> = Deserializer::new(input:s); |
78 | let ret: T = T::deserialize(&mut d)?; |
79 | d.end()?; |
80 | Ok(ret) |
81 | } |
82 | |
83 | /// Errors that can occur when deserializing a type. |
84 | #[derive (Debug, PartialEq, Eq, Clone)] |
85 | pub struct Error { |
86 | inner: Box<ErrorInner>, |
87 | } |
88 | |
89 | #[derive (Debug, PartialEq, Eq, Clone)] |
90 | struct ErrorInner { |
91 | kind: ErrorKind, |
92 | line: Option<usize>, |
93 | col: usize, |
94 | at: Option<usize>, |
95 | message: String, |
96 | key: Vec<String>, |
97 | } |
98 | |
99 | /// Errors that can occur when deserializing a type. |
100 | #[derive (Debug, PartialEq, Eq, Clone)] |
101 | #[non_exhaustive ] |
102 | enum ErrorKind { |
103 | /// EOF was reached when looking for a value |
104 | UnexpectedEof, |
105 | |
106 | /// An invalid character not allowed in a string was found |
107 | InvalidCharInString(char), |
108 | |
109 | /// An invalid character was found as an escape |
110 | InvalidEscape(char), |
111 | |
112 | /// An invalid character was found in a hex escape |
113 | InvalidHexEscape(char), |
114 | |
115 | /// An invalid escape value was specified in a hex escape in a string. |
116 | /// |
117 | /// Valid values are in the plane of unicode codepoints. |
118 | InvalidEscapeValue(u32), |
119 | |
120 | /// A newline in a string was encountered when one was not allowed. |
121 | NewlineInString, |
122 | |
123 | /// An unexpected character was encountered, typically when looking for a |
124 | /// value. |
125 | Unexpected(char), |
126 | |
127 | /// An unterminated string was found where EOF was found before the ending |
128 | /// EOF mark. |
129 | UnterminatedString, |
130 | |
131 | /// A newline was found in a table key. |
132 | NewlineInTableKey, |
133 | |
134 | /// A number failed to parse |
135 | NumberInvalid, |
136 | |
137 | /// A date or datetime was invalid |
138 | DateInvalid, |
139 | |
140 | /// Wanted one sort of token, but found another. |
141 | Wanted { |
142 | /// Expected token type |
143 | expected: &'static str, |
144 | /// Actually found token type |
145 | found: &'static str, |
146 | }, |
147 | |
148 | /// A duplicate table definition was found. |
149 | DuplicateTable(String), |
150 | |
151 | /// A previously defined table was redefined as an array. |
152 | RedefineAsArray, |
153 | |
154 | /// An empty table key was found. |
155 | EmptyTableKey, |
156 | |
157 | /// Multiline strings are not allowed for key |
158 | MultilineStringKey, |
159 | |
160 | /// A custom error which could be generated when deserializing a particular |
161 | /// type. |
162 | Custom, |
163 | |
164 | /// A tuple with a certain number of elements was expected but something |
165 | /// else was found. |
166 | ExpectedTuple(usize), |
167 | |
168 | /// Expected table keys to be in increasing tuple index order, but something |
169 | /// else was found. |
170 | ExpectedTupleIndex { |
171 | /// Expected index. |
172 | expected: usize, |
173 | /// Key that was specified. |
174 | found: String, |
175 | }, |
176 | |
177 | /// An empty table was expected but entries were found |
178 | ExpectedEmptyTable, |
179 | |
180 | /// Dotted key attempted to extend something that is not a table. |
181 | DottedKeyInvalidType, |
182 | |
183 | /// An unexpected key was encountered. |
184 | /// |
185 | /// Used when deserializing a struct with a limited set of fields. |
186 | UnexpectedKeys { |
187 | /// The unexpected keys. |
188 | keys: Vec<String>, |
189 | /// Keys that may be specified. |
190 | available: &'static [&'static str], |
191 | }, |
192 | |
193 | /// Unquoted string was found when quoted one was expected |
194 | UnquotedString, |
195 | } |
196 | |
197 | /// Deserialization implementation for TOML. |
198 | pub struct Deserializer<'a> { |
199 | require_newline_after_table: bool, |
200 | allow_duplciate_after_longer_table: bool, |
201 | input: &'a str, |
202 | tokens: Tokenizer<'a>, |
203 | } |
204 | |
205 | impl<'de, 'b> de::Deserializer<'de> for &'b mut Deserializer<'de> { |
206 | type Error = Error; |
207 | |
208 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error> |
209 | where |
210 | V: de::Visitor<'de>, |
211 | { |
212 | let mut tables = self.tables()?; |
213 | let table_indices = build_table_indices(&tables); |
214 | let table_pindices = build_table_pindices(&tables); |
215 | |
216 | let res = visitor.visit_map(MapVisitor { |
217 | values: Vec::new().into_iter().peekable(), |
218 | next_value: None, |
219 | depth: 0, |
220 | cur: 0, |
221 | cur_parent: 0, |
222 | max: tables.len(), |
223 | table_indices: &table_indices, |
224 | table_pindices: &table_pindices, |
225 | tables: &mut tables, |
226 | array: false, |
227 | de: self, |
228 | }); |
229 | res.map_err(|mut err| { |
230 | // Errors originating from this library (toml), have an offset |
231 | // attached to them already. Other errors, like those originating |
232 | // from serde (like "missing field") or from a custom deserializer, |
233 | // do not have offsets on them. Here, we do a best guess at their |
234 | // location, by attributing them to the "current table" (the last |
235 | // item in `tables`). |
236 | err.fix_offset(|| tables.last().map(|table| table.at)); |
237 | err.fix_linecol(|at| self.to_linecol(at)); |
238 | err |
239 | }) |
240 | } |
241 | |
242 | // Called when the type to deserialize is an enum, as opposed to a field in the type. |
243 | fn deserialize_enum<V>( |
244 | self, |
245 | _name: &'static str, |
246 | _variants: &'static [&'static str], |
247 | visitor: V, |
248 | ) -> Result<V::Value, Error> |
249 | where |
250 | V: de::Visitor<'de>, |
251 | { |
252 | let (value, name) = self.string_or_table()?; |
253 | match value.e { |
254 | E::String(val) => visitor.visit_enum(val.into_deserializer()), |
255 | E::InlineTable(values) => { |
256 | if values.len() != 1 { |
257 | Err(Error::from_kind( |
258 | Some(value.start), |
259 | ErrorKind::Wanted { |
260 | expected: "exactly 1 element" , |
261 | found: if values.is_empty() { |
262 | "zero elements" |
263 | } else { |
264 | "more than 1 element" |
265 | }, |
266 | }, |
267 | )) |
268 | } else { |
269 | visitor.visit_enum(InlineTableDeserializer { |
270 | values: values.into_iter(), |
271 | next_value: None, |
272 | }) |
273 | } |
274 | } |
275 | E::DottedTable(_) => visitor.visit_enum(DottedTableDeserializer { |
276 | name: name.expect("Expected table header to be passed." ), |
277 | value, |
278 | }), |
279 | e => Err(Error::from_kind( |
280 | Some(value.start), |
281 | ErrorKind::Wanted { |
282 | expected: "string or table" , |
283 | found: e.type_name(), |
284 | }, |
285 | )), |
286 | } |
287 | } |
288 | |
289 | fn deserialize_struct<V>( |
290 | self, |
291 | name: &'static str, |
292 | fields: &'static [&'static str], |
293 | visitor: V, |
294 | ) -> Result<V::Value, Error> |
295 | where |
296 | V: de::Visitor<'de>, |
297 | { |
298 | if name == spanned::NAME && fields == [spanned::START, spanned::END, spanned::VALUE] { |
299 | let start = 0; |
300 | let end = self.input.len(); |
301 | |
302 | let res = visitor.visit_map(SpannedDeserializer { |
303 | phantom_data: PhantomData, |
304 | start: Some(start), |
305 | value: Some(self), |
306 | end: Some(end), |
307 | }); |
308 | return res; |
309 | } |
310 | |
311 | self.deserialize_any(visitor) |
312 | } |
313 | |
314 | serde::forward_to_deserialize_any! { |
315 | bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq |
316 | bytes byte_buf map unit newtype_struct |
317 | ignored_any unit_struct tuple_struct tuple option identifier |
318 | } |
319 | } |
320 | |
321 | // Builds a datastructure that allows for efficient sublinear lookups. |
322 | // The returned HashMap contains a mapping from table header (like [a.b.c]) |
323 | // to list of tables with that precise name. The tables are being identified |
324 | // by their index in the passed slice. We use a list as the implementation |
325 | // uses this data structure for arrays as well as tables, |
326 | // so if any top level [[name]] array contains multiple entries, |
327 | // there are multiple entries in the list. |
328 | // The lookup is performed in the `SeqAccess` implementation of `MapVisitor`. |
329 | // The lists are ordered, which we exploit in the search code by using |
330 | // bisection. |
331 | fn build_table_indices<'de>(tables: &[Table<'de>]) -> HashMap<Vec<Cow<'de, str>>, Vec<usize>> { |
332 | let mut res: HashMap>, …> = HashMap::new(); |
333 | for (i: usize, table: &Table<'_>) in tables.iter().enumerate() { |
334 | let header: Vec> = table.header.iter().map(|v: &(Span, Cow<'_, str>)| v.1.clone()).collect::<Vec<_>>(); |
335 | res.entry(header).or_insert_with(default:Vec::new).push(i); |
336 | } |
337 | res |
338 | } |
339 | |
340 | // Builds a datastructure that allows for efficient sublinear lookups. |
341 | // The returned HashMap contains a mapping from table header (like [a.b.c]) |
342 | // to list of tables whose name at least starts with the specified |
343 | // name. So searching for [a.b] would give both [a.b.c.d] as well as [a.b.e]. |
344 | // The tables are being identified by their index in the passed slice. |
345 | // |
346 | // A list is used for two reasons: First, the implementation also |
347 | // stores arrays in the same data structure and any top level array |
348 | // of size 2 or greater creates multiple entries in the list with the |
349 | // same shared name. Second, there can be multiple tables sharing |
350 | // the same prefix. |
351 | // |
352 | // The lookup is performed in the `MapAccess` implementation of `MapVisitor`. |
353 | // The lists are ordered, which we exploit in the search code by using |
354 | // bisection. |
355 | fn build_table_pindices<'de>(tables: &[Table<'de>]) -> HashMap<Vec<Cow<'de, str>>, Vec<usize>> { |
356 | let mut res: HashMap>, …> = HashMap::new(); |
357 | for (i: usize, table: &Table<'_>) in tables.iter().enumerate() { |
358 | let header: Vec> = table.header.iter().map(|v: &(Span, Cow<'_, str>)| v.1.clone()).collect::<Vec<_>>(); |
359 | for len: usize in 0..=header.len() { |
360 | res&mut Vec.entry(header[..len].to_owned()) |
361 | .or_insert_with(default:Vec::new) |
362 | .push(i); |
363 | } |
364 | } |
365 | res |
366 | } |
367 | |
368 | fn headers_equal<'a, 'b>(hdr_a: &[(Span, Cow<'a, str>)], hdr_b: &[(Span, Cow<'b, str>)]) -> bool { |
369 | if hdr_a.len() != hdr_b.len() { |
370 | return false; |
371 | } |
372 | hdr_a.iter().zip(hdr_b.iter()).all(|(h1: &(Span, Cow<'_, str>), h2: &(Span, Cow<'_, str>))| h1.1 == h2.1) |
373 | } |
374 | |
375 | struct Table<'a> { |
376 | at: usize, |
377 | header: Vec<(Span, Cow<'a, str>)>, |
378 | values: Option<Vec<TablePair<'a>>>, |
379 | array: bool, |
380 | } |
381 | |
382 | struct MapVisitor<'de, 'b> { |
383 | values: iter::Peekable<vec::IntoIter<TablePair<'de>>>, |
384 | next_value: Option<TablePair<'de>>, |
385 | depth: usize, |
386 | cur: usize, |
387 | cur_parent: usize, |
388 | max: usize, |
389 | table_indices: &'b HashMap<Vec<Cow<'de, str>>, Vec<usize>>, |
390 | table_pindices: &'b HashMap<Vec<Cow<'de, str>>, Vec<usize>>, |
391 | tables: &'b mut [Table<'de>], |
392 | array: bool, |
393 | de: &'b mut Deserializer<'de>, |
394 | } |
395 | |
396 | impl<'de, 'b> de::MapAccess<'de> for MapVisitor<'de, 'b> { |
397 | type Error = Error; |
398 | |
399 | fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error> |
400 | where |
401 | K: de::DeserializeSeed<'de>, |
402 | { |
403 | if self.cur_parent == self.max || self.cur == self.max { |
404 | return Ok(None); |
405 | } |
406 | |
407 | loop { |
408 | assert!(self.next_value.is_none()); |
409 | if let Some((key, value)) = self.values.next() { |
410 | let ret = seed.deserialize(StrDeserializer::spanned(key.clone()))?; |
411 | self.next_value = Some((key, value)); |
412 | return Ok(Some(ret)); |
413 | } |
414 | |
415 | let next_table = { |
416 | let prefix_stripped = self.tables[self.cur_parent].header[..self.depth] |
417 | .iter() |
418 | .map(|v| v.1.clone()) |
419 | .collect::<Vec<_>>(); |
420 | self.table_pindices |
421 | .get(&prefix_stripped) |
422 | .and_then(|entries| { |
423 | let start = entries.binary_search(&self.cur).unwrap_or_else(|v| v); |
424 | if start == entries.len() || entries[start] < self.cur { |
425 | return None; |
426 | } |
427 | entries[start..] |
428 | .iter() |
429 | .filter_map(|i| if *i < self.max { Some(*i) } else { None }) |
430 | .map(|i| (i, &self.tables[i])) |
431 | .find(|(_, table)| table.values.is_some()) |
432 | .map(|p| p.0) |
433 | }) |
434 | }; |
435 | |
436 | let pos = match next_table { |
437 | Some(pos) => pos, |
438 | None => return Ok(None), |
439 | }; |
440 | self.cur = pos; |
441 | |
442 | // Test to see if we're duplicating our parent's table, and if so |
443 | // then this is an error in the toml format |
444 | if self.cur_parent != pos { |
445 | if headers_equal( |
446 | &self.tables[self.cur_parent].header, |
447 | &self.tables[pos].header, |
448 | ) { |
449 | let at = self.tables[pos].at; |
450 | let name = self.tables[pos] |
451 | .header |
452 | .iter() |
453 | .map(|k| k.1.to_owned()) |
454 | .collect::<Vec<_>>() |
455 | .join("." ); |
456 | return Err(self.de.error(at, ErrorKind::DuplicateTable(name))); |
457 | } |
458 | |
459 | // If we're here we know we should share the same prefix, and if |
460 | // the longer table was defined first then we want to narrow |
461 | // down our parent's length if possible to ensure that we catch |
462 | // duplicate tables defined afterwards. |
463 | if !self.de.allow_duplciate_after_longer_table { |
464 | let parent_len = self.tables[self.cur_parent].header.len(); |
465 | let cur_len = self.tables[pos].header.len(); |
466 | if cur_len < parent_len { |
467 | self.cur_parent = pos; |
468 | } |
469 | } |
470 | } |
471 | |
472 | let table = &mut self.tables[pos]; |
473 | |
474 | // If we're not yet at the appropriate depth for this table then we |
475 | // just next the next portion of its header and then continue |
476 | // decoding. |
477 | if self.depth != table.header.len() { |
478 | let key = &table.header[self.depth]; |
479 | let key = seed.deserialize(StrDeserializer::spanned(key.clone()))?; |
480 | return Ok(Some(key)); |
481 | } |
482 | |
483 | // Rule out cases like: |
484 | // |
485 | // [[foo.bar]] |
486 | // [[foo]] |
487 | if table.array { |
488 | let kind = ErrorKind::RedefineAsArray; |
489 | return Err(self.de.error(table.at, kind)); |
490 | } |
491 | |
492 | self.values = table |
493 | .values |
494 | .take() |
495 | .expect("Unable to read table values" ) |
496 | .into_iter() |
497 | .peekable(); |
498 | } |
499 | } |
500 | |
501 | fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error> |
502 | where |
503 | V: de::DeserializeSeed<'de>, |
504 | { |
505 | if let Some((k, v)) = self.next_value.take() { |
506 | match seed.deserialize(ValueDeserializer::new(v)) { |
507 | Ok(v) => return Ok(v), |
508 | Err(mut e) => { |
509 | e.add_key_context(&k.1); |
510 | return Err(e); |
511 | } |
512 | } |
513 | } |
514 | |
515 | let array = |
516 | self.tables[self.cur].array && self.depth == self.tables[self.cur].header.len() - 1; |
517 | self.cur += 1; |
518 | let res = seed.deserialize(MapVisitor { |
519 | values: Vec::new().into_iter().peekable(), |
520 | next_value: None, |
521 | depth: self.depth + if array { 0 } else { 1 }, |
522 | cur_parent: self.cur - 1, |
523 | cur: 0, |
524 | max: self.max, |
525 | array, |
526 | table_indices: self.table_indices, |
527 | table_pindices: self.table_pindices, |
528 | tables: &mut *self.tables, |
529 | de: &mut *self.de, |
530 | }); |
531 | res.map_err(|mut e| { |
532 | e.add_key_context(&self.tables[self.cur - 1].header[self.depth].1); |
533 | e |
534 | }) |
535 | } |
536 | } |
537 | |
538 | impl<'de, 'b> de::SeqAccess<'de> for MapVisitor<'de, 'b> { |
539 | type Error = Error; |
540 | |
541 | fn next_element_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error> |
542 | where |
543 | K: de::DeserializeSeed<'de>, |
544 | { |
545 | assert!(self.next_value.is_none()); |
546 | assert!(self.values.next().is_none()); |
547 | |
548 | if self.cur_parent == self.max { |
549 | return Ok(None); |
550 | } |
551 | |
552 | let header_stripped = self.tables[self.cur_parent] |
553 | .header |
554 | .iter() |
555 | .map(|v| v.1.clone()) |
556 | .collect::<Vec<_>>(); |
557 | let start_idx = self.cur_parent + 1; |
558 | let next = self |
559 | .table_indices |
560 | .get(&header_stripped) |
561 | .and_then(|entries| { |
562 | let start = entries.binary_search(&start_idx).unwrap_or_else(|v| v); |
563 | if start == entries.len() || entries[start] < start_idx { |
564 | return None; |
565 | } |
566 | entries[start..] |
567 | .iter() |
568 | .filter_map(|i| if *i < self.max { Some(*i) } else { None }) |
569 | .map(|i| (i, &self.tables[i])) |
570 | .find(|(_, table)| table.array) |
571 | .map(|p| p.0) |
572 | }) |
573 | .unwrap_or(self.max); |
574 | |
575 | let ret = seed.deserialize(MapVisitor { |
576 | values: self.tables[self.cur_parent] |
577 | .values |
578 | .take() |
579 | .expect("Unable to read table values" ) |
580 | .into_iter() |
581 | .peekable(), |
582 | next_value: None, |
583 | depth: self.depth + 1, |
584 | cur_parent: self.cur_parent, |
585 | max: next, |
586 | cur: 0, |
587 | array: false, |
588 | table_indices: self.table_indices, |
589 | table_pindices: self.table_pindices, |
590 | tables: self.tables, |
591 | de: self.de, |
592 | })?; |
593 | self.cur_parent = next; |
594 | Ok(Some(ret)) |
595 | } |
596 | } |
597 | |
598 | impl<'de, 'b> de::Deserializer<'de> for MapVisitor<'de, 'b> { |
599 | type Error = Error; |
600 | |
601 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error> |
602 | where |
603 | V: de::Visitor<'de>, |
604 | { |
605 | if self.array { |
606 | visitor.visit_seq(self) |
607 | } else { |
608 | visitor.visit_map(self) |
609 | } |
610 | } |
611 | |
612 | // `None` is interpreted as a missing field so be sure to implement `Some` |
613 | // as a present field. |
614 | fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error> |
615 | where |
616 | V: de::Visitor<'de>, |
617 | { |
618 | visitor.visit_some(self) |
619 | } |
620 | |
621 | fn deserialize_newtype_struct<V>( |
622 | self, |
623 | _name: &'static str, |
624 | visitor: V, |
625 | ) -> Result<V::Value, Error> |
626 | where |
627 | V: de::Visitor<'de>, |
628 | { |
629 | visitor.visit_newtype_struct(self) |
630 | } |
631 | |
632 | fn deserialize_struct<V>( |
633 | mut self, |
634 | name: &'static str, |
635 | fields: &'static [&'static str], |
636 | visitor: V, |
637 | ) -> Result<V::Value, Error> |
638 | where |
639 | V: de::Visitor<'de>, |
640 | { |
641 | if name == spanned::NAME |
642 | && fields == [spanned::START, spanned::END, spanned::VALUE] |
643 | && !(self.array && self.values.peek().is_some()) |
644 | { |
645 | // TODO we can't actually emit spans here for the *entire* table/array |
646 | // due to the format that toml uses. Setting the start and end to 0 is |
647 | // *detectable* (and no reasonable span would look like that), |
648 | // it would be better to expose this in the API via proper |
649 | // ADTs like Option<T>. |
650 | let start = 0; |
651 | let end = 0; |
652 | |
653 | let res = visitor.visit_map(SpannedDeserializer { |
654 | phantom_data: PhantomData, |
655 | start: Some(start), |
656 | value: Some(self), |
657 | end: Some(end), |
658 | }); |
659 | return res; |
660 | } |
661 | |
662 | self.deserialize_any(visitor) |
663 | } |
664 | |
665 | fn deserialize_enum<V>( |
666 | self, |
667 | _name: &'static str, |
668 | _variants: &'static [&'static str], |
669 | visitor: V, |
670 | ) -> Result<V::Value, Error> |
671 | where |
672 | V: de::Visitor<'de>, |
673 | { |
674 | if self.tables.len() != 1 { |
675 | return Err(Error::custom( |
676 | Some(self.cur), |
677 | "enum table must contain exactly one table" .into(), |
678 | )); |
679 | } |
680 | let table = &mut self.tables[0]; |
681 | let values = table.values.take().expect("table has no values?" ); |
682 | if table.header.is_empty() { |
683 | return Err(self.de.error(self.cur, ErrorKind::EmptyTableKey)); |
684 | } |
685 | let name = table.header[table.header.len() - 1].1.to_owned(); |
686 | visitor.visit_enum(DottedTableDeserializer { |
687 | name, |
688 | value: Value { |
689 | e: E::DottedTable(values), |
690 | start: 0, |
691 | end: 0, |
692 | }, |
693 | }) |
694 | } |
695 | |
696 | serde::forward_to_deserialize_any! { |
697 | bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq |
698 | bytes byte_buf map unit identifier |
699 | ignored_any unit_struct tuple_struct tuple |
700 | } |
701 | } |
702 | |
703 | struct StrDeserializer<'a> { |
704 | span: Option<Span>, |
705 | key: Cow<'a, str>, |
706 | } |
707 | |
708 | impl<'a> StrDeserializer<'a> { |
709 | fn spanned(inner: (Span, Cow<'a, str>)) -> StrDeserializer<'a> { |
710 | StrDeserializer { |
711 | span: Some(inner.0), |
712 | key: inner.1, |
713 | } |
714 | } |
715 | fn new(key: Cow<'a, str>) -> StrDeserializer<'a> { |
716 | StrDeserializer { span: None, key } |
717 | } |
718 | } |
719 | |
720 | impl<'a> de::IntoDeserializer<'a, Error> for StrDeserializer<'a> { |
721 | type Deserializer = Self; |
722 | |
723 | fn into_deserializer(self) -> Self::Deserializer { |
724 | self |
725 | } |
726 | } |
727 | |
728 | impl<'de> de::Deserializer<'de> for StrDeserializer<'de> { |
729 | type Error = Error; |
730 | |
731 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error> |
732 | where |
733 | V: de::Visitor<'de>, |
734 | { |
735 | match self.key { |
736 | Cow::Borrowed(s) => visitor.visit_borrowed_str(s), |
737 | Cow::Owned(s) => visitor.visit_string(s), |
738 | } |
739 | } |
740 | |
741 | fn deserialize_struct<V>( |
742 | self, |
743 | name: &'static str, |
744 | fields: &'static [&'static str], |
745 | visitor: V, |
746 | ) -> Result<V::Value, Error> |
747 | where |
748 | V: de::Visitor<'de>, |
749 | { |
750 | if name == spanned::NAME && fields == [spanned::START, spanned::END, spanned::VALUE] { |
751 | if let Some(span) = self.span { |
752 | return visitor.visit_map(SpannedDeserializer { |
753 | phantom_data: PhantomData, |
754 | start: Some(span.start), |
755 | value: Some(StrDeserializer::new(self.key)), |
756 | end: Some(span.end), |
757 | }); |
758 | } |
759 | } |
760 | self.deserialize_any(visitor) |
761 | } |
762 | |
763 | serde::forward_to_deserialize_any! { |
764 | bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq |
765 | bytes byte_buf map option unit newtype_struct |
766 | ignored_any unit_struct tuple_struct tuple enum identifier |
767 | } |
768 | } |
769 | |
770 | struct ValueDeserializer<'a> { |
771 | value: Value<'a>, |
772 | validate_struct_keys: bool, |
773 | } |
774 | |
775 | impl<'a> ValueDeserializer<'a> { |
776 | fn new(value: Value<'a>) -> ValueDeserializer<'a> { |
777 | ValueDeserializer { |
778 | value, |
779 | validate_struct_keys: false, |
780 | } |
781 | } |
782 | |
783 | fn with_struct_key_validation(mut self) -> Self { |
784 | self.validate_struct_keys = true; |
785 | self |
786 | } |
787 | } |
788 | |
789 | impl<'de> de::Deserializer<'de> for ValueDeserializer<'de> { |
790 | type Error = Error; |
791 | |
792 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error> |
793 | where |
794 | V: de::Visitor<'de>, |
795 | { |
796 | let start = self.value.start; |
797 | let res = match self.value.e { |
798 | E::Integer(i) => visitor.visit_i64(i), |
799 | E::Boolean(b) => visitor.visit_bool(b), |
800 | E::Float(f) => visitor.visit_f64(f), |
801 | E::String(Cow::Borrowed(s)) => visitor.visit_borrowed_str(s), |
802 | E::String(Cow::Owned(s)) => visitor.visit_string(s), |
803 | E::Datetime(s) => visitor.visit_map(DatetimeDeserializer { |
804 | date: s, |
805 | visited: false, |
806 | }), |
807 | E::Array(values) => { |
808 | let mut s = de::value::SeqDeserializer::new(values.into_iter()); |
809 | let ret = visitor.visit_seq(&mut s)?; |
810 | s.end()?; |
811 | Ok(ret) |
812 | } |
813 | E::InlineTable(values) | E::DottedTable(values) => { |
814 | visitor.visit_map(InlineTableDeserializer { |
815 | values: values.into_iter(), |
816 | next_value: None, |
817 | }) |
818 | } |
819 | }; |
820 | res.map_err(|mut err| { |
821 | // Attribute the error to whatever value returned the error. |
822 | err.fix_offset(|| Some(start)); |
823 | err |
824 | }) |
825 | } |
826 | |
827 | fn deserialize_struct<V>( |
828 | self, |
829 | name: &'static str, |
830 | fields: &'static [&'static str], |
831 | visitor: V, |
832 | ) -> Result<V::Value, Error> |
833 | where |
834 | V: de::Visitor<'de>, |
835 | { |
836 | if name == datetime::NAME && fields == [datetime::FIELD] { |
837 | if let E::Datetime(s) = self.value.e { |
838 | return visitor.visit_map(DatetimeDeserializer { |
839 | date: s, |
840 | visited: false, |
841 | }); |
842 | } |
843 | } |
844 | |
845 | if self.validate_struct_keys { |
846 | match self.value.e { |
847 | E::InlineTable(ref values) | E::DottedTable(ref values) => { |
848 | let extra_fields = values |
849 | .iter() |
850 | .filter_map(|key_value| { |
851 | let (ref key, ref _val) = *key_value; |
852 | if !fields.contains(&&*(key.1)) { |
853 | Some(key.clone()) |
854 | } else { |
855 | None |
856 | } |
857 | }) |
858 | .collect::<Vec<_>>(); |
859 | |
860 | if !extra_fields.is_empty() { |
861 | return Err(Error::from_kind( |
862 | Some(self.value.start), |
863 | ErrorKind::UnexpectedKeys { |
864 | keys: extra_fields |
865 | .iter() |
866 | .map(|k| k.1.to_string()) |
867 | .collect::<Vec<_>>(), |
868 | available: fields, |
869 | }, |
870 | )); |
871 | } |
872 | } |
873 | _ => {} |
874 | } |
875 | } |
876 | |
877 | if name == spanned::NAME && fields == [spanned::START, spanned::END, spanned::VALUE] { |
878 | let start = self.value.start; |
879 | let end = self.value.end; |
880 | |
881 | return visitor.visit_map(SpannedDeserializer { |
882 | phantom_data: PhantomData, |
883 | start: Some(start), |
884 | value: Some(self.value), |
885 | end: Some(end), |
886 | }); |
887 | } |
888 | |
889 | self.deserialize_any(visitor) |
890 | } |
891 | |
892 | // `None` is interpreted as a missing field so be sure to implement `Some` |
893 | // as a present field. |
894 | fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error> |
895 | where |
896 | V: de::Visitor<'de>, |
897 | { |
898 | visitor.visit_some(self) |
899 | } |
900 | |
901 | fn deserialize_enum<V>( |
902 | self, |
903 | _name: &'static str, |
904 | _variants: &'static [&'static str], |
905 | visitor: V, |
906 | ) -> Result<V::Value, Error> |
907 | where |
908 | V: de::Visitor<'de>, |
909 | { |
910 | match self.value.e { |
911 | E::String(val) => visitor.visit_enum(val.into_deserializer()), |
912 | E::InlineTable(values) => { |
913 | if values.len() != 1 { |
914 | Err(Error::from_kind( |
915 | Some(self.value.start), |
916 | ErrorKind::Wanted { |
917 | expected: "exactly 1 element" , |
918 | found: if values.is_empty() { |
919 | "zero elements" |
920 | } else { |
921 | "more than 1 element" |
922 | }, |
923 | }, |
924 | )) |
925 | } else { |
926 | visitor.visit_enum(InlineTableDeserializer { |
927 | values: values.into_iter(), |
928 | next_value: None, |
929 | }) |
930 | } |
931 | } |
932 | e => Err(Error::from_kind( |
933 | Some(self.value.start), |
934 | ErrorKind::Wanted { |
935 | expected: "string or inline table" , |
936 | found: e.type_name(), |
937 | }, |
938 | )), |
939 | } |
940 | } |
941 | |
942 | fn deserialize_newtype_struct<V>( |
943 | self, |
944 | _name: &'static str, |
945 | visitor: V, |
946 | ) -> Result<V::Value, Error> |
947 | where |
948 | V: de::Visitor<'de>, |
949 | { |
950 | visitor.visit_newtype_struct(self) |
951 | } |
952 | |
953 | serde::forward_to_deserialize_any! { |
954 | bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq |
955 | bytes byte_buf map unit identifier |
956 | ignored_any unit_struct tuple_struct tuple |
957 | } |
958 | } |
959 | |
960 | impl<'de, 'b> de::IntoDeserializer<'de, Error> for MapVisitor<'de, 'b> { |
961 | type Deserializer = MapVisitor<'de, 'b>; |
962 | |
963 | fn into_deserializer(self) -> Self::Deserializer { |
964 | self |
965 | } |
966 | } |
967 | |
968 | impl<'de, 'b> de::IntoDeserializer<'de, Error> for &'b mut Deserializer<'de> { |
969 | type Deserializer = Self; |
970 | |
971 | fn into_deserializer(self) -> Self::Deserializer { |
972 | self |
973 | } |
974 | } |
975 | |
976 | impl<'de> de::IntoDeserializer<'de, Error> for Value<'de> { |
977 | type Deserializer = ValueDeserializer<'de>; |
978 | |
979 | fn into_deserializer(self) -> Self::Deserializer { |
980 | ValueDeserializer::new(self) |
981 | } |
982 | } |
983 | |
984 | struct SpannedDeserializer<'de, T: de::IntoDeserializer<'de, Error>> { |
985 | phantom_data: PhantomData<&'de ()>, |
986 | start: Option<usize>, |
987 | end: Option<usize>, |
988 | value: Option<T>, |
989 | } |
990 | |
991 | impl<'de, T> de::MapAccess<'de> for SpannedDeserializer<'de, T> |
992 | where |
993 | T: de::IntoDeserializer<'de, Error>, |
994 | { |
995 | type Error = Error; |
996 | |
997 | fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error> |
998 | where |
999 | K: de::DeserializeSeed<'de>, |
1000 | { |
1001 | if self.start.is_some() { |
1002 | seed.deserialize(BorrowedStrDeserializer::new(spanned::START)) |
1003 | .map(Some) |
1004 | } else if self.end.is_some() { |
1005 | seed.deserialize(BorrowedStrDeserializer::new(spanned::END)) |
1006 | .map(Some) |
1007 | } else if self.value.is_some() { |
1008 | seed.deserialize(BorrowedStrDeserializer::new(spanned::VALUE)) |
1009 | .map(Some) |
1010 | } else { |
1011 | Ok(None) |
1012 | } |
1013 | } |
1014 | |
1015 | fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error> |
1016 | where |
1017 | V: de::DeserializeSeed<'de>, |
1018 | { |
1019 | if let Some(start) = self.start.take() { |
1020 | seed.deserialize(start.into_deserializer()) |
1021 | } else if let Some(end) = self.end.take() { |
1022 | seed.deserialize(end.into_deserializer()) |
1023 | } else if let Some(value) = self.value.take() { |
1024 | seed.deserialize(value.into_deserializer()) |
1025 | } else { |
1026 | panic!("next_value_seed called before next_key_seed" ) |
1027 | } |
1028 | } |
1029 | } |
1030 | |
1031 | struct DatetimeDeserializer<'a> { |
1032 | visited: bool, |
1033 | date: &'a str, |
1034 | } |
1035 | |
1036 | impl<'de> de::MapAccess<'de> for DatetimeDeserializer<'de> { |
1037 | type Error = Error; |
1038 | |
1039 | fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error> |
1040 | where |
1041 | K: de::DeserializeSeed<'de>, |
1042 | { |
1043 | if self.visited { |
1044 | return Ok(None); |
1045 | } |
1046 | self.visited = true; |
1047 | seed.deserialize(DatetimeFieldDeserializer).map(op:Some) |
1048 | } |
1049 | |
1050 | fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error> |
1051 | where |
1052 | V: de::DeserializeSeed<'de>, |
1053 | { |
1054 | seed.deserialize(deserializer:StrDeserializer::new(self.date.into())) |
1055 | } |
1056 | } |
1057 | |
1058 | struct DatetimeFieldDeserializer; |
1059 | |
1060 | impl<'de> de::Deserializer<'de> for DatetimeFieldDeserializer { |
1061 | type Error = Error; |
1062 | |
1063 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error> |
1064 | where |
1065 | V: de::Visitor<'de>, |
1066 | { |
1067 | visitor.visit_borrowed_str(datetime::FIELD) |
1068 | } |
1069 | |
1070 | serde::forward_to_deserialize_any! { |
1071 | bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq |
1072 | bytes byte_buf map struct option unit newtype_struct |
1073 | ignored_any unit_struct tuple_struct tuple enum identifier |
1074 | } |
1075 | } |
1076 | |
1077 | struct DottedTableDeserializer<'a> { |
1078 | name: Cow<'a, str>, |
1079 | value: Value<'a>, |
1080 | } |
1081 | |
1082 | impl<'de> de::EnumAccess<'de> for DottedTableDeserializer<'de> { |
1083 | type Error = Error; |
1084 | type Variant = TableEnumDeserializer<'de>; |
1085 | |
1086 | fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error> |
1087 | where |
1088 | V: de::DeserializeSeed<'de>, |
1089 | { |
1090 | let (name: Cow<'_, str>, value: Value<'_>) = (self.name, self.value); |
1091 | seed.deserialize(StrDeserializer::new(name)) |
1092 | .map(|val: ::Value| (val, TableEnumDeserializer { value })) |
1093 | } |
1094 | } |
1095 | |
1096 | struct InlineTableDeserializer<'a> { |
1097 | values: vec::IntoIter<TablePair<'a>>, |
1098 | next_value: Option<Value<'a>>, |
1099 | } |
1100 | |
1101 | impl<'de> de::MapAccess<'de> for InlineTableDeserializer<'de> { |
1102 | type Error = Error; |
1103 | |
1104 | fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error> |
1105 | where |
1106 | K: de::DeserializeSeed<'de>, |
1107 | { |
1108 | let (key: (Span, Cow<'_, str>), value: Value<'_>) = match self.values.next() { |
1109 | Some(pair: ((Span, Cow<'_, str>), Value<'_>)) => pair, |
1110 | None => return Ok(None), |
1111 | }; |
1112 | self.next_value = Some(value); |
1113 | seed.deserialize(StrDeserializer::spanned(key)).map(op:Some) |
1114 | } |
1115 | |
1116 | fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error> |
1117 | where |
1118 | V: de::DeserializeSeed<'de>, |
1119 | { |
1120 | let value: Value<'_> = self.next_value.take().expect(msg:"Unable to read table values" ); |
1121 | seed.deserialize(deserializer:ValueDeserializer::new(value)) |
1122 | } |
1123 | } |
1124 | |
1125 | impl<'de> de::EnumAccess<'de> for InlineTableDeserializer<'de> { |
1126 | type Error = Error; |
1127 | type Variant = TableEnumDeserializer<'de>; |
1128 | |
1129 | fn variant_seed<V>(mut self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error> |
1130 | where |
1131 | V: de::DeserializeSeed<'de>, |
1132 | { |
1133 | let (key, value) = match self.values.next() { |
1134 | Some(pair) => pair, |
1135 | None => { |
1136 | return Err(Error::from_kind( |
1137 | None, // FIXME: How do we get an offset here? |
1138 | ErrorKind::Wanted { |
1139 | expected: "table with exactly 1 entry" , |
1140 | found: "empty table" , |
1141 | }, |
1142 | )); |
1143 | } |
1144 | }; |
1145 | |
1146 | seed.deserialize(StrDeserializer::new(key.1)) |
1147 | .map(|val| (val, TableEnumDeserializer { value })) |
1148 | } |
1149 | } |
1150 | |
1151 | /// Deserializes table values into enum variants. |
1152 | struct TableEnumDeserializer<'a> { |
1153 | value: Value<'a>, |
1154 | } |
1155 | |
1156 | impl<'de> de::VariantAccess<'de> for TableEnumDeserializer<'de> { |
1157 | type Error = Error; |
1158 | |
1159 | fn unit_variant(self) -> Result<(), Self::Error> { |
1160 | match self.value.e { |
1161 | E::InlineTable(values) | E::DottedTable(values) => { |
1162 | if values.is_empty() { |
1163 | Ok(()) |
1164 | } else { |
1165 | Err(Error::from_kind( |
1166 | Some(self.value.start), |
1167 | ErrorKind::ExpectedEmptyTable, |
1168 | )) |
1169 | } |
1170 | } |
1171 | e => Err(Error::from_kind( |
1172 | Some(self.value.start), |
1173 | ErrorKind::Wanted { |
1174 | expected: "table" , |
1175 | found: e.type_name(), |
1176 | }, |
1177 | )), |
1178 | } |
1179 | } |
1180 | |
1181 | fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error> |
1182 | where |
1183 | T: de::DeserializeSeed<'de>, |
1184 | { |
1185 | seed.deserialize(ValueDeserializer::new(self.value)) |
1186 | } |
1187 | |
1188 | fn tuple_variant<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error> |
1189 | where |
1190 | V: de::Visitor<'de>, |
1191 | { |
1192 | match self.value.e { |
1193 | E::InlineTable(values) | E::DottedTable(values) => { |
1194 | let tuple_values = values |
1195 | .into_iter() |
1196 | .enumerate() |
1197 | .map(|(index, (key, value))| match key.1.parse::<usize>() { |
1198 | Ok(key_index) if key_index == index => Ok(value), |
1199 | Ok(_) | Err(_) => Err(Error::from_kind( |
1200 | Some(key.0.start), |
1201 | ErrorKind::ExpectedTupleIndex { |
1202 | expected: index, |
1203 | found: key.1.to_string(), |
1204 | }, |
1205 | )), |
1206 | }) |
1207 | // Fold all values into a `Vec`, or return the first error. |
1208 | .fold(Ok(Vec::with_capacity(len)), |result, value_result| { |
1209 | result.and_then(move |mut tuple_values| match value_result { |
1210 | Ok(value) => { |
1211 | tuple_values.push(value); |
1212 | Ok(tuple_values) |
1213 | } |
1214 | // `Result<de::Value, Self::Error>` to `Result<Vec<_>, Self::Error>` |
1215 | Err(e) => Err(e), |
1216 | }) |
1217 | })?; |
1218 | |
1219 | if tuple_values.len() == len { |
1220 | de::Deserializer::deserialize_seq( |
1221 | ValueDeserializer::new(Value { |
1222 | e: E::Array(tuple_values), |
1223 | start: self.value.start, |
1224 | end: self.value.end, |
1225 | }), |
1226 | visitor, |
1227 | ) |
1228 | } else { |
1229 | Err(Error::from_kind( |
1230 | Some(self.value.start), |
1231 | ErrorKind::ExpectedTuple(len), |
1232 | )) |
1233 | } |
1234 | } |
1235 | e => Err(Error::from_kind( |
1236 | Some(self.value.start), |
1237 | ErrorKind::Wanted { |
1238 | expected: "table" , |
1239 | found: e.type_name(), |
1240 | }, |
1241 | )), |
1242 | } |
1243 | } |
1244 | |
1245 | fn struct_variant<V>( |
1246 | self, |
1247 | fields: &'static [&'static str], |
1248 | visitor: V, |
1249 | ) -> Result<V::Value, Self::Error> |
1250 | where |
1251 | V: de::Visitor<'de>, |
1252 | { |
1253 | de::Deserializer::deserialize_struct( |
1254 | ValueDeserializer::new(self.value).with_struct_key_validation(), |
1255 | "" , // TODO: this should be the variant name |
1256 | fields, |
1257 | visitor, |
1258 | ) |
1259 | } |
1260 | } |
1261 | |
1262 | impl<'a> Deserializer<'a> { |
1263 | /// Creates a new deserializer which will be deserializing the string |
1264 | /// provided. |
1265 | pub fn new(input: &'a str) -> Deserializer<'a> { |
1266 | Deserializer { |
1267 | tokens: Tokenizer::new(input), |
1268 | input, |
1269 | require_newline_after_table: true, |
1270 | allow_duplciate_after_longer_table: false, |
1271 | } |
1272 | } |
1273 | |
1274 | #[doc (hidden)] |
1275 | #[deprecated (since = "0.5.11" )] |
1276 | pub fn end(&mut self) -> Result<(), Error> { |
1277 | Ok(()) |
1278 | } |
1279 | |
1280 | #[doc (hidden)] |
1281 | #[deprecated (since = "0.5.11" )] |
1282 | pub fn set_require_newline_after_table(&mut self, require: bool) { |
1283 | self.require_newline_after_table = require; |
1284 | } |
1285 | |
1286 | #[doc (hidden)] |
1287 | #[deprecated (since = "0.5.11" )] |
1288 | pub fn set_allow_duplicate_after_longer_table(&mut self, allow: bool) { |
1289 | self.allow_duplciate_after_longer_table = allow; |
1290 | } |
1291 | |
1292 | fn tables(&mut self) -> Result<Vec<Table<'a>>, Error> { |
1293 | let mut tables = Vec::new(); |
1294 | let mut cur_table = Table { |
1295 | at: 0, |
1296 | header: Vec::new(), |
1297 | values: None, |
1298 | array: false, |
1299 | }; |
1300 | |
1301 | while let Some(line) = self.line()? { |
1302 | match line { |
1303 | Line::Table { |
1304 | at, |
1305 | mut header, |
1306 | array, |
1307 | } => { |
1308 | if !cur_table.header.is_empty() || cur_table.values.is_some() { |
1309 | tables.push(cur_table); |
1310 | } |
1311 | cur_table = Table { |
1312 | at, |
1313 | header: Vec::new(), |
1314 | values: Some(Vec::new()), |
1315 | array, |
1316 | }; |
1317 | loop { |
1318 | let part = header.next().map_err(|e| self.token_error(e)); |
1319 | match part? { |
1320 | Some(part) => cur_table.header.push(part), |
1321 | None => break, |
1322 | } |
1323 | } |
1324 | } |
1325 | Line::KeyValue(key, value) => { |
1326 | if cur_table.values.is_none() { |
1327 | cur_table.values = Some(Vec::new()); |
1328 | } |
1329 | self.add_dotted_key(key, value, cur_table.values.as_mut().unwrap())?; |
1330 | } |
1331 | } |
1332 | } |
1333 | if !cur_table.header.is_empty() || cur_table.values.is_some() { |
1334 | tables.push(cur_table); |
1335 | } |
1336 | Ok(tables) |
1337 | } |
1338 | |
1339 | fn line(&mut self) -> Result<Option<Line<'a>>, Error> { |
1340 | loop { |
1341 | self.eat_whitespace()?; |
1342 | if self.eat_comment()? { |
1343 | continue; |
1344 | } |
1345 | if self.eat(Token::Newline)? { |
1346 | continue; |
1347 | } |
1348 | break; |
1349 | } |
1350 | |
1351 | match self.peek()? { |
1352 | Some((_, Token::LeftBracket)) => self.table_header().map(Some), |
1353 | Some(_) => self.key_value().map(Some), |
1354 | None => Ok(None), |
1355 | } |
1356 | } |
1357 | |
1358 | fn table_header(&mut self) -> Result<Line<'a>, Error> { |
1359 | let start = self.tokens.current(); |
1360 | self.expect(Token::LeftBracket)?; |
1361 | let array = self.eat(Token::LeftBracket)?; |
1362 | let ret = Header::new(self.tokens.clone(), array, self.require_newline_after_table); |
1363 | if self.require_newline_after_table { |
1364 | self.tokens.skip_to_newline(); |
1365 | } else { |
1366 | loop { |
1367 | match self.next()? { |
1368 | Some((_, Token::RightBracket)) => { |
1369 | if array { |
1370 | self.eat(Token::RightBracket)?; |
1371 | } |
1372 | break; |
1373 | } |
1374 | Some((_, Token::Newline)) | None => break, |
1375 | _ => {} |
1376 | } |
1377 | } |
1378 | self.eat_whitespace()?; |
1379 | } |
1380 | Ok(Line::Table { |
1381 | at: start, |
1382 | header: ret, |
1383 | array, |
1384 | }) |
1385 | } |
1386 | |
1387 | fn key_value(&mut self) -> Result<Line<'a>, Error> { |
1388 | let key = self.dotted_key()?; |
1389 | self.eat_whitespace()?; |
1390 | self.expect(Token::Equals)?; |
1391 | self.eat_whitespace()?; |
1392 | |
1393 | let value = self.value()?; |
1394 | self.eat_whitespace()?; |
1395 | if !self.eat_comment()? { |
1396 | self.eat_newline_or_eof()?; |
1397 | } |
1398 | |
1399 | Ok(Line::KeyValue(key, value)) |
1400 | } |
1401 | |
1402 | fn value(&mut self) -> Result<Value<'a>, Error> { |
1403 | let at = self.tokens.current(); |
1404 | let value = match self.next()? { |
1405 | Some((Span { start, end }, Token::String { val, .. })) => Value { |
1406 | e: E::String(val), |
1407 | start, |
1408 | end, |
1409 | }, |
1410 | Some((Span { start, end }, Token::Keylike("true" ))) => Value { |
1411 | e: E::Boolean(true), |
1412 | start, |
1413 | end, |
1414 | }, |
1415 | Some((Span { start, end }, Token::Keylike("false" ))) => Value { |
1416 | e: E::Boolean(false), |
1417 | start, |
1418 | end, |
1419 | }, |
1420 | Some((span, Token::Keylike(key))) => self.parse_keylike(at, span, key)?, |
1421 | Some((span, Token::Plus)) => self.number_leading_plus(span)?, |
1422 | Some((Span { start, .. }, Token::LeftBrace)) => { |
1423 | self.inline_table().map(|(Span { end, .. }, table)| Value { |
1424 | e: E::InlineTable(table), |
1425 | start, |
1426 | end, |
1427 | })? |
1428 | } |
1429 | Some((Span { start, .. }, Token::LeftBracket)) => { |
1430 | self.array().map(|(Span { end, .. }, array)| Value { |
1431 | e: E::Array(array), |
1432 | start, |
1433 | end, |
1434 | })? |
1435 | } |
1436 | Some(token) => { |
1437 | return Err(self.error( |
1438 | at, |
1439 | ErrorKind::Wanted { |
1440 | expected: "a value" , |
1441 | found: token.1.describe(), |
1442 | }, |
1443 | )); |
1444 | } |
1445 | None => return Err(self.eof()), |
1446 | }; |
1447 | Ok(value) |
1448 | } |
1449 | |
1450 | fn parse_keylike(&mut self, at: usize, span: Span, key: &'a str) -> Result<Value<'a>, Error> { |
1451 | if key == "inf" || key == "nan" { |
1452 | return self.number_or_date(span, key); |
1453 | } |
1454 | |
1455 | let first_char = key.chars().next().expect("key should not be empty here" ); |
1456 | match first_char { |
1457 | '-' | '0' ..='9' => self.number_or_date(span, key), |
1458 | _ => Err(self.error(at, ErrorKind::UnquotedString)), |
1459 | } |
1460 | } |
1461 | |
1462 | fn number_or_date(&mut self, span: Span, s: &'a str) -> Result<Value<'a>, Error> { |
1463 | if s.contains('T' ) |
1464 | || s.contains('t' ) |
1465 | || (s.len() > 1 && s[1..].contains('-' ) && !s.contains("e-" ) && !s.contains("E-" )) |
1466 | { |
1467 | self.datetime(span, s, false) |
1468 | .map(|(Span { start, end }, d)| Value { |
1469 | e: E::Datetime(d), |
1470 | start, |
1471 | end, |
1472 | }) |
1473 | } else if self.eat(Token::Colon)? { |
1474 | self.datetime(span, s, true) |
1475 | .map(|(Span { start, end }, d)| Value { |
1476 | e: E::Datetime(d), |
1477 | start, |
1478 | end, |
1479 | }) |
1480 | } else { |
1481 | self.number(span, s) |
1482 | } |
1483 | } |
1484 | |
1485 | /// Returns a string or table value type. |
1486 | /// |
1487 | /// Used to deserialize enums. Unit enums may be represented as a string or a table, all other |
1488 | /// structures (tuple, newtype, struct) must be represented as a table. |
1489 | fn string_or_table(&mut self) -> Result<(Value<'a>, Option<Cow<'a, str>>), Error> { |
1490 | match self.peek()? { |
1491 | Some((span, Token::LeftBracket)) => { |
1492 | let tables = self.tables()?; |
1493 | if tables.len() != 1 { |
1494 | return Err(Error::from_kind( |
1495 | Some(span.start), |
1496 | ErrorKind::Wanted { |
1497 | expected: "exactly 1 table" , |
1498 | found: if tables.is_empty() { |
1499 | "zero tables" |
1500 | } else { |
1501 | "more than 1 table" |
1502 | }, |
1503 | }, |
1504 | )); |
1505 | } |
1506 | |
1507 | let table = tables |
1508 | .into_iter() |
1509 | .next() |
1510 | .expect("Expected exactly one table" ); |
1511 | let header = table |
1512 | .header |
1513 | .last() |
1514 | .expect("Expected at least one header value for table." ); |
1515 | |
1516 | let start = table.at; |
1517 | let end = table |
1518 | .values |
1519 | .as_ref() |
1520 | .and_then(|values| values.last()) |
1521 | .map(|&(_, ref val)| val.end) |
1522 | .unwrap_or_else(|| header.1.len()); |
1523 | Ok(( |
1524 | Value { |
1525 | e: E::DottedTable(table.values.unwrap_or_default()), |
1526 | start, |
1527 | end, |
1528 | }, |
1529 | Some(header.1.clone()), |
1530 | )) |
1531 | } |
1532 | Some(_) => self.value().map(|val| (val, None)), |
1533 | None => Err(self.eof()), |
1534 | } |
1535 | } |
1536 | |
1537 | fn number(&mut self, Span { start, end }: Span, s: &'a str) -> Result<Value<'a>, Error> { |
1538 | let to_integer = |f| Value { |
1539 | e: E::Integer(f), |
1540 | start, |
1541 | end, |
1542 | }; |
1543 | if let Some(value) = s.strip_prefix("0x" ) { |
1544 | self.integer(value, 16).map(to_integer) |
1545 | } else if let Some(value) = s.strip_prefix("0o" ) { |
1546 | self.integer(value, 8).map(to_integer) |
1547 | } else if let Some(value) = s.strip_prefix("0b" ) { |
1548 | self.integer(value, 2).map(to_integer) |
1549 | } else if s.contains('e' ) || s.contains('E' ) { |
1550 | self.float(s, None).map(|f| Value { |
1551 | e: E::Float(f), |
1552 | start, |
1553 | end, |
1554 | }) |
1555 | } else if self.eat(Token::Period)? { |
1556 | let at = self.tokens.current(); |
1557 | match self.next()? { |
1558 | Some((Span { start, end }, Token::Keylike(after))) => { |
1559 | self.float(s, Some(after)).map(|f| Value { |
1560 | e: E::Float(f), |
1561 | start, |
1562 | end, |
1563 | }) |
1564 | } |
1565 | _ => Err(self.error(at, ErrorKind::NumberInvalid)), |
1566 | } |
1567 | } else if s == "inf" { |
1568 | Ok(Value { |
1569 | e: E::Float(f64::INFINITY), |
1570 | start, |
1571 | end, |
1572 | }) |
1573 | } else if s == "-inf" { |
1574 | Ok(Value { |
1575 | e: E::Float(f64::NEG_INFINITY), |
1576 | start, |
1577 | end, |
1578 | }) |
1579 | } else if s == "nan" { |
1580 | Ok(Value { |
1581 | e: E::Float(f64::NAN), |
1582 | start, |
1583 | end, |
1584 | }) |
1585 | } else if s == "-nan" { |
1586 | Ok(Value { |
1587 | e: E::Float(-f64::NAN), |
1588 | start, |
1589 | end, |
1590 | }) |
1591 | } else { |
1592 | self.integer(s, 10).map(to_integer) |
1593 | } |
1594 | } |
1595 | |
1596 | fn number_leading_plus(&mut self, Span { start, .. }: Span) -> Result<Value<'a>, Error> { |
1597 | let start_token = self.tokens.current(); |
1598 | match self.next()? { |
1599 | Some((Span { end, .. }, Token::Keylike(s))) => self.number(Span { start, end }, s), |
1600 | _ => Err(self.error(start_token, ErrorKind::NumberInvalid)), |
1601 | } |
1602 | } |
1603 | |
1604 | fn integer(&self, s: &'a str, radix: u32) -> Result<i64, Error> { |
1605 | let allow_sign = radix == 10; |
1606 | let allow_leading_zeros = radix != 10; |
1607 | let (prefix, suffix) = self.parse_integer(s, allow_sign, allow_leading_zeros, radix)?; |
1608 | let start = self.tokens.substr_offset(s); |
1609 | if !suffix.is_empty() { |
1610 | return Err(self.error(start, ErrorKind::NumberInvalid)); |
1611 | } |
1612 | i64::from_str_radix(prefix.replace('_' , "" ).trim_start_matches('+' ), radix) |
1613 | .map_err(|_e| self.error(start, ErrorKind::NumberInvalid)) |
1614 | } |
1615 | |
1616 | fn parse_integer( |
1617 | &self, |
1618 | s: &'a str, |
1619 | allow_sign: bool, |
1620 | allow_leading_zeros: bool, |
1621 | radix: u32, |
1622 | ) -> Result<(&'a str, &'a str), Error> { |
1623 | let start = self.tokens.substr_offset(s); |
1624 | |
1625 | let mut first = true; |
1626 | let mut first_zero = false; |
1627 | let mut underscore = false; |
1628 | let mut end = s.len(); |
1629 | for (i, c) in s.char_indices() { |
1630 | let at = i + start; |
1631 | if i == 0 && (c == '+' || c == '-' ) && allow_sign { |
1632 | continue; |
1633 | } |
1634 | |
1635 | if c == '0' && first { |
1636 | first_zero = true; |
1637 | } else if c.is_digit(radix) { |
1638 | if !first && first_zero && !allow_leading_zeros { |
1639 | return Err(self.error(at, ErrorKind::NumberInvalid)); |
1640 | } |
1641 | underscore = false; |
1642 | } else if c == '_' && first { |
1643 | return Err(self.error(at, ErrorKind::NumberInvalid)); |
1644 | } else if c == '_' && !underscore { |
1645 | underscore = true; |
1646 | } else { |
1647 | end = i; |
1648 | break; |
1649 | } |
1650 | first = false; |
1651 | } |
1652 | if first || underscore { |
1653 | return Err(self.error(start, ErrorKind::NumberInvalid)); |
1654 | } |
1655 | Ok((&s[..end], &s[end..])) |
1656 | } |
1657 | |
1658 | fn float(&mut self, s: &'a str, after_decimal: Option<&'a str>) -> Result<f64, Error> { |
1659 | let (integral, mut suffix) = self.parse_integer(s, true, false, 10)?; |
1660 | let start = self.tokens.substr_offset(integral); |
1661 | |
1662 | let mut fraction = None; |
1663 | if let Some(after) = after_decimal { |
1664 | if !suffix.is_empty() { |
1665 | return Err(self.error(start, ErrorKind::NumberInvalid)); |
1666 | } |
1667 | let (a, b) = self.parse_integer(after, false, true, 10)?; |
1668 | fraction = Some(a); |
1669 | suffix = b; |
1670 | } |
1671 | |
1672 | let mut exponent = None; |
1673 | if suffix.starts_with('e' ) || suffix.starts_with('E' ) { |
1674 | let (a, b) = if suffix.len() == 1 { |
1675 | self.eat(Token::Plus)?; |
1676 | match self.next()? { |
1677 | Some((_, Token::Keylike(s))) => self.parse_integer(s, false, true, 10)?, |
1678 | _ => return Err(self.error(start, ErrorKind::NumberInvalid)), |
1679 | } |
1680 | } else { |
1681 | self.parse_integer(&suffix[1..], true, true, 10)? |
1682 | }; |
1683 | if !b.is_empty() { |
1684 | return Err(self.error(start, ErrorKind::NumberInvalid)); |
1685 | } |
1686 | exponent = Some(a); |
1687 | } else if !suffix.is_empty() { |
1688 | return Err(self.error(start, ErrorKind::NumberInvalid)); |
1689 | } |
1690 | |
1691 | let mut number = integral |
1692 | .trim_start_matches('+' ) |
1693 | .chars() |
1694 | .filter(|c| *c != '_' ) |
1695 | .collect::<String>(); |
1696 | if let Some(fraction) = fraction { |
1697 | number.push('.' ); |
1698 | number.extend(fraction.chars().filter(|c| *c != '_' )); |
1699 | } |
1700 | if let Some(exponent) = exponent { |
1701 | number.push('E' ); |
1702 | number.extend(exponent.chars().filter(|c| *c != '_' )); |
1703 | } |
1704 | number |
1705 | .parse() |
1706 | .map_err(|_e| self.error(start, ErrorKind::NumberInvalid)) |
1707 | .and_then(|n: f64| { |
1708 | if n.is_finite() { |
1709 | Ok(n) |
1710 | } else { |
1711 | Err(self.error(start, ErrorKind::NumberInvalid)) |
1712 | } |
1713 | }) |
1714 | } |
1715 | |
1716 | fn datetime( |
1717 | &mut self, |
1718 | mut span: Span, |
1719 | date: &'a str, |
1720 | colon_eaten: bool, |
1721 | ) -> Result<(Span, &'a str), Error> { |
1722 | let start = self.tokens.substr_offset(date); |
1723 | |
1724 | // Check for space separated date and time. |
1725 | let mut lookahead = self.tokens.clone(); |
1726 | if let Ok(Some((_, Token::Whitespace(" " )))) = lookahead.next() { |
1727 | // Check if hour follows. |
1728 | if let Ok(Some((_, Token::Keylike(_)))) = lookahead.next() { |
1729 | self.next()?; // skip space |
1730 | self.next()?; // skip keylike hour |
1731 | } |
1732 | } |
1733 | |
1734 | if colon_eaten || self.eat(Token::Colon)? { |
1735 | // minutes |
1736 | match self.next()? { |
1737 | Some((_, Token::Keylike(_))) => {} |
1738 | _ => return Err(self.error(start, ErrorKind::DateInvalid)), |
1739 | } |
1740 | // Seconds |
1741 | self.expect(Token::Colon)?; |
1742 | match self.next()? { |
1743 | Some((Span { end, .. }, Token::Keylike(_))) => { |
1744 | span.end = end; |
1745 | } |
1746 | _ => return Err(self.error(start, ErrorKind::DateInvalid)), |
1747 | } |
1748 | // Fractional seconds |
1749 | if self.eat(Token::Period)? { |
1750 | match self.next()? { |
1751 | Some((Span { end, .. }, Token::Keylike(_))) => { |
1752 | span.end = end; |
1753 | } |
1754 | _ => return Err(self.error(start, ErrorKind::DateInvalid)), |
1755 | } |
1756 | } |
1757 | |
1758 | // offset |
1759 | if self.eat(Token::Plus)? { |
1760 | match self.next()? { |
1761 | Some((Span { end, .. }, Token::Keylike(_))) => { |
1762 | span.end = end; |
1763 | } |
1764 | _ => return Err(self.error(start, ErrorKind::DateInvalid)), |
1765 | } |
1766 | } |
1767 | if self.eat(Token::Colon)? { |
1768 | match self.next()? { |
1769 | Some((Span { end, .. }, Token::Keylike(_))) => { |
1770 | span.end = end; |
1771 | } |
1772 | _ => return Err(self.error(start, ErrorKind::DateInvalid)), |
1773 | } |
1774 | } |
1775 | } |
1776 | |
1777 | let end = self.tokens.current(); |
1778 | Ok((span, &self.tokens.input()[start..end])) |
1779 | } |
1780 | |
1781 | // TODO(#140): shouldn't buffer up this entire table in memory, it'd be |
1782 | // great to defer parsing everything until later. |
1783 | fn inline_table(&mut self) -> Result<(Span, Vec<TablePair<'a>>), Error> { |
1784 | let mut ret = Vec::new(); |
1785 | self.eat_whitespace()?; |
1786 | if let Some(span) = self.eat_spanned(Token::RightBrace)? { |
1787 | return Ok((span, ret)); |
1788 | } |
1789 | loop { |
1790 | let key = self.dotted_key()?; |
1791 | self.eat_whitespace()?; |
1792 | self.expect(Token::Equals)?; |
1793 | self.eat_whitespace()?; |
1794 | let value = self.value()?; |
1795 | self.add_dotted_key(key, value, &mut ret)?; |
1796 | |
1797 | self.eat_whitespace()?; |
1798 | if let Some(span) = self.eat_spanned(Token::RightBrace)? { |
1799 | return Ok((span, ret)); |
1800 | } |
1801 | self.expect(Token::Comma)?; |
1802 | self.eat_whitespace()?; |
1803 | } |
1804 | } |
1805 | |
1806 | // TODO(#140): shouldn't buffer up this entire array in memory, it'd be |
1807 | // great to defer parsing everything until later. |
1808 | fn array(&mut self) -> Result<(Span, Vec<Value<'a>>), Error> { |
1809 | let mut ret = Vec::new(); |
1810 | |
1811 | let intermediate = |me: &mut Deserializer<'_>| { |
1812 | loop { |
1813 | me.eat_whitespace()?; |
1814 | if !me.eat(Token::Newline)? && !me.eat_comment()? { |
1815 | break; |
1816 | } |
1817 | } |
1818 | Ok(()) |
1819 | }; |
1820 | |
1821 | loop { |
1822 | intermediate(self)?; |
1823 | if let Some(span) = self.eat_spanned(Token::RightBracket)? { |
1824 | return Ok((span, ret)); |
1825 | } |
1826 | let value = self.value()?; |
1827 | ret.push(value); |
1828 | intermediate(self)?; |
1829 | if !self.eat(Token::Comma)? { |
1830 | break; |
1831 | } |
1832 | } |
1833 | intermediate(self)?; |
1834 | let span = self.expect_spanned(Token::RightBracket)?; |
1835 | Ok((span, ret)) |
1836 | } |
1837 | |
1838 | fn table_key(&mut self) -> Result<(Span, Cow<'a, str>), Error> { |
1839 | self.tokens.table_key().map_err(|e| self.token_error(e)) |
1840 | } |
1841 | |
1842 | fn dotted_key(&mut self) -> Result<Vec<(Span, Cow<'a, str>)>, Error> { |
1843 | let mut result = vec![self.table_key()?]; |
1844 | self.eat_whitespace()?; |
1845 | while self.eat(Token::Period)? { |
1846 | self.eat_whitespace()?; |
1847 | result.push(self.table_key()?); |
1848 | self.eat_whitespace()?; |
1849 | } |
1850 | Ok(result) |
1851 | } |
1852 | |
1853 | /// Stores a value in the appropriate hierarchical structure positioned based on the dotted key. |
1854 | /// |
1855 | /// Given the following definition: `multi.part.key = "value"`, `multi` and `part` are |
1856 | /// intermediate parts which are mapped to the relevant fields in the deserialized type's data |
1857 | /// hierarchy. |
1858 | /// |
1859 | /// # Parameters |
1860 | /// |
1861 | /// * `key_parts`: Each segment of the dotted key, e.g. `part.one` maps to |
1862 | /// `vec![Cow::Borrowed("part"), Cow::Borrowed("one")].` |
1863 | /// * `value`: The parsed value. |
1864 | /// * `values`: The `Vec` to store the value in. |
1865 | fn add_dotted_key( |
1866 | &self, |
1867 | mut key_parts: Vec<(Span, Cow<'a, str>)>, |
1868 | value: Value<'a>, |
1869 | values: &mut Vec<TablePair<'a>>, |
1870 | ) -> Result<(), Error> { |
1871 | let key = key_parts.remove(0); |
1872 | if key_parts.is_empty() { |
1873 | values.push((key, value)); |
1874 | return Ok(()); |
1875 | } |
1876 | match values.iter_mut().find(|&&mut (ref k, _)| *k.1 == key.1) { |
1877 | Some(&mut ( |
1878 | _, |
1879 | Value { |
1880 | e: E::DottedTable(ref mut v), |
1881 | .. |
1882 | }, |
1883 | )) => { |
1884 | return self.add_dotted_key(key_parts, value, v); |
1885 | } |
1886 | Some(&mut (_, Value { start, .. })) => { |
1887 | return Err(self.error(start, ErrorKind::DottedKeyInvalidType)); |
1888 | } |
1889 | None => {} |
1890 | } |
1891 | // The start/end value is somewhat misleading here. |
1892 | let table_values = Value { |
1893 | e: E::DottedTable(Vec::new()), |
1894 | start: value.start, |
1895 | end: value.end, |
1896 | }; |
1897 | values.push((key, table_values)); |
1898 | let last_i = values.len() - 1; |
1899 | if let ( |
1900 | _, |
1901 | Value { |
1902 | e: E::DottedTable(ref mut v), |
1903 | .. |
1904 | }, |
1905 | ) = values[last_i] |
1906 | { |
1907 | self.add_dotted_key(key_parts, value, v)?; |
1908 | } |
1909 | Ok(()) |
1910 | } |
1911 | |
1912 | fn eat_whitespace(&mut self) -> Result<(), Error> { |
1913 | self.tokens |
1914 | .eat_whitespace() |
1915 | .map_err(|e| self.token_error(e)) |
1916 | } |
1917 | |
1918 | fn eat_comment(&mut self) -> Result<bool, Error> { |
1919 | self.tokens.eat_comment().map_err(|e| self.token_error(e)) |
1920 | } |
1921 | |
1922 | fn eat_newline_or_eof(&mut self) -> Result<(), Error> { |
1923 | self.tokens |
1924 | .eat_newline_or_eof() |
1925 | .map_err(|e| self.token_error(e)) |
1926 | } |
1927 | |
1928 | fn eat(&mut self, expected: Token<'a>) -> Result<bool, Error> { |
1929 | self.tokens.eat(expected).map_err(|e| self.token_error(e)) |
1930 | } |
1931 | |
1932 | fn eat_spanned(&mut self, expected: Token<'a>) -> Result<Option<Span>, Error> { |
1933 | self.tokens |
1934 | .eat_spanned(expected) |
1935 | .map_err(|e| self.token_error(e)) |
1936 | } |
1937 | |
1938 | fn expect(&mut self, expected: Token<'a>) -> Result<(), Error> { |
1939 | self.tokens |
1940 | .expect(expected) |
1941 | .map_err(|e| self.token_error(e)) |
1942 | } |
1943 | |
1944 | fn expect_spanned(&mut self, expected: Token<'a>) -> Result<Span, Error> { |
1945 | self.tokens |
1946 | .expect_spanned(expected) |
1947 | .map_err(|e| self.token_error(e)) |
1948 | } |
1949 | |
1950 | fn next(&mut self) -> Result<Option<(Span, Token<'a>)>, Error> { |
1951 | self.tokens.next().map_err(|e| self.token_error(e)) |
1952 | } |
1953 | |
1954 | fn peek(&mut self) -> Result<Option<(Span, Token<'a>)>, Error> { |
1955 | self.tokens.peek().map_err(|e| self.token_error(e)) |
1956 | } |
1957 | |
1958 | fn eof(&self) -> Error { |
1959 | self.error(self.input.len(), ErrorKind::UnexpectedEof) |
1960 | } |
1961 | |
1962 | fn token_error(&self, error: TokenError) -> Error { |
1963 | match error { |
1964 | TokenError::InvalidCharInString(at, ch) => { |
1965 | self.error(at, ErrorKind::InvalidCharInString(ch)) |
1966 | } |
1967 | TokenError::InvalidEscape(at, ch) => self.error(at, ErrorKind::InvalidEscape(ch)), |
1968 | TokenError::InvalidEscapeValue(at, v) => { |
1969 | self.error(at, ErrorKind::InvalidEscapeValue(v)) |
1970 | } |
1971 | TokenError::InvalidHexEscape(at, ch) => self.error(at, ErrorKind::InvalidHexEscape(ch)), |
1972 | TokenError::NewlineInString(at) => self.error(at, ErrorKind::NewlineInString), |
1973 | TokenError::Unexpected(at, ch) => self.error(at, ErrorKind::Unexpected(ch)), |
1974 | TokenError::UnterminatedString(at) => self.error(at, ErrorKind::UnterminatedString), |
1975 | TokenError::NewlineInTableKey(at) => self.error(at, ErrorKind::NewlineInTableKey), |
1976 | TokenError::Wanted { |
1977 | at, |
1978 | expected, |
1979 | found, |
1980 | } => self.error(at, ErrorKind::Wanted { expected, found }), |
1981 | TokenError::MultilineStringKey(at) => self.error(at, ErrorKind::MultilineStringKey), |
1982 | } |
1983 | } |
1984 | |
1985 | fn error(&self, at: usize, kind: ErrorKind) -> Error { |
1986 | let mut err = Error::from_kind(Some(at), kind); |
1987 | err.fix_linecol(|at| self.to_linecol(at)); |
1988 | err |
1989 | } |
1990 | |
1991 | /// Converts a byte offset from an error message to a (line, column) pair |
1992 | /// |
1993 | /// All indexes are 0-based. |
1994 | fn to_linecol(&self, offset: usize) -> (usize, usize) { |
1995 | let mut cur = 0; |
1996 | // Use split_terminator instead of lines so that if there is a `\r`, |
1997 | // it is included in the offset calculation. The `+1` values below |
1998 | // account for the `\n`. |
1999 | for (i, line) in self.input.split_terminator(' \n' ).enumerate() { |
2000 | if cur + line.len() + 1 > offset { |
2001 | return (i, offset - cur); |
2002 | } |
2003 | cur += line.len() + 1; |
2004 | } |
2005 | (self.input.lines().count(), 0) |
2006 | } |
2007 | } |
2008 | |
2009 | impl Error { |
2010 | /// Produces a (line, column) pair of the position of the error if available |
2011 | /// |
2012 | /// All indexes are 0-based. |
2013 | pub fn line_col(&self) -> Option<(usize, usize)> { |
2014 | self.inner.line.map(|line| (line, self.inner.col)) |
2015 | } |
2016 | |
2017 | fn from_kind(at: Option<usize>, kind: ErrorKind) -> Error { |
2018 | Error { |
2019 | inner: Box::new(ErrorInner { |
2020 | kind, |
2021 | line: None, |
2022 | col: 0, |
2023 | at, |
2024 | message: String::new(), |
2025 | key: Vec::new(), |
2026 | }), |
2027 | } |
2028 | } |
2029 | |
2030 | fn custom(at: Option<usize>, s: String) -> Error { |
2031 | Error { |
2032 | inner: Box::new(ErrorInner { |
2033 | kind: ErrorKind::Custom, |
2034 | line: None, |
2035 | col: 0, |
2036 | at, |
2037 | message: s, |
2038 | key: Vec::new(), |
2039 | }), |
2040 | } |
2041 | } |
2042 | |
2043 | pub(crate) fn add_key_context(&mut self, key: &str) { |
2044 | self.inner.key.insert(0, key.to_string()); |
2045 | } |
2046 | |
2047 | fn fix_offset<F>(&mut self, f: F) |
2048 | where |
2049 | F: FnOnce() -> Option<usize>, |
2050 | { |
2051 | // An existing offset is always better positioned than anything we |
2052 | // might want to add later. |
2053 | if self.inner.at.is_none() { |
2054 | self.inner.at = f(); |
2055 | } |
2056 | } |
2057 | |
2058 | fn fix_linecol<F>(&mut self, f: F) |
2059 | where |
2060 | F: FnOnce(usize) -> (usize, usize), |
2061 | { |
2062 | if let Some(at) = self.inner.at { |
2063 | let (line, col) = f(at); |
2064 | self.inner.line = Some(line); |
2065 | self.inner.col = col; |
2066 | } |
2067 | } |
2068 | } |
2069 | |
2070 | impl std::convert::From<Error> for std::io::Error { |
2071 | fn from(e: Error) -> Self { |
2072 | std::io::Error::new(kind:std::io::ErrorKind::InvalidData, error:e.to_string()) |
2073 | } |
2074 | } |
2075 | |
2076 | impl fmt::Display for Error { |
2077 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
2078 | match &self.inner.kind { |
2079 | ErrorKind::UnexpectedEof => "unexpected eof encountered" .fmt(f)?, |
2080 | ErrorKind::InvalidCharInString(c) => write!( |
2081 | f, |
2082 | "invalid character in string: ` {}`" , |
2083 | c.escape_default().collect::<String>() |
2084 | )?, |
2085 | ErrorKind::InvalidEscape(c) => write!( |
2086 | f, |
2087 | "invalid escape character in string: ` {}`" , |
2088 | c.escape_default().collect::<String>() |
2089 | )?, |
2090 | ErrorKind::InvalidHexEscape(c) => write!( |
2091 | f, |
2092 | "invalid hex escape character in string: ` {}`" , |
2093 | c.escape_default().collect::<String>() |
2094 | )?, |
2095 | ErrorKind::InvalidEscapeValue(c) => write!(f, "invalid escape value: ` {}`" , c)?, |
2096 | ErrorKind::NewlineInString => "newline in string found" .fmt(f)?, |
2097 | ErrorKind::Unexpected(ch) => write!( |
2098 | f, |
2099 | "unexpected character found: ` {}`" , |
2100 | ch.escape_default().collect::<String>() |
2101 | )?, |
2102 | ErrorKind::UnterminatedString => "unterminated string" .fmt(f)?, |
2103 | ErrorKind::NewlineInTableKey => "found newline in table key" .fmt(f)?, |
2104 | ErrorKind::Wanted { expected, found } => { |
2105 | write!(f, "expected {}, found {}" , expected, found)? |
2106 | } |
2107 | ErrorKind::NumberInvalid => "invalid number" .fmt(f)?, |
2108 | ErrorKind::DateInvalid => "invalid date" .fmt(f)?, |
2109 | ErrorKind::DuplicateTable(ref s) => { |
2110 | write!(f, "redefinition of table ` {}`" , s)?; |
2111 | } |
2112 | ErrorKind::RedefineAsArray => "table redefined as array" .fmt(f)?, |
2113 | ErrorKind::EmptyTableKey => "empty table key found" .fmt(f)?, |
2114 | ErrorKind::MultilineStringKey => "multiline strings are not allowed for key" .fmt(f)?, |
2115 | ErrorKind::Custom => self.inner.message.fmt(f)?, |
2116 | ErrorKind::ExpectedTuple(l) => write!(f, "expected table with length {}" , l)?, |
2117 | ErrorKind::ExpectedTupleIndex { |
2118 | expected, |
2119 | ref found, |
2120 | } => write!(f, "expected table key ` {}`, but was ` {}`" , expected, found)?, |
2121 | ErrorKind::ExpectedEmptyTable => "expected empty table" .fmt(f)?, |
2122 | ErrorKind::DottedKeyInvalidType => { |
2123 | "dotted key attempted to extend non-table type" .fmt(f)? |
2124 | } |
2125 | ErrorKind::UnexpectedKeys { |
2126 | ref keys, |
2127 | available, |
2128 | } => write!( |
2129 | f, |
2130 | "unexpected keys in table: ` {:?}`, available keys: ` {:?}`" , |
2131 | keys, available |
2132 | )?, |
2133 | ErrorKind::UnquotedString => write!( |
2134 | f, |
2135 | "invalid TOML value, did you mean to use a quoted string?" |
2136 | )?, |
2137 | } |
2138 | |
2139 | if !self.inner.key.is_empty() { |
2140 | write!(f, " for key `" )?; |
2141 | for (i, k) in self.inner.key.iter().enumerate() { |
2142 | if i > 0 { |
2143 | write!(f, "." )?; |
2144 | } |
2145 | write!(f, " {}" , k)?; |
2146 | } |
2147 | write!(f, "`" )?; |
2148 | } |
2149 | |
2150 | if let Some(line) = self.inner.line { |
2151 | write!(f, " at line {} column {}" , line + 1, self.inner.col + 1)?; |
2152 | } |
2153 | |
2154 | Ok(()) |
2155 | } |
2156 | } |
2157 | |
2158 | impl error::Error for Error {} |
2159 | |
2160 | impl de::Error for Error { |
2161 | fn custom<T: fmt::Display>(msg: T) -> Error { |
2162 | Error::custom(at:None, s:msg.to_string()) |
2163 | } |
2164 | } |
2165 | |
2166 | enum Line<'a> { |
2167 | Table { |
2168 | at: usize, |
2169 | header: Header<'a>, |
2170 | array: bool, |
2171 | }, |
2172 | KeyValue(Vec<(Span, Cow<'a, str>)>, Value<'a>), |
2173 | } |
2174 | |
2175 | struct Header<'a> { |
2176 | first: bool, |
2177 | array: bool, |
2178 | require_newline_after_table: bool, |
2179 | tokens: Tokenizer<'a>, |
2180 | } |
2181 | |
2182 | impl<'a> Header<'a> { |
2183 | fn new(tokens: Tokenizer<'a>, array: bool, require_newline_after_table: bool) -> Header<'a> { |
2184 | Header { |
2185 | first: true, |
2186 | array, |
2187 | tokens, |
2188 | require_newline_after_table, |
2189 | } |
2190 | } |
2191 | |
2192 | fn next(&mut self) -> Result<Option<(Span, Cow<'a, str>)>, TokenError> { |
2193 | self.tokens.eat_whitespace()?; |
2194 | |
2195 | if self.first || self.tokens.eat(Token::Period)? { |
2196 | self.first = false; |
2197 | self.tokens.eat_whitespace()?; |
2198 | self.tokens.table_key().map(Some) |
2199 | } else { |
2200 | self.tokens.expect(Token::RightBracket)?; |
2201 | if self.array { |
2202 | self.tokens.expect(Token::RightBracket)?; |
2203 | } |
2204 | |
2205 | self.tokens.eat_whitespace()?; |
2206 | if self.require_newline_after_table && !self.tokens.eat_comment()? { |
2207 | self.tokens.eat_newline_or_eof()?; |
2208 | } |
2209 | Ok(None) |
2210 | } |
2211 | } |
2212 | } |
2213 | |
2214 | #[derive (Debug)] |
2215 | struct Value<'a> { |
2216 | e: E<'a>, |
2217 | start: usize, |
2218 | end: usize, |
2219 | } |
2220 | |
2221 | #[derive (Debug)] |
2222 | enum E<'a> { |
2223 | Integer(i64), |
2224 | Float(f64), |
2225 | Boolean(bool), |
2226 | String(Cow<'a, str>), |
2227 | Datetime(&'a str), |
2228 | Array(Vec<Value<'a>>), |
2229 | InlineTable(Vec<TablePair<'a>>), |
2230 | DottedTable(Vec<TablePair<'a>>), |
2231 | } |
2232 | |
2233 | impl<'a> E<'a> { |
2234 | fn type_name(&self) -> &'static str { |
2235 | match *self { |
2236 | E::String(..) => "string" , |
2237 | E::Integer(..) => "integer" , |
2238 | E::Float(..) => "float" , |
2239 | E::Boolean(..) => "boolean" , |
2240 | E::Datetime(..) => "datetime" , |
2241 | E::Array(..) => "array" , |
2242 | E::InlineTable(..) => "inline table" , |
2243 | E::DottedTable(..) => "dotted table" , |
2244 | } |
2245 | } |
2246 | } |
2247 | |