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