1use crate::tokens::{Error as TokenError, Span, Token, Tokenizer};
2use serde::de;
3use serde::de::IntoDeserializer;
4use std::borrow::Cow;
5use std::collections::{HashMap, HashSet};
6use std::error;
7use std::f64;
8use std::fmt::{self, Display};
9use std::iter;
10use std::str;
11use std::vec;
12
13type 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.
19pub fn from_slice<'de, T>(bytes: &'de [u8]) -> Result<T, crate::Error>
20where
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.
33pub fn from_str<'de, T>(s: &'de str) -> Result<T, crate::Error>
34where
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)]
42pub(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)]
53enum 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
145struct Deserializer<'a> {
146 input: &'a str,
147 tokens: Tokenizer<'a>,
148}
149
150impl<'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.
203fn 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.
226fn 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
239fn 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
246struct Table<'a> {
247 at: usize,
248 header: Vec<(Span, Cow<'a, str>)>,
249 values: Option<Vec<TablePair<'a>>>,
250 array: bool,
251}
252
253struct 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
268impl<'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
421impl<'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
482impl<'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
523struct StrDeserializer<'a> {
524 key: Cow<'a, str>,
525}
526
527impl<'a> StrDeserializer<'a> {
528 fn new(key: Cow<'a, str>) -> StrDeserializer<'a> {
529 StrDeserializer { key }
530 }
531}
532
533impl<'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
541impl<'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
561struct ValueDeserializer<'a> {
562 value: Value<'a>,
563 validate_struct_keys: bool,
564}
565
566impl<'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
580impl<'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
707impl<'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
715impl<'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
723impl<'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
731struct DottedTableDeserializer<'a> {
732 name: Cow<'a, str>,
733 value: Value<'a>,
734}
735
736impl<'de> de::EnumAccess<'de> for DottedTableDeserializer<'de> {
737 type Error = Box<Error>;
738 type Variant = TableEnumDeserializer<'de>;
739
740 fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
741 where
742 V: de::DeserializeSeed<'de>,
743 {
744 let (name: Cow<'_, str>, value: Value<'_>) = (self.name, self.value);
745 seed.deserialize(StrDeserializer::new(name))
746 .map(|val: ::Value| (val, TableEnumDeserializer { value }))
747 }
748}
749
750struct InlineTableDeserializer<'de> {
751 values: vec::IntoIter<TablePair<'de>>,
752 next_value: Option<Value<'de>>,
753 keys: HashSet<Cow<'de, str>>,
754}
755
756impl<'de> de::MapAccess<'de> for InlineTableDeserializer<'de> {
757 type Error = Box<Error>;
758
759 fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Box<Error>>
760 where
761 K: de::DeserializeSeed<'de>,
762 {
763 let ((span, key), value) = match self.values.next() {
764 Some(pair) => pair,
765 None => return Ok(None),
766 };
767 self.next_value = Some(value);
768 if !self.keys.insert(key.clone()) {
769 return Err(Error::from_kind(
770 Some(span.start),
771 ErrorKind::DuplicateKey(key.into_owned()),
772 ));
773 }
774 seed.deserialize(StrDeserializer::new(key)).map(Some)
775 }
776
777 fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Box<Error>>
778 where
779 V: de::DeserializeSeed<'de>,
780 {
781 let value = self.next_value.take().expect("Unable to read table values");
782 seed.deserialize(ValueDeserializer::new(value))
783 }
784}
785
786impl<'de> de::EnumAccess<'de> for InlineTableDeserializer<'de> {
787 type Error = Box<Error>;
788 type Variant = TableEnumDeserializer<'de>;
789
790 fn variant_seed<V>(mut self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
791 where
792 V: de::DeserializeSeed<'de>,
793 {
794 let (key, value) = match self.values.next() {
795 Some(pair) => pair,
796 None => {
797 return Err(Error::from_kind(
798 None, // FIXME: How do we get an offset here?
799 ErrorKind::Wanted {
800 expected: "table with exactly 1 entry",
801 found: "empty table",
802 },
803 ));
804 }
805 };
806
807 seed.deserialize(StrDeserializer::new(key.1))
808 .map(|val| (val, TableEnumDeserializer { value }))
809 }
810}
811
812/// Deserializes table values into enum variants.
813struct TableEnumDeserializer<'a> {
814 value: Value<'a>,
815}
816
817impl<'de> de::VariantAccess<'de> for TableEnumDeserializer<'de> {
818 type Error = Box<Error>;
819
820 fn unit_variant(self) -> Result<(), Self::Error> {
821 match self.value.e {
822 E::InlineTable(values) | E::DottedTable(values) => {
823 if values.is_empty() {
824 Ok(())
825 } else {
826 Err(Error::from_kind(
827 Some(self.value.start),
828 ErrorKind::ExpectedEmptyTable,
829 ))
830 }
831 }
832 e => Err(Error::from_kind(
833 Some(self.value.start),
834 ErrorKind::Wanted {
835 expected: "table",
836 found: e.type_name(),
837 },
838 )),
839 }
840 }
841
842 fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>
843 where
844 T: de::DeserializeSeed<'de>,
845 {
846 seed.deserialize(ValueDeserializer::new(self.value))
847 }
848
849 fn tuple_variant<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
850 where
851 V: de::Visitor<'de>,
852 {
853 match self.value.e {
854 E::InlineTable(values) | E::DottedTable(values) => {
855 let tuple_values = values
856 .into_iter()
857 .enumerate()
858 .map(|(index, (key, value))| match key.1.parse::<usize>() {
859 Ok(key_index) if key_index == index => Ok(value),
860 Ok(_) | Err(_) => Err(Error::from_kind(
861 Some(key.0.start),
862 ErrorKind::ExpectedTupleIndex {
863 expected: index,
864 found: key.1.to_string(),
865 },
866 )),
867 })
868 // Fold all values into a `Vec`, or return the first error.
869 .fold(Ok(Vec::with_capacity(len)), |result, value_result| {
870 result.and_then(move |mut tuple_values| match value_result {
871 Ok(value) => {
872 tuple_values.push(value);
873 Ok(tuple_values)
874 }
875 // `Result<de::Value, Self::Error>` to `Result<Vec<_>, Self::Error>`
876 Err(e) => Err(e),
877 })
878 })?;
879
880 if tuple_values.len() == len {
881 de::Deserializer::deserialize_seq(
882 ValueDeserializer::new(Value {
883 e: E::Array(tuple_values),
884 start: self.value.start,
885 end: self.value.end,
886 }),
887 visitor,
888 )
889 } else {
890 Err(Error::from_kind(
891 Some(self.value.start),
892 ErrorKind::ExpectedTuple(len),
893 ))
894 }
895 }
896 e => Err(Error::from_kind(
897 Some(self.value.start),
898 ErrorKind::Wanted {
899 expected: "table",
900 found: e.type_name(),
901 },
902 )),
903 }
904 }
905
906 fn struct_variant<V>(
907 self,
908 fields: &'static [&'static str],
909 visitor: V,
910 ) -> Result<V::Value, Self::Error>
911 where
912 V: de::Visitor<'de>,
913 {
914 de::Deserializer::deserialize_struct(
915 ValueDeserializer::new(self.value).with_struct_key_validation(),
916 "", // TODO: this should be the variant name
917 fields,
918 visitor,
919 )
920 }
921}
922
923impl<'a> Deserializer<'a> {
924 fn new(input: &'a str) -> Deserializer<'a> {
925 Deserializer {
926 tokens: Tokenizer::new(input),
927 input,
928 }
929 }
930
931 fn tables(&mut self) -> Result<Vec<Table<'a>>, Box<Error>> {
932 let mut tables = Vec::new();
933 let mut cur_table = Table {
934 at: 0,
935 header: Vec::new(),
936 values: None,
937 array: false,
938 };
939
940 while let Some(line) = self.line()? {
941 match line {
942 Line::Table {
943 at,
944 mut header,
945 array,
946 } => {
947 if !cur_table.header.is_empty() || cur_table.values.is_some() {
948 tables.push(cur_table);
949 }
950 cur_table = Table {
951 at,
952 header: Vec::new(),
953 values: Some(Vec::new()),
954 array,
955 };
956 loop {
957 let part = header.next().map_err(|e| self.token_error(e));
958 match part? {
959 Some(part) => cur_table.header.push(part),
960 None => break,
961 }
962 }
963 }
964 Line::KeyValue(key, value) => {
965 if cur_table.values.is_none() {
966 cur_table.values = Some(Vec::new());
967 }
968 self.add_dotted_key(key, value, cur_table.values.as_mut().unwrap())?;
969 }
970 }
971 }
972 if !cur_table.header.is_empty() || cur_table.values.is_some() {
973 tables.push(cur_table);
974 }
975 Ok(tables)
976 }
977
978 fn line(&mut self) -> Result<Option<Line<'a>>, Box<Error>> {
979 loop {
980 self.eat_whitespace();
981 if self.eat_comment()? {
982 continue;
983 }
984 if self.eat(Token::Newline)? {
985 continue;
986 }
987 break;
988 }
989
990 match self.peek()? {
991 Some((_, Token::LeftBracket)) => self.table_header().map(Some),
992 Some(_) => self.key_value().map(Some),
993 None => Ok(None),
994 }
995 }
996
997 fn table_header(&mut self) -> Result<Line<'a>, Box<Error>> {
998 let start = self.tokens.current();
999 self.expect(Token::LeftBracket)?;
1000 let array = self.eat(Token::LeftBracket)?;
1001 let ret = Header::new(self.tokens.clone(), array);
1002 self.tokens.skip_to_newline();
1003 Ok(Line::Table {
1004 at: start,
1005 header: ret,
1006 array,
1007 })
1008 }
1009
1010 fn key_value(&mut self) -> Result<Line<'a>, Box<Error>> {
1011 let key = self.dotted_key()?;
1012 self.eat_whitespace();
1013 self.expect(Token::Equals)?;
1014 self.eat_whitespace();
1015
1016 let value = self.value()?;
1017 self.eat_whitespace();
1018 if !self.eat_comment()? {
1019 self.eat_newline_or_eof()?;
1020 }
1021
1022 Ok(Line::KeyValue(key, value))
1023 }
1024
1025 fn value(&mut self) -> Result<Value<'a>, Box<Error>> {
1026 let at = self.tokens.current();
1027 let value = match self.next()? {
1028 Some((Span { start, end }, Token::String { val, .. })) => Value {
1029 e: E::String(val),
1030 start,
1031 end,
1032 },
1033 Some((Span { start, end }, Token::Keylike("true"))) => Value {
1034 e: E::Boolean(true),
1035 start,
1036 end,
1037 },
1038 Some((Span { start, end }, Token::Keylike("false"))) => Value {
1039 e: E::Boolean(false),
1040 start,
1041 end,
1042 },
1043 Some((span, Token::Keylike(key))) => self.parse_keylike(at, span, key)?,
1044 Some((span, Token::Plus)) => self.number_leading_plus(span)?,
1045 Some((Span { start, .. }, Token::LeftBrace)) => {
1046 self.inline_table().map(|(Span { end, .. }, table)| Value {
1047 e: E::InlineTable(table),
1048 start,
1049 end,
1050 })?
1051 }
1052 Some((Span { start, .. }, Token::LeftBracket)) => {
1053 self.array().map(|(Span { end, .. }, array)| Value {
1054 e: E::Array(array),
1055 start,
1056 end,
1057 })?
1058 }
1059 Some(token) => {
1060 return Err(self.error(
1061 at,
1062 ErrorKind::Wanted {
1063 expected: "a value",
1064 found: token.1.describe(),
1065 },
1066 ));
1067 }
1068 None => return Err(self.eof()),
1069 };
1070 Ok(value)
1071 }
1072
1073 fn parse_keylike(
1074 &mut self,
1075 at: usize,
1076 span: Span,
1077 key: &'a str,
1078 ) -> Result<Value<'a>, Box<Error>> {
1079 if key == "inf" || key == "nan" {
1080 return self.number(span, key);
1081 }
1082
1083 let first_char = key.chars().next().expect("key should not be empty here");
1084 match first_char {
1085 '-' | '0'..='9' => self.number(span, key),
1086 _ => Err(self.error(at, ErrorKind::UnquotedString)),
1087 }
1088 }
1089
1090 fn number(&mut self, Span { start, end }: Span, s: &'a str) -> Result<Value<'a>, Box<Error>> {
1091 let to_integer = |f| Value {
1092 e: E::Integer(f),
1093 start,
1094 end,
1095 };
1096 if let Some(s) = s.strip_prefix("0x") {
1097 self.integer(s, 16).map(to_integer)
1098 } else if let Some(s) = s.strip_prefix("0o") {
1099 self.integer(s, 8).map(to_integer)
1100 } else if let Some(s) = s.strip_prefix("0b") {
1101 self.integer(s, 2).map(to_integer)
1102 } else if s.contains('e') || s.contains('E') {
1103 self.float(s, None).map(|f| Value {
1104 e: E::Float(f),
1105 start,
1106 end,
1107 })
1108 } else if self.eat(Token::Period)? {
1109 let at = self.tokens.current();
1110 match self.next()? {
1111 Some((Span { start, end }, Token::Keylike(after))) => {
1112 self.float(s, Some(after)).map(|f| Value {
1113 e: E::Float(f),
1114 start,
1115 end,
1116 })
1117 }
1118 _ => Err(self.error(at, ErrorKind::NumberInvalid)),
1119 }
1120 } else if s == "inf" {
1121 Ok(Value {
1122 e: E::Float(f64::INFINITY),
1123 start,
1124 end,
1125 })
1126 } else if s == "-inf" {
1127 Ok(Value {
1128 e: E::Float(f64::NEG_INFINITY),
1129 start,
1130 end,
1131 })
1132 } else if s == "nan" {
1133 Ok(Value {
1134 e: E::Float(f64::NAN),
1135 start,
1136 end,
1137 })
1138 } else if s == "-nan" {
1139 Ok(Value {
1140 e: E::Float(-f64::NAN),
1141 start,
1142 end,
1143 })
1144 } else {
1145 self.integer(s, 10).map(to_integer)
1146 }
1147 }
1148
1149 fn number_leading_plus(&mut self, Span { start, .. }: Span) -> Result<Value<'a>, Box<Error>> {
1150 let start_token = self.tokens.current();
1151 match self.next()? {
1152 Some((Span { end, .. }, Token::Keylike(s))) => self.number(Span { start, end }, s),
1153 _ => Err(self.error(start_token, ErrorKind::NumberInvalid)),
1154 }
1155 }
1156
1157 fn integer(&self, s: &'a str, radix: u32) -> Result<i64, Box<Error>> {
1158 let allow_sign = radix == 10;
1159 let allow_leading_zeros = radix != 10;
1160 let (prefix, suffix) = self.parse_integer(s, allow_sign, allow_leading_zeros, radix)?;
1161 let start = self.tokens.substr_offset(s);
1162 if !suffix.is_empty() {
1163 return Err(self.error(start, ErrorKind::NumberInvalid));
1164 }
1165 i64::from_str_radix(prefix.replace('_', "").trim_start_matches('+'), radix)
1166 .map_err(|_e| self.error(start, ErrorKind::NumberInvalid))
1167 }
1168
1169 fn parse_integer(
1170 &self,
1171 s: &'a str,
1172 allow_sign: bool,
1173 allow_leading_zeros: bool,
1174 radix: u32,
1175 ) -> Result<(&'a str, &'a str), Box<Error>> {
1176 let start = self.tokens.substr_offset(s);
1177
1178 let mut first = true;
1179 let mut first_zero = false;
1180 let mut underscore = false;
1181 let mut end = s.len();
1182 for (i, c) in s.char_indices() {
1183 let at = i + start;
1184 if i == 0 && (c == '+' || c == '-') && allow_sign {
1185 continue;
1186 }
1187
1188 if c == '0' && first {
1189 first_zero = true;
1190 } else if c.is_digit(radix) {
1191 if !first && first_zero && !allow_leading_zeros {
1192 return Err(self.error(at, ErrorKind::NumberInvalid));
1193 }
1194 underscore = false;
1195 } else if c == '_' && first {
1196 return Err(self.error(at, ErrorKind::NumberInvalid));
1197 } else if c == '_' && !underscore {
1198 underscore = true;
1199 } else {
1200 end = i;
1201 break;
1202 }
1203 first = false;
1204 }
1205 if first || underscore {
1206 return Err(self.error(start, ErrorKind::NumberInvalid));
1207 }
1208 Ok((&s[..end], &s[end..]))
1209 }
1210
1211 fn float(&mut self, s: &'a str, after_decimal: Option<&'a str>) -> Result<f64, Box<Error>> {
1212 let (integral, mut suffix) = self.parse_integer(s, true, false, 10)?;
1213 let start = self.tokens.substr_offset(integral);
1214
1215 let mut fraction = None;
1216 if let Some(after) = after_decimal {
1217 if !suffix.is_empty() {
1218 return Err(self.error(start, ErrorKind::NumberInvalid));
1219 }
1220 let (a, b) = self.parse_integer(after, false, true, 10)?;
1221 fraction = Some(a);
1222 suffix = b;
1223 }
1224
1225 let mut exponent = None;
1226 if suffix.starts_with('e') || suffix.starts_with('E') {
1227 let (a, b) = if suffix.len() == 1 {
1228 self.eat(Token::Plus)?;
1229 match self.next()? {
1230 Some((_, Token::Keylike(s))) => self.parse_integer(s, false, true, 10)?,
1231 _ => return Err(self.error(start, ErrorKind::NumberInvalid)),
1232 }
1233 } else {
1234 self.parse_integer(&suffix[1..], true, true, 10)?
1235 };
1236 if !b.is_empty() {
1237 return Err(self.error(start, ErrorKind::NumberInvalid));
1238 }
1239 exponent = Some(a);
1240 } else if !suffix.is_empty() {
1241 return Err(self.error(start, ErrorKind::NumberInvalid));
1242 }
1243
1244 let mut number = integral
1245 .trim_start_matches('+')
1246 .chars()
1247 .filter(|c| *c != '_')
1248 .collect::<String>();
1249 if let Some(fraction) = fraction {
1250 number.push('.');
1251 number.extend(fraction.chars().filter(|c| *c != '_'));
1252 }
1253 if let Some(exponent) = exponent {
1254 number.push('E');
1255 number.extend(exponent.chars().filter(|c| *c != '_'));
1256 }
1257 number
1258 .parse()
1259 .map_err(|_e| self.error(start, ErrorKind::NumberInvalid))
1260 .and_then(|n: f64| {
1261 if n.is_finite() {
1262 Ok(n)
1263 } else {
1264 Err(self.error(start, ErrorKind::NumberInvalid))
1265 }
1266 })
1267 }
1268
1269 // TODO(#140): shouldn't buffer up this entire table in memory, it'd be
1270 // great to defer parsing everything until later.
1271 fn inline_table(&mut self) -> Result<(Span, Vec<TablePair<'a>>), Box<Error>> {
1272 let mut ret = Vec::new();
1273 self.eat_whitespace();
1274 if let Some(span) = self.eat_spanned(Token::RightBrace)? {
1275 return Ok((span, ret));
1276 }
1277 loop {
1278 let key = self.dotted_key()?;
1279 self.eat_whitespace();
1280 self.expect(Token::Equals)?;
1281 self.eat_whitespace();
1282 let value = self.value()?;
1283 self.add_dotted_key(key, value, &mut ret)?;
1284
1285 self.eat_whitespace();
1286 if let Some(span) = self.eat_spanned(Token::RightBrace)? {
1287 return Ok((span, ret));
1288 }
1289 self.expect(Token::Comma)?;
1290 self.eat_whitespace();
1291 }
1292 }
1293
1294 // TODO(#140): shouldn't buffer up this entire array in memory, it'd be
1295 // great to defer parsing everything until later.
1296 fn array(&mut self) -> Result<(Span, Vec<Value<'a>>), Box<Error>> {
1297 let mut ret = Vec::new();
1298
1299 let intermediate = |me: &mut Deserializer| -> Result<(), Box<Error>> {
1300 loop {
1301 me.eat_whitespace();
1302 if !me.eat(Token::Newline)? && !me.eat_comment()? {
1303 break;
1304 }
1305 }
1306 Ok(())
1307 };
1308
1309 loop {
1310 intermediate(self)?;
1311 if let Some(span) = self.eat_spanned(Token::RightBracket)? {
1312 return Ok((span, ret));
1313 }
1314 let value = self.value()?;
1315 ret.push(value);
1316 intermediate(self)?;
1317 if !self.eat(Token::Comma)? {
1318 break;
1319 }
1320 }
1321 intermediate(self)?;
1322 let span = self.expect_spanned(Token::RightBracket)?;
1323 Ok((span, ret))
1324 }
1325
1326 fn table_key(&mut self) -> Result<(Span, Cow<'a, str>), Box<Error>> {
1327 self.tokens.table_key().map_err(|e| self.token_error(e))
1328 }
1329
1330 fn dotted_key(&mut self) -> Result<Vec<(Span, Cow<'a, str>)>, Box<Error>> {
1331 let mut result = Vec::new();
1332 result.push(self.table_key()?);
1333 self.eat_whitespace();
1334 while self.eat(Token::Period)? {
1335 self.eat_whitespace();
1336 result.push(self.table_key()?);
1337 self.eat_whitespace();
1338 }
1339 Ok(result)
1340 }
1341
1342 /// Stores a value in the appropriate hierarchical structure positioned based on the dotted key.
1343 ///
1344 /// Given the following definition: `multi.part.key = "value"`, `multi` and `part` are
1345 /// intermediate parts which are mapped to the relevant fields in the deserialized type's data
1346 /// hierarchy.
1347 ///
1348 /// # Parameters
1349 ///
1350 /// * `key_parts`: Each segment of the dotted key, e.g. `part.one` maps to
1351 /// `vec![Cow::Borrowed("part"), Cow::Borrowed("one")].`
1352 /// * `value`: The parsed value.
1353 /// * `values`: The `Vec` to store the value in.
1354 fn add_dotted_key(
1355 &self,
1356 mut key_parts: Vec<(Span, Cow<'a, str>)>,
1357 value: Value<'a>,
1358 values: &mut Vec<TablePair<'a>>,
1359 ) -> Result<(), Box<Error>> {
1360 let key = key_parts.remove(0);
1361 if key_parts.is_empty() {
1362 values.push((key, value));
1363 return Ok(());
1364 }
1365 match values.iter_mut().find(|&&mut (ref k, _)| *k.1 == key.1) {
1366 Some(&mut (
1367 _,
1368 Value {
1369 e: E::DottedTable(ref mut v),
1370 ..
1371 },
1372 )) => {
1373 return self.add_dotted_key(key_parts, value, v);
1374 }
1375 Some(&mut (_, Value { start, .. })) => {
1376 return Err(self.error(start, ErrorKind::DottedKeyInvalidType));
1377 }
1378 None => {}
1379 }
1380 // The start/end value is somewhat misleading here.
1381 let table_values = Value {
1382 e: E::DottedTable(Vec::new()),
1383 start: value.start,
1384 end: value.end,
1385 };
1386 values.push((key, table_values));
1387 let last_i = values.len() - 1;
1388 if let (
1389 _,
1390 Value {
1391 e: E::DottedTable(ref mut v),
1392 ..
1393 },
1394 ) = values[last_i]
1395 {
1396 self.add_dotted_key(key_parts, value, v)?;
1397 }
1398 Ok(())
1399 }
1400
1401 fn eat_whitespace(&mut self) {
1402 self.tokens.eat_whitespace();
1403 }
1404
1405 fn eat_comment(&mut self) -> Result<bool, Box<Error>> {
1406 self.tokens.eat_comment().map_err(|e| self.token_error(e))
1407 }
1408
1409 fn eat_newline_or_eof(&mut self) -> Result<(), Box<Error>> {
1410 self.tokens
1411 .eat_newline_or_eof()
1412 .map_err(|e| self.token_error(e))
1413 }
1414
1415 fn eat(&mut self, expected: Token<'a>) -> Result<bool, Box<Error>> {
1416 self.tokens.eat(expected).map_err(|e| self.token_error(e))
1417 }
1418
1419 fn eat_spanned(&mut self, expected: Token<'a>) -> Result<Option<Span>, Box<Error>> {
1420 self.tokens
1421 .eat_spanned(expected)
1422 .map_err(|e| self.token_error(e))
1423 }
1424
1425 fn expect(&mut self, expected: Token<'a>) -> Result<(), Box<Error>> {
1426 self.tokens
1427 .expect(expected)
1428 .map_err(|e| self.token_error(e))
1429 }
1430
1431 fn expect_spanned(&mut self, expected: Token<'a>) -> Result<Span, Box<Error>> {
1432 self.tokens
1433 .expect_spanned(expected)
1434 .map_err(|e| self.token_error(e))
1435 }
1436
1437 fn next(&mut self) -> Result<Option<(Span, Token<'a>)>, Box<Error>> {
1438 self.tokens.next().map_err(|e| self.token_error(e))
1439 }
1440
1441 fn peek(&mut self) -> Result<Option<(Span, Token<'a>)>, Box<Error>> {
1442 self.tokens.peek().map_err(|e| self.token_error(e))
1443 }
1444
1445 fn eof(&self) -> Box<Error> {
1446 self.error(self.input.len(), ErrorKind::UnexpectedEof)
1447 }
1448
1449 fn token_error(&self, error: TokenError) -> Box<Error> {
1450 match error {
1451 TokenError::InvalidCharInString(at, ch) => {
1452 self.error(at, ErrorKind::InvalidCharInString(ch))
1453 }
1454 TokenError::InvalidEscape(at, ch) => self.error(at, ErrorKind::InvalidEscape(ch)),
1455 TokenError::InvalidEscapeValue(at, v) => {
1456 self.error(at, ErrorKind::InvalidEscapeValue(v))
1457 }
1458 TokenError::InvalidHexEscape(at, ch) => self.error(at, ErrorKind::InvalidHexEscape(ch)),
1459 TokenError::NewlineInString(at) => self.error(at, ErrorKind::NewlineInString),
1460 TokenError::Unexpected(at, ch) => self.error(at, ErrorKind::Unexpected(ch)),
1461 TokenError::UnterminatedString(at) => self.error(at, ErrorKind::UnterminatedString),
1462 TokenError::NewlineInTableKey(at) => self.error(at, ErrorKind::NewlineInTableKey),
1463 TokenError::Wanted {
1464 at,
1465 expected,
1466 found,
1467 } => self.error(at, ErrorKind::Wanted { expected, found }),
1468 TokenError::MultilineStringKey(at) => self.error(at, ErrorKind::MultilineStringKey),
1469 }
1470 }
1471
1472 fn error(&self, at: usize, kind: ErrorKind) -> Box<Error> {
1473 let mut err = Error::from_kind(Some(at), kind);
1474 err.fix_linecol(|at| self.to_linecol(at));
1475 err
1476 }
1477
1478 /// Converts a byte offset from an error message to a (line, column) pair
1479 ///
1480 /// All indexes are 0-based.
1481 fn to_linecol(&self, offset: usize) -> (usize, usize) {
1482 let mut cur = 0;
1483 // Use split_terminator instead of lines so that if there is a `\r`, it
1484 // is included in the offset calculation. The `+1` values below account
1485 // for the `\n`.
1486 for (i, line) in self.input.split_terminator('\n').enumerate() {
1487 if cur + line.len() + 1 > offset {
1488 return (i, offset - cur);
1489 }
1490 cur += line.len() + 1;
1491 }
1492 (self.input.lines().count(), 0)
1493 }
1494}
1495
1496impl Error {
1497 pub(crate) fn line_col(&self) -> Option<(usize, usize)> {
1498 self.line.map(|line| (line, self.col))
1499 }
1500
1501 fn from_kind(at: Option<usize>, kind: ErrorKind) -> Box<Self> {
1502 Box::new(Error {
1503 kind,
1504 line: None,
1505 col: 0,
1506 at,
1507 message: String::new(),
1508 key: Vec::new(),
1509 })
1510 }
1511
1512 fn custom(at: Option<usize>, s: String) -> Box<Self> {
1513 Box::new(Error {
1514 kind: ErrorKind::Custom,
1515 line: None,
1516 col: 0,
1517 at,
1518 message: s,
1519 key: Vec::new(),
1520 })
1521 }
1522
1523 pub(crate) fn add_key_context(&mut self, key: &str) {
1524 self.key.insert(0, key.to_string());
1525 }
1526
1527 fn fix_offset<F>(&mut self, f: F)
1528 where
1529 F: FnOnce() -> Option<usize>,
1530 {
1531 // An existing offset is always better positioned than anything we might
1532 // want to add later.
1533 if self.at.is_none() {
1534 self.at = f();
1535 }
1536 }
1537
1538 fn fix_linecol<F>(&mut self, f: F)
1539 where
1540 F: FnOnce(usize) -> (usize, usize),
1541 {
1542 if let Some(at) = self.at {
1543 let (line, col) = f(at);
1544 self.line = Some(line);
1545 self.col = col;
1546 }
1547 }
1548}
1549
1550impl std::convert::From<Error> for std::io::Error {
1551 fn from(e: Error) -> Self {
1552 std::io::Error::new(kind:std::io::ErrorKind::InvalidData, error:e.to_string())
1553 }
1554}
1555
1556impl Display for Error {
1557 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1558 match &self.kind {
1559 ErrorKind::UnexpectedEof => "unexpected eof encountered".fmt(f)?,
1560 ErrorKind::InvalidCharInString(c) => write!(
1561 f,
1562 "invalid character in string: `{}`",
1563 c.escape_default().collect::<String>()
1564 )?,
1565 ErrorKind::InvalidEscape(c) => write!(
1566 f,
1567 "invalid escape character in string: `{}`",
1568 c.escape_default().collect::<String>()
1569 )?,
1570 ErrorKind::InvalidHexEscape(c) => write!(
1571 f,
1572 "invalid hex escape character in string: `{}`",
1573 c.escape_default().collect::<String>()
1574 )?,
1575 ErrorKind::InvalidEscapeValue(c) => write!(f, "invalid escape value: `{}`", c)?,
1576 ErrorKind::NewlineInString => "newline in string found".fmt(f)?,
1577 ErrorKind::Unexpected(ch) => write!(
1578 f,
1579 "unexpected character found: `{}`",
1580 ch.escape_default().collect::<String>()
1581 )?,
1582 ErrorKind::UnterminatedString => "unterminated string".fmt(f)?,
1583 ErrorKind::NewlineInTableKey => "found newline in table key".fmt(f)?,
1584 ErrorKind::Wanted { expected, found } => {
1585 write!(f, "expected {}, found {}", expected, found)?;
1586 }
1587 ErrorKind::NumberInvalid => "invalid number".fmt(f)?,
1588 ErrorKind::DuplicateTable(ref s) => {
1589 write!(f, "redefinition of table `{}`", s)?;
1590 }
1591 ErrorKind::DuplicateKey(ref s) => {
1592 write!(f, "duplicate key: `{}`", s)?;
1593 }
1594 ErrorKind::RedefineAsArray => "table redefined as array".fmt(f)?,
1595 ErrorKind::MultilineStringKey => "multiline strings are not allowed for key".fmt(f)?,
1596 ErrorKind::Custom => self.message.fmt(f)?,
1597 ErrorKind::ExpectedTuple(l) => write!(f, "expected table with length {}", l)?,
1598 ErrorKind::ExpectedTupleIndex {
1599 expected,
1600 ref found,
1601 } => write!(f, "expected table key `{}`, but was `{}`", expected, found)?,
1602 ErrorKind::ExpectedEmptyTable => "expected empty table".fmt(f)?,
1603 ErrorKind::DottedKeyInvalidType => {
1604 "dotted key attempted to extend non-table type".fmt(f)?;
1605 }
1606 ErrorKind::UnexpectedKeys {
1607 ref keys,
1608 available,
1609 } => write!(
1610 f,
1611 "unexpected keys in table: `{:?}`, available keys: `{:?}`",
1612 keys, available
1613 )?,
1614 ErrorKind::UnquotedString => write!(
1615 f,
1616 "invalid TOML value, did you mean to use a quoted string?"
1617 )?,
1618 }
1619
1620 if !self.key.is_empty() {
1621 write!(f, " for key `")?;
1622 for (i, k) in self.key.iter().enumerate() {
1623 if i > 0 {
1624 write!(f, ".")?;
1625 }
1626 write!(f, "{}", k)?;
1627 }
1628 write!(f, "`")?;
1629 }
1630
1631 if let Some(line) = self.line {
1632 write!(f, " at line {} column {}", line + 1, self.col + 1)?;
1633 }
1634
1635 Ok(())
1636 }
1637}
1638
1639impl error::Error for Error {}
1640
1641impl de::Error for Box<Error> {
1642 fn custom<T: Display>(msg: T) -> Self {
1643 Error::custom(at:None, s:msg.to_string())
1644 }
1645}
1646
1647enum Line<'a> {
1648 Table {
1649 at: usize,
1650 header: Header<'a>,
1651 array: bool,
1652 },
1653 KeyValue(Vec<(Span, Cow<'a, str>)>, Value<'a>),
1654}
1655
1656struct Header<'a> {
1657 first: bool,
1658 array: bool,
1659 tokens: Tokenizer<'a>,
1660}
1661
1662impl<'a> Header<'a> {
1663 fn new(tokens: Tokenizer<'a>, array: bool) -> Header<'a> {
1664 Header {
1665 first: true,
1666 array,
1667 tokens,
1668 }
1669 }
1670
1671 fn next(&mut self) -> Result<Option<(Span, Cow<'a, str>)>, TokenError> {
1672 self.tokens.eat_whitespace();
1673
1674 if self.first || self.tokens.eat(Token::Period)? {
1675 self.first = false;
1676 self.tokens.eat_whitespace();
1677 self.tokens.table_key().map(Some)
1678 } else {
1679 self.tokens.expect(Token::RightBracket)?;
1680 if self.array {
1681 self.tokens.expect(Token::RightBracket)?;
1682 }
1683
1684 self.tokens.eat_whitespace();
1685 if !self.tokens.eat_comment()? {
1686 self.tokens.eat_newline_or_eof()?;
1687 }
1688 Ok(None)
1689 }
1690 }
1691}
1692
1693#[derive(Debug)]
1694struct Value<'a> {
1695 e: E<'a>,
1696 start: usize,
1697 end: usize,
1698}
1699
1700#[derive(Debug)]
1701enum E<'a> {
1702 Integer(i64),
1703 Float(f64),
1704 Boolean(bool),
1705 String(Cow<'a, str>),
1706 Array(Vec<Value<'a>>),
1707 InlineTable(Vec<TablePair<'a>>),
1708 DottedTable(Vec<TablePair<'a>>),
1709}
1710
1711impl<'a> E<'a> {
1712 fn type_name(&self) -> &'static str {
1713 match *self {
1714 E::String(..) => "string",
1715 E::Integer(..) => "integer",
1716 E::Float(..) => "float",
1717 E::Boolean(..) => "boolean",
1718 E::Array(..) => "array",
1719 E::InlineTable(..) => "inline table",
1720 E::DottedTable(..) => "dotted table",
1721 }
1722 }
1723}
1724