2017-01-29 18:53:20 -06:00
|
|
|
//! Deserializing TOML into Rust structures.
|
|
|
|
//!
|
|
|
|
//! This module contains all the Serde support for deserializing TOML documents
|
|
|
|
//! into Rust structures. Note that some top-level functions here are also
|
|
|
|
//! provided at the top of the crate.
|
|
|
|
|
|
|
|
use std::borrow::Cow;
|
2019-10-28 10:05:13 -05:00
|
|
|
use std::collections::HashMap;
|
2017-01-29 18:53:20 -06:00
|
|
|
use std::error;
|
2018-07-10 18:27:58 -05:00
|
|
|
use std::f64;
|
2017-01-29 18:53:20 -06:00
|
|
|
use std::fmt;
|
2019-10-28 09:01:23 -05:00
|
|
|
use std::iter;
|
2019-09-05 08:38:54 -05:00
|
|
|
use std::marker::PhantomData;
|
2017-01-29 18:53:20 -06:00
|
|
|
use std::str;
|
|
|
|
use std::vec;
|
|
|
|
|
|
|
|
use serde::de;
|
2018-05-06 21:56:25 -05:00
|
|
|
use serde::de::value::BorrowedStrDeserializer;
|
2018-12-17 19:45:35 -06:00
|
|
|
use serde::de::IntoDeserializer;
|
2017-01-29 18:53:20 -06:00
|
|
|
|
2019-05-08 14:12:14 -05:00
|
|
|
use crate::datetime;
|
|
|
|
use crate::spanned;
|
|
|
|
use crate::tokens::{Error as TokenError, Span, Token, Tokenizer};
|
2017-01-29 18:53:20 -06:00
|
|
|
|
2019-08-18 20:07:23 -05:00
|
|
|
/// Type Alias for a TOML Table pair
|
2019-09-16 16:32:45 -05:00
|
|
|
type TablePair<'a> = ((Span, Cow<'a, str>), Value<'a>);
|
2019-08-18 20:07:23 -05:00
|
|
|
|
2017-01-29 18:53:20 -06:00
|
|
|
/// Deserializes a byte slice into a type.
|
|
|
|
///
|
|
|
|
/// This function will attempt to interpret `bytes` as UTF-8 data and then
|
|
|
|
/// deserialize `T` from the TOML document provided.
|
2017-04-20 12:16:00 -05:00
|
|
|
pub fn from_slice<'de, T>(bytes: &'de [u8]) -> Result<T, Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
T: de::Deserialize<'de>,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
match str::from_utf8(bytes) {
|
|
|
|
Ok(s) => from_str(s),
|
2019-07-30 12:20:18 -05:00
|
|
|
Err(e) => Err(Error::custom(None, e.to_string())),
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deserializes a string into a type.
|
|
|
|
///
|
|
|
|
/// This function will attempt to interpret `s` as a TOML document and
|
|
|
|
/// deserialize `T` from the document.
|
2017-05-10 09:49:35 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2017-05-10 10:17:58 -05:00
|
|
|
/// ```
|
2019-05-08 19:37:38 -05:00
|
|
|
/// use serde_derive::Deserialize;
|
2017-05-10 09:49:35 -05:00
|
|
|
///
|
|
|
|
/// #[derive(Deserialize)]
|
|
|
|
/// struct Config {
|
|
|
|
/// title: String,
|
|
|
|
/// owner: Owner,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// #[derive(Deserialize)]
|
|
|
|
/// struct Owner {
|
|
|
|
/// name: String,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let config: Config = toml::from_str(r#"
|
|
|
|
/// title = 'TOML Example'
|
|
|
|
///
|
|
|
|
/// [owner]
|
|
|
|
/// name = 'Lisa'
|
|
|
|
/// "#).unwrap();
|
|
|
|
///
|
|
|
|
/// assert_eq!(config.title, "TOML Example");
|
|
|
|
/// assert_eq!(config.owner.name, "Lisa");
|
|
|
|
/// }
|
|
|
|
/// ```
|
2017-04-20 12:16:00 -05:00
|
|
|
pub fn from_str<'de, T>(s: &'de str) -> Result<T, Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
T: de::Deserialize<'de>,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
let mut d = Deserializer::new(s);
|
|
|
|
let ret = T::deserialize(&mut d)?;
|
|
|
|
d.end()?;
|
2017-03-30 06:38:48 -05:00
|
|
|
Ok(ret)
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Errors that can occur when deserializing a type.
|
2019-09-04 18:44:32 -05:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
2017-01-29 18:53:20 -06:00
|
|
|
pub struct Error {
|
|
|
|
inner: Box<ErrorInner>,
|
|
|
|
}
|
|
|
|
|
2019-09-04 18:44:32 -05:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
2017-01-29 18:53:20 -06:00
|
|
|
struct ErrorInner {
|
|
|
|
kind: ErrorKind,
|
|
|
|
line: Option<usize>,
|
|
|
|
col: usize,
|
2019-07-30 12:20:18 -05:00
|
|
|
at: Option<usize>,
|
2017-01-29 18:53:20 -06:00
|
|
|
message: String,
|
|
|
|
key: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Errors that can occur when deserializing a type.
|
2019-09-04 18:44:32 -05:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
2017-01-29 18:53:20 -06:00
|
|
|
enum ErrorKind {
|
|
|
|
/// EOF was reached when looking for a value
|
|
|
|
UnexpectedEof,
|
|
|
|
|
|
|
|
/// An invalid character not allowed in a string was found
|
|
|
|
InvalidCharInString(char),
|
|
|
|
|
|
|
|
/// An invalid character was found as an escape
|
|
|
|
InvalidEscape(char),
|
|
|
|
|
|
|
|
/// An invalid character was found in a hex escape
|
|
|
|
InvalidHexEscape(char),
|
|
|
|
|
|
|
|
/// An invalid escape value was specified in a hex escape in a string.
|
|
|
|
///
|
|
|
|
/// Valid values are in the plane of unicode codepoints.
|
|
|
|
InvalidEscapeValue(u32),
|
|
|
|
|
|
|
|
/// A newline in a string was encountered when one was not allowed.
|
|
|
|
NewlineInString,
|
|
|
|
|
|
|
|
/// An unexpected character was encountered, typically when looking for a
|
|
|
|
/// value.
|
|
|
|
Unexpected(char),
|
|
|
|
|
|
|
|
/// An unterminated string was found where EOF was found before the ending
|
|
|
|
/// EOF mark.
|
|
|
|
UnterminatedString,
|
|
|
|
|
|
|
|
/// A newline was found in a table key.
|
|
|
|
NewlineInTableKey,
|
|
|
|
|
|
|
|
/// A number failed to parse
|
|
|
|
NumberInvalid,
|
|
|
|
|
|
|
|
/// A date or datetime was invalid
|
|
|
|
DateInvalid,
|
|
|
|
|
|
|
|
/// Wanted one sort of token, but found another.
|
|
|
|
Wanted {
|
|
|
|
/// Expected token type
|
|
|
|
expected: &'static str,
|
|
|
|
/// Actually found token type
|
|
|
|
found: &'static str,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// A duplicate table definition was found.
|
|
|
|
DuplicateTable(String),
|
|
|
|
|
|
|
|
/// A previously defined table was redefined as an array.
|
|
|
|
RedefineAsArray,
|
|
|
|
|
|
|
|
/// An empty table key was found.
|
|
|
|
EmptyTableKey,
|
|
|
|
|
2018-09-25 02:33:52 -05:00
|
|
|
/// Multiline strings are not allowed for key
|
|
|
|
MultilineStringKey,
|
|
|
|
|
2017-01-29 18:53:20 -06:00
|
|
|
/// A custom error which could be generated when deserializing a particular
|
|
|
|
/// type.
|
|
|
|
Custom,
|
|
|
|
|
2018-10-19 22:24:16 -05:00
|
|
|
/// A tuple with a certain number of elements was expected but something
|
|
|
|
/// else was found.
|
2018-10-09 17:36:46 -05:00
|
|
|
ExpectedTuple(usize),
|
|
|
|
|
2018-10-19 22:24:16 -05:00
|
|
|
/// Expected table keys to be in increasing tuple index order, but something
|
|
|
|
/// else was found.
|
|
|
|
ExpectedTupleIndex {
|
|
|
|
/// Expected index.
|
|
|
|
expected: usize,
|
|
|
|
/// Key that was specified.
|
|
|
|
found: String,
|
|
|
|
},
|
|
|
|
|
2018-10-09 17:36:46 -05:00
|
|
|
/// An empty table was expected but entries were found
|
|
|
|
ExpectedEmptyTable,
|
2017-04-24 07:48:02 -05:00
|
|
|
|
2018-07-11 01:29:47 -05:00
|
|
|
/// Dotted key attempted to extend something that is not a table.
|
|
|
|
DottedKeyInvalidType,
|
|
|
|
|
2018-11-11 15:09:30 -06:00
|
|
|
/// An unexpected key was encountered.
|
|
|
|
///
|
|
|
|
/// Used when deserializing a struct with a limited set of fields.
|
|
|
|
UnexpectedKeys {
|
|
|
|
/// The unexpected keys.
|
|
|
|
keys: Vec<String>,
|
|
|
|
/// Keys that may be specified.
|
|
|
|
available: &'static [&'static str],
|
|
|
|
},
|
|
|
|
|
2017-01-29 18:53:20 -06:00
|
|
|
#[doc(hidden)]
|
|
|
|
__Nonexhaustive,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deserialization implementation for TOML.
|
|
|
|
pub struct Deserializer<'a> {
|
|
|
|
require_newline_after_table: bool,
|
2019-01-07 11:06:04 -06:00
|
|
|
allow_duplciate_after_longer_table: bool,
|
2017-01-29 18:53:20 -06:00
|
|
|
input: &'a str,
|
|
|
|
tokens: Tokenizer<'a>,
|
|
|
|
}
|
|
|
|
|
2017-04-20 12:16:00 -05:00
|
|
|
impl<'de, 'b> de::Deserializer<'de> for &'b mut Deserializer<'de> {
|
2017-01-29 18:53:20 -06:00
|
|
|
type Error = Error;
|
|
|
|
|
2017-04-20 12:16:00 -05:00
|
|
|
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
2018-10-21 15:40:16 -05:00
|
|
|
let mut tables = self.tables()?;
|
2019-10-28 10:05:13 -05:00
|
|
|
let table_indices = build_table_indices(&tables);
|
|
|
|
let table_pindices = build_table_pindices(&tables);
|
2017-01-29 18:53:20 -06:00
|
|
|
|
2019-07-30 12:20:18 -05:00
|
|
|
let res = visitor.visit_map(MapVisitor {
|
2019-10-28 09:01:23 -05:00
|
|
|
values: Vec::new().into_iter().peekable(),
|
2017-01-29 18:53:20 -06:00
|
|
|
next_value: None,
|
|
|
|
depth: 0,
|
|
|
|
cur: 0,
|
|
|
|
cur_parent: 0,
|
|
|
|
max: tables.len(),
|
2019-10-28 10:05:13 -05:00
|
|
|
table_indices: &table_indices,
|
|
|
|
table_pindices: &table_pindices,
|
2017-01-29 18:53:20 -06:00
|
|
|
tables: &mut tables,
|
|
|
|
array: false,
|
|
|
|
de: self,
|
2019-07-30 12:20:18 -05:00
|
|
|
});
|
|
|
|
res.map_err(|mut err| {
|
|
|
|
// Errors originating from this library (toml), have an offset
|
|
|
|
// attached to them already. Other errors, like those originating
|
|
|
|
// from serde (like "missing field") or from a custom deserializer,
|
|
|
|
// do not have offsets on them. Here, we do a best guess at their
|
|
|
|
// location, by attributing them to the "current table" (the last
|
|
|
|
// item in `tables`).
|
|
|
|
err.fix_offset(|| tables.last().map(|table| table.at));
|
|
|
|
err.fix_linecol(|at| self.to_linecol(at));
|
|
|
|
err
|
2017-01-29 18:53:20 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-21 15:40:16 -05:00
|
|
|
// Called when the type to deserialize is an enum, as opposed to a field in the type.
|
2017-04-24 07:48:02 -05:00
|
|
|
fn deserialize_enum<V>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variants: &'static [&'static str],
|
2018-12-17 19:45:35 -06:00
|
|
|
visitor: V,
|
2017-04-24 07:48:02 -05:00
|
|
|
) -> Result<V::Value, Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
2017-04-24 07:48:02 -05:00
|
|
|
{
|
2018-10-21 15:40:16 -05:00
|
|
|
let (value, name) = self.string_or_table()?;
|
|
|
|
match value.e {
|
|
|
|
E::String(val) => visitor.visit_enum(val.into_deserializer()),
|
|
|
|
E::InlineTable(values) => {
|
|
|
|
if values.len() != 1 {
|
2019-07-30 12:20:18 -05:00
|
|
|
Err(Error::from_kind(
|
|
|
|
Some(value.start),
|
|
|
|
ErrorKind::Wanted {
|
|
|
|
expected: "exactly 1 element",
|
|
|
|
found: if values.is_empty() {
|
|
|
|
"zero elements"
|
|
|
|
} else {
|
|
|
|
"more than 1 element"
|
|
|
|
},
|
2018-10-21 15:40:16 -05:00
|
|
|
},
|
2019-07-30 12:20:18 -05:00
|
|
|
))
|
2018-10-21 15:40:16 -05:00
|
|
|
} else {
|
|
|
|
visitor.visit_enum(InlineTableDeserializer {
|
|
|
|
values: values.into_iter(),
|
|
|
|
next_value: None,
|
|
|
|
})
|
|
|
|
}
|
2017-04-24 07:48:02 -05:00
|
|
|
}
|
2018-10-21 15:40:16 -05:00
|
|
|
E::DottedTable(_) => visitor.visit_enum(DottedTableDeserializer {
|
|
|
|
name: name.expect("Expected table header to be passed."),
|
2019-08-14 21:52:24 -05:00
|
|
|
value,
|
2018-10-21 15:40:16 -05:00
|
|
|
}),
|
2019-08-14 21:52:24 -05:00
|
|
|
e => Err(Error::from_kind(
|
2019-07-30 12:20:18 -05:00
|
|
|
Some(value.start),
|
|
|
|
ErrorKind::Wanted {
|
|
|
|
expected: "string or table",
|
|
|
|
found: e.type_name(),
|
|
|
|
},
|
|
|
|
)),
|
2017-04-24 07:48:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 08:38:54 -05:00
|
|
|
fn deserialize_struct<V>(
|
|
|
|
self,
|
|
|
|
name: &'static str,
|
|
|
|
fields: &'static [&'static str],
|
|
|
|
visitor: V,
|
|
|
|
) -> Result<V::Value, Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
|
|
|
if name == spanned::NAME && fields == [spanned::START, spanned::END, spanned::VALUE] {
|
|
|
|
let start = 0;
|
|
|
|
let end = self.input.len();
|
|
|
|
|
|
|
|
let res = visitor.visit_map(SpannedDeserializer {
|
|
|
|
phantom_data: PhantomData,
|
|
|
|
start: Some(start),
|
|
|
|
value: Some(self),
|
|
|
|
end: Some(end),
|
|
|
|
});
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.deserialize_any(visitor)
|
|
|
|
}
|
|
|
|
|
2019-05-08 19:37:38 -05:00
|
|
|
serde::forward_to_deserialize_any! {
|
2017-01-29 18:53:20 -06:00
|
|
|
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq
|
2019-09-05 08:38:54 -05:00
|
|
|
bytes byte_buf map unit newtype_struct
|
2017-04-20 12:16:00 -05:00
|
|
|
ignored_any unit_struct tuple_struct tuple option identifier
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 10:05:13 -05:00
|
|
|
// Builds a datastructure that allows for efficient sublinear lookups.
|
|
|
|
// The returned HashMap contains a mapping from table header (like [a.b.c])
|
|
|
|
// to list of tables with that precise name. The tables are being identified
|
|
|
|
// by their index in the passed slice. We use a list as the implementation
|
|
|
|
// uses this data structure for arrays as well as tables,
|
|
|
|
// so if any top level [[name]] array contains multiple entries,
|
|
|
|
// there are multiple entires in the list.
|
|
|
|
// The lookup is performed in the `SeqAccess` implementation of `MapVisitor`.
|
|
|
|
// The lists are ordered, which we exploit in the search code by using
|
|
|
|
// bisection.
|
|
|
|
fn build_table_indices<'de>(tables: &[Table<'de>]) -> HashMap<Vec<Cow<'de, str>>, Vec<usize>> {
|
|
|
|
let mut res = HashMap::new();
|
|
|
|
for (i, table) in tables.iter().enumerate() {
|
|
|
|
let header = table.header.iter().map(|v| v.1.clone()).collect::<Vec<_>>();
|
2020-02-25 12:08:29 -06:00
|
|
|
res.entry(header).or_insert_with(Vec::new).push(i);
|
2019-10-28 10:05:13 -05:00
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
// Builds a datastructure that allows for efficient sublinear lookups.
|
|
|
|
// The returned HashMap contains a mapping from table header (like [a.b.c])
|
|
|
|
// to list of tables whose name at least starts with the specified
|
|
|
|
// name. So searching for [a.b] would give both [a.b.c.d] as well as [a.b.e].
|
|
|
|
// The tables are being identified by their index in the passed slice.
|
|
|
|
//
|
|
|
|
// A list is used for two reasons: First, the implementation also
|
|
|
|
// stores arrays in the same data structure and any top level array
|
|
|
|
// of size 2 or greater creates multiple entries in the list with the
|
|
|
|
// same shared name. Second, there can be multiple tables sharing
|
|
|
|
// the same prefix.
|
|
|
|
//
|
|
|
|
// The lookup is performed in the `MapAccess` implementation of `MapVisitor`.
|
|
|
|
// The lists are ordered, which we exploit in the search code by using
|
|
|
|
// bisection.
|
|
|
|
fn build_table_pindices<'de>(tables: &[Table<'de>]) -> HashMap<Vec<Cow<'de, str>>, Vec<usize>> {
|
|
|
|
let mut res = HashMap::new();
|
|
|
|
for (i, table) in tables.iter().enumerate() {
|
|
|
|
let header = table.header.iter().map(|v| v.1.clone()).collect::<Vec<_>>();
|
|
|
|
for len in 0..=header.len() {
|
|
|
|
res.entry(header[..len].to_owned())
|
2020-02-25 12:08:29 -06:00
|
|
|
.or_insert_with(Vec::new)
|
2019-10-28 10:05:13 -05:00
|
|
|
.push(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2019-09-16 16:32:45 -05:00
|
|
|
fn headers_equal<'a, 'b>(hdr_a: &[(Span, Cow<'a, str>)], hdr_b: &[(Span, Cow<'b, str>)]) -> bool {
|
|
|
|
if hdr_a.len() != hdr_b.len() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
hdr_a.iter().zip(hdr_b.iter()).all(|(h1, h2)| h1.1 == h2.1)
|
|
|
|
}
|
|
|
|
|
2017-01-29 18:53:20 -06:00
|
|
|
struct Table<'a> {
|
|
|
|
at: usize,
|
2019-09-16 16:32:45 -05:00
|
|
|
header: Vec<(Span, Cow<'a, str>)>,
|
2019-08-18 20:07:23 -05:00
|
|
|
values: Option<Vec<TablePair<'a>>>,
|
2017-01-29 18:53:20 -06:00
|
|
|
array: bool,
|
|
|
|
}
|
|
|
|
|
2019-09-09 13:04:47 -05:00
|
|
|
struct MapVisitor<'de, 'b> {
|
2019-10-28 09:01:23 -05:00
|
|
|
values: iter::Peekable<vec::IntoIter<TablePair<'de>>>,
|
2019-08-18 20:07:23 -05:00
|
|
|
next_value: Option<TablePair<'de>>,
|
2017-01-29 18:53:20 -06:00
|
|
|
depth: usize,
|
|
|
|
cur: usize,
|
|
|
|
cur_parent: usize,
|
|
|
|
max: usize,
|
2019-10-28 10:05:13 -05:00
|
|
|
table_indices: &'b HashMap<Vec<Cow<'de, str>>, Vec<usize>>,
|
|
|
|
table_pindices: &'b HashMap<Vec<Cow<'de, str>>, Vec<usize>>,
|
2017-04-20 12:16:00 -05:00
|
|
|
tables: &'b mut [Table<'de>],
|
2017-01-29 18:53:20 -06:00
|
|
|
array: bool,
|
2017-04-20 12:16:00 -05:00
|
|
|
de: &'b mut Deserializer<'de>,
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2017-04-20 12:16:00 -05:00
|
|
|
impl<'de, 'b> de::MapAccess<'de> for MapVisitor<'de, 'b> {
|
2017-01-29 18:53:20 -06:00
|
|
|
type Error = Error;
|
|
|
|
|
2017-04-20 12:16:00 -05:00
|
|
|
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
K: de::DeserializeSeed<'de>,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
if self.cur_parent == self.max || self.cur == self.max {
|
2018-12-17 19:45:35 -06:00
|
|
|
return Ok(None);
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
loop {
|
|
|
|
assert!(self.next_value.is_none());
|
|
|
|
if let Some((key, value)) = self.values.next() {
|
2019-09-16 16:32:45 -05:00
|
|
|
let ret = seed.deserialize(StrDeserializer::spanned(key.clone()))?;
|
2017-01-29 18:53:20 -06:00
|
|
|
self.next_value = Some((key, value));
|
2018-12-17 19:45:35 -06:00
|
|
|
return Ok(Some(ret));
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
let next_table = {
|
2019-10-28 10:05:13 -05:00
|
|
|
let prefix_stripped = self.tables[self.cur_parent].header[..self.depth]
|
2018-12-17 19:45:35 -06:00
|
|
|
.iter()
|
2019-10-28 10:05:13 -05:00
|
|
|
.map(|v| v.1.clone())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
self.table_pindices
|
|
|
|
.get(&prefix_stripped)
|
|
|
|
.and_then(|entries| {
|
|
|
|
let start = entries.binary_search(&self.cur).unwrap_or_else(|v| v);
|
|
|
|
if start == entries.len() || entries[start] < self.cur {
|
|
|
|
return None;
|
2018-12-17 19:45:35 -06:00
|
|
|
}
|
2019-10-28 10:05:13 -05:00
|
|
|
entries[start..]
|
|
|
|
.iter()
|
2019-11-01 09:58:09 -05:00
|
|
|
.filter_map(|i| if *i < self.max { Some(*i) } else { None })
|
2019-10-28 10:05:13 -05:00
|
|
|
.map(|i| (i, &self.tables[i]))
|
|
|
|
.find(|(_, table)| table.values.is_some())
|
|
|
|
.map(|p| p.0)
|
2018-12-17 19:45:35 -06:00
|
|
|
})
|
2017-01-29 18:53:20 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
let pos = match next_table {
|
|
|
|
Some(pos) => pos,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
self.cur = pos;
|
|
|
|
|
|
|
|
// Test to see if we're duplicating our parent's table, and if so
|
|
|
|
// then this is an error in the toml format
|
2019-01-07 11:06:04 -06:00
|
|
|
if self.cur_parent != pos {
|
2019-09-16 16:32:45 -05:00
|
|
|
if headers_equal(
|
|
|
|
&self.tables[self.cur_parent].header,
|
|
|
|
&self.tables[pos].header,
|
|
|
|
) {
|
2019-01-07 11:06:04 -06:00
|
|
|
let at = self.tables[pos].at;
|
2019-09-16 16:32:45 -05:00
|
|
|
let name = self.tables[pos]
|
|
|
|
.header
|
|
|
|
.iter()
|
|
|
|
.map(|k| k.1.to_owned())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(".");
|
2019-01-07 11:06:04 -06:00
|
|
|
return Err(self.de.error(at, ErrorKind::DuplicateTable(name)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're here we know we should share the same prefix, and if
|
|
|
|
// the longer table was defined first then we want to narrow
|
|
|
|
// down our parent's length if possible to ensure that we catch
|
|
|
|
// duplicate tables defined afterwards.
|
|
|
|
if !self.de.allow_duplciate_after_longer_table {
|
|
|
|
let parent_len = self.tables[self.cur_parent].header.len();
|
|
|
|
let cur_len = self.tables[pos].header.len();
|
|
|
|
if cur_len < parent_len {
|
|
|
|
self.cur_parent = pos;
|
|
|
|
}
|
|
|
|
}
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
let table = &mut self.tables[pos];
|
|
|
|
|
|
|
|
// If we're not yet at the appropriate depth for this table then we
|
2017-04-20 12:16:00 -05:00
|
|
|
// just next the next portion of its header and then continue
|
2017-01-29 18:53:20 -06:00
|
|
|
// decoding.
|
|
|
|
if self.depth != table.header.len() {
|
|
|
|
let key = &table.header[self.depth];
|
2019-09-16 16:32:45 -05:00
|
|
|
let key = seed.deserialize(StrDeserializer::spanned(key.clone()))?;
|
2018-12-17 19:45:35 -06:00
|
|
|
return Ok(Some(key));
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rule out cases like:
|
|
|
|
//
|
|
|
|
// [[foo.bar]]
|
|
|
|
// [[foo]]
|
2018-12-17 19:45:35 -06:00
|
|
|
if table.array {
|
2017-01-29 18:53:20 -06:00
|
|
|
let kind = ErrorKind::RedefineAsArray;
|
2018-12-17 19:45:35 -06:00
|
|
|
return Err(self.de.error(table.at, kind));
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
self.values = table
|
|
|
|
.values
|
|
|
|
.take()
|
|
|
|
.expect("Unable to read table values")
|
2019-10-28 09:01:23 -05:00
|
|
|
.into_iter()
|
|
|
|
.peekable();
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 12:16:00 -05:00
|
|
|
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
V: de::DeserializeSeed<'de>,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
if let Some((k, v)) = self.next_value.take() {
|
|
|
|
match seed.deserialize(ValueDeserializer::new(v)) {
|
|
|
|
Ok(v) => return Ok(v),
|
|
|
|
Err(mut e) => {
|
2019-09-16 16:32:45 -05:00
|
|
|
e.add_key_context(&k.1);
|
2018-12-17 19:45:35 -06:00
|
|
|
return Err(e);
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
let array =
|
|
|
|
self.tables[self.cur].array && self.depth == self.tables[self.cur].header.len() - 1;
|
2017-01-29 18:53:20 -06:00
|
|
|
self.cur += 1;
|
|
|
|
let res = seed.deserialize(MapVisitor {
|
2019-10-28 09:01:23 -05:00
|
|
|
values: Vec::new().into_iter().peekable(),
|
2017-01-29 18:53:20 -06:00
|
|
|
next_value: None,
|
2018-12-17 19:45:35 -06:00
|
|
|
depth: self.depth + if array { 0 } else { 1 },
|
2017-01-29 18:53:20 -06:00
|
|
|
cur_parent: self.cur - 1,
|
|
|
|
cur: 0,
|
|
|
|
max: self.max,
|
2019-08-14 21:52:24 -05:00
|
|
|
array,
|
2019-10-28 10:05:13 -05:00
|
|
|
table_indices: &*self.table_indices,
|
|
|
|
table_pindices: &*self.table_pindices,
|
2017-01-29 18:53:20 -06:00
|
|
|
tables: &mut *self.tables,
|
|
|
|
de: &mut *self.de,
|
|
|
|
});
|
|
|
|
res.map_err(|mut e| {
|
2019-09-16 16:32:45 -05:00
|
|
|
e.add_key_context(&self.tables[self.cur - 1].header[self.depth].1);
|
2017-01-29 18:53:20 -06:00
|
|
|
e
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 12:16:00 -05:00
|
|
|
impl<'de, 'b> de::SeqAccess<'de> for MapVisitor<'de, 'b> {
|
2017-01-29 18:53:20 -06:00
|
|
|
type Error = Error;
|
|
|
|
|
2017-04-20 12:16:00 -05:00
|
|
|
fn next_element_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
K: de::DeserializeSeed<'de>,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
assert!(self.next_value.is_none());
|
|
|
|
assert!(self.values.next().is_none());
|
|
|
|
|
|
|
|
if self.cur_parent == self.max {
|
2018-12-17 19:45:35 -06:00
|
|
|
return Ok(None);
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2019-10-28 10:05:13 -05:00
|
|
|
let header_stripped = self.tables[self.cur_parent]
|
|
|
|
.header
|
2017-01-29 18:53:20 -06:00
|
|
|
.iter()
|
2019-10-28 10:05:13 -05:00
|
|
|
.map(|v| v.1.clone())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
let start_idx = self.cur_parent + 1;
|
|
|
|
let next = self
|
|
|
|
.table_indices
|
|
|
|
.get(&header_stripped)
|
|
|
|
.and_then(|entries| {
|
|
|
|
let start = entries.binary_search(&start_idx).unwrap_or_else(|v| v);
|
|
|
|
if start == entries.len() || entries[start] < start_idx {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
entries[start..]
|
|
|
|
.iter()
|
2019-11-01 09:58:09 -05:00
|
|
|
.filter_map(|i| if *i < self.max { Some(*i) } else { None })
|
2019-10-28 10:05:13 -05:00
|
|
|
.map(|i| (i, &self.tables[i]))
|
|
|
|
.find(|(_, table)| table.array)
|
|
|
|
.map(|p| p.0)
|
2019-09-16 16:32:45 -05:00
|
|
|
})
|
2017-01-29 18:53:20 -06:00
|
|
|
.unwrap_or(self.max);
|
|
|
|
|
|
|
|
let ret = seed.deserialize(MapVisitor {
|
2018-12-17 19:45:35 -06:00
|
|
|
values: self.tables[self.cur_parent]
|
|
|
|
.values
|
|
|
|
.take()
|
|
|
|
.expect("Unable to read table values")
|
2019-10-28 09:01:23 -05:00
|
|
|
.into_iter()
|
|
|
|
.peekable(),
|
2017-01-29 18:53:20 -06:00
|
|
|
next_value: None,
|
|
|
|
depth: self.depth + 1,
|
|
|
|
cur_parent: self.cur_parent,
|
|
|
|
max: next,
|
|
|
|
cur: 0,
|
|
|
|
array: false,
|
2019-10-28 10:05:13 -05:00
|
|
|
table_indices: &*self.table_indices,
|
|
|
|
table_pindices: &*self.table_pindices,
|
2017-01-29 18:53:20 -06:00
|
|
|
tables: &mut self.tables,
|
|
|
|
de: &mut self.de,
|
|
|
|
})?;
|
|
|
|
self.cur_parent = next;
|
2017-03-30 06:38:48 -05:00
|
|
|
Ok(Some(ret))
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 12:16:00 -05:00
|
|
|
impl<'de, 'b> de::Deserializer<'de> for MapVisitor<'de, 'b> {
|
2017-01-29 18:53:20 -06:00
|
|
|
type Error = Error;
|
|
|
|
|
2017-04-20 12:16:00 -05:00
|
|
|
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
2018-12-17 19:45:35 -06:00
|
|
|
if self.array {
|
2017-01-29 18:53:20 -06:00
|
|
|
visitor.visit_seq(self)
|
|
|
|
} else {
|
|
|
|
visitor.visit_map(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// `None` is interpreted as a missing field so be sure to implement `Some`
|
|
|
|
// as a present field.
|
|
|
|
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
visitor.visit_some(self)
|
|
|
|
}
|
|
|
|
|
2017-07-06 13:43:36 -05:00
|
|
|
fn deserialize_newtype_struct<V>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
2018-12-17 19:45:35 -06:00
|
|
|
visitor: V,
|
2017-07-06 13:43:36 -05:00
|
|
|
) -> Result<V::Value, Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
2017-07-06 13:43:36 -05:00
|
|
|
{
|
|
|
|
visitor.visit_newtype_struct(self)
|
|
|
|
}
|
|
|
|
|
2019-10-28 09:01:23 -05:00
|
|
|
fn deserialize_struct<V>(
|
|
|
|
mut self,
|
|
|
|
name: &'static str,
|
|
|
|
fields: &'static [&'static str],
|
|
|
|
visitor: V,
|
|
|
|
) -> Result<V::Value, Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
|
|
|
if name == spanned::NAME
|
|
|
|
&& fields == [spanned::START, spanned::END, spanned::VALUE]
|
|
|
|
&& !(self.array && !self.values.peek().is_none())
|
|
|
|
{
|
|
|
|
// TODO we can't actually emit spans here for the *entire* table/array
|
|
|
|
// due to the format that toml uses. Setting the start and end to 0 is
|
|
|
|
// *detectable* (and no reasonable span would look like that),
|
|
|
|
// it would be better to expose this in the API via proper
|
|
|
|
// ADTs like Option<T>.
|
|
|
|
let start = 0;
|
|
|
|
let end = 0;
|
|
|
|
|
|
|
|
let res = visitor.visit_map(SpannedDeserializer {
|
|
|
|
phantom_data: PhantomData,
|
|
|
|
start: Some(start),
|
|
|
|
value: Some(self),
|
|
|
|
end: Some(end),
|
|
|
|
});
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.deserialize_any(visitor)
|
|
|
|
}
|
|
|
|
|
2019-09-05 09:18:26 -05:00
|
|
|
fn deserialize_enum<V>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variants: &'static [&'static str],
|
|
|
|
visitor: V,
|
|
|
|
) -> Result<V::Value, Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
|
|
|
if self.tables.len() != 1 {
|
|
|
|
return Err(Error::custom(
|
|
|
|
Some(self.cur),
|
|
|
|
"enum table must contain exactly one table".into(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
let table = &mut self.tables[0];
|
|
|
|
let values = table.values.take().expect("table has no values?");
|
2020-02-25 12:08:29 -06:00
|
|
|
if table.header.is_empty() {
|
2019-09-05 09:18:26 -05:00
|
|
|
return Err(self.de.error(self.cur, ErrorKind::EmptyTableKey));
|
|
|
|
}
|
2019-09-16 16:32:45 -05:00
|
|
|
let name = table.header[table.header.len() - 1].1.to_owned();
|
2019-09-05 09:18:26 -05:00
|
|
|
visitor.visit_enum(DottedTableDeserializer {
|
2019-09-16 16:32:45 -05:00
|
|
|
name,
|
2019-09-05 09:18:26 -05:00
|
|
|
value: Value {
|
|
|
|
e: E::DottedTable(values),
|
|
|
|
start: 0,
|
|
|
|
end: 0,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-08 19:37:38 -05:00
|
|
|
serde::forward_to_deserialize_any! {
|
2017-01-29 18:53:20 -06:00
|
|
|
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq
|
2019-10-28 09:01:23 -05:00
|
|
|
bytes byte_buf map unit identifier
|
2019-09-05 09:18:26 -05:00
|
|
|
ignored_any unit_struct tuple_struct tuple
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct StrDeserializer<'a> {
|
2019-09-16 16:32:45 -05:00
|
|
|
span: Option<Span>,
|
2017-01-29 18:53:20 -06:00
|
|
|
key: Cow<'a, str>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> StrDeserializer<'a> {
|
2019-09-16 16:32:45 -05:00
|
|
|
fn spanned(inner: (Span, Cow<'a, str>)) -> StrDeserializer<'a> {
|
|
|
|
StrDeserializer {
|
|
|
|
span: Some(inner.0),
|
|
|
|
key: inner.1,
|
|
|
|
}
|
|
|
|
}
|
2017-01-29 18:53:20 -06:00
|
|
|
fn new(key: Cow<'a, str>) -> StrDeserializer<'a> {
|
2019-09-16 16:32:45 -05:00
|
|
|
StrDeserializer { span: None, key }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> de::IntoDeserializer<'a, Error> for StrDeserializer<'a> {
|
|
|
|
type Deserializer = Self;
|
|
|
|
|
|
|
|
fn into_deserializer(self) -> Self::Deserializer {
|
|
|
|
self
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 12:16:00 -05:00
|
|
|
impl<'de> de::Deserializer<'de> for StrDeserializer<'de> {
|
2017-01-29 18:53:20 -06:00
|
|
|
type Error = Error;
|
|
|
|
|
2017-04-20 12:16:00 -05:00
|
|
|
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
match self.key {
|
2017-04-20 12:16:00 -05:00
|
|
|
Cow::Borrowed(s) => visitor.visit_borrowed_str(s),
|
2017-01-29 18:53:20 -06:00
|
|
|
Cow::Owned(s) => visitor.visit_string(s),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 16:32:45 -05:00
|
|
|
fn deserialize_struct<V>(
|
|
|
|
self,
|
|
|
|
name: &'static str,
|
|
|
|
fields: &'static [&'static str],
|
|
|
|
visitor: V,
|
|
|
|
) -> Result<V::Value, Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
|
|
|
if name == spanned::NAME && fields == [spanned::START, spanned::END, spanned::VALUE] {
|
|
|
|
if let Some(span) = self.span {
|
|
|
|
return visitor.visit_map(SpannedDeserializer {
|
|
|
|
phantom_data: PhantomData,
|
|
|
|
start: Some(span.start),
|
|
|
|
value: Some(StrDeserializer::new(self.key)),
|
|
|
|
end: Some(span.end),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.deserialize_any(visitor)
|
|
|
|
}
|
|
|
|
|
2019-05-08 19:37:38 -05:00
|
|
|
serde::forward_to_deserialize_any! {
|
2017-01-29 18:53:20 -06:00
|
|
|
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq
|
2019-09-16 16:32:45 -05:00
|
|
|
bytes byte_buf map option unit newtype_struct
|
2017-04-20 12:16:00 -05:00
|
|
|
ignored_any unit_struct tuple_struct tuple enum identifier
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ValueDeserializer<'a> {
|
|
|
|
value: Value<'a>,
|
2018-11-16 14:37:02 -06:00
|
|
|
validate_struct_keys: bool,
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ValueDeserializer<'a> {
|
|
|
|
fn new(value: Value<'a>) -> ValueDeserializer<'a> {
|
|
|
|
ValueDeserializer {
|
2019-08-14 21:52:24 -05:00
|
|
|
value,
|
2018-11-16 14:37:02 -06:00
|
|
|
validate_struct_keys: false,
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
}
|
2018-11-16 14:37:02 -06:00
|
|
|
|
|
|
|
fn with_struct_key_validation(mut self) -> Self {
|
|
|
|
self.validate_struct_keys = true;
|
|
|
|
self
|
|
|
|
}
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2017-04-20 12:16:00 -05:00
|
|
|
impl<'de> de::Deserializer<'de> for ValueDeserializer<'de> {
|
2017-01-29 18:53:20 -06:00
|
|
|
type Error = Error;
|
|
|
|
|
2017-04-20 12:16:00 -05:00
|
|
|
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
2019-07-30 12:20:18 -05:00
|
|
|
let start = self.value.start;
|
|
|
|
let res = match self.value.e {
|
2018-05-05 16:41:57 -05:00
|
|
|
E::Integer(i) => visitor.visit_i64(i),
|
|
|
|
E::Boolean(b) => visitor.visit_bool(b),
|
|
|
|
E::Float(f) => visitor.visit_f64(f),
|
|
|
|
E::String(Cow::Borrowed(s)) => visitor.visit_borrowed_str(s),
|
|
|
|
E::String(Cow::Owned(s)) => visitor.visit_string(s),
|
|
|
|
E::Datetime(s) => visitor.visit_map(DatetimeDeserializer {
|
2017-01-29 18:53:20 -06:00
|
|
|
date: s,
|
|
|
|
visited: false,
|
|
|
|
}),
|
2018-05-05 16:41:57 -05:00
|
|
|
E::Array(values) => {
|
2017-01-29 18:53:20 -06:00
|
|
|
let mut s = de::value::SeqDeserializer::new(values.into_iter());
|
|
|
|
let ret = visitor.visit_seq(&mut s)?;
|
|
|
|
s.end()?;
|
|
|
|
Ok(ret)
|
|
|
|
}
|
2018-07-27 13:49:30 -05:00
|
|
|
E::InlineTable(values) | E::DottedTable(values) => {
|
2017-01-29 18:53:20 -06:00
|
|
|
visitor.visit_map(InlineTableDeserializer {
|
|
|
|
values: values.into_iter(),
|
|
|
|
next_value: None,
|
|
|
|
})
|
|
|
|
}
|
2019-07-30 12:20:18 -05:00
|
|
|
};
|
|
|
|
res.map_err(|mut err| {
|
|
|
|
// Attribute the error to whatever value returned the error.
|
|
|
|
err.fix_offset(|| Some(start));
|
|
|
|
err
|
|
|
|
})
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn deserialize_struct<V>(
|
|
|
|
self,
|
|
|
|
name: &'static str,
|
|
|
|
fields: &'static [&'static str],
|
|
|
|
visitor: V,
|
|
|
|
) -> Result<V::Value, Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
2019-08-14 21:52:24 -05:00
|
|
|
if name == datetime::NAME && fields == [datetime::FIELD] {
|
2018-05-05 16:41:57 -05:00
|
|
|
if let E::Datetime(s) = self.value.e {
|
2017-01-29 18:53:20 -06:00
|
|
|
return visitor.visit_map(DatetimeDeserializer {
|
|
|
|
date: s,
|
|
|
|
visited: false,
|
2018-12-17 19:45:35 -06:00
|
|
|
});
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 14:37:02 -06:00
|
|
|
if self.validate_struct_keys {
|
2019-08-14 21:52:24 -05:00
|
|
|
match self.value.e {
|
|
|
|
E::InlineTable(ref values) | E::DottedTable(ref values) => {
|
2018-12-17 19:45:35 -06:00
|
|
|
let extra_fields = values
|
|
|
|
.iter()
|
2018-11-16 18:47:23 -06:00
|
|
|
.filter_map(|key_value| {
|
|
|
|
let (ref key, ref _val) = *key_value;
|
2019-09-16 16:32:45 -05:00
|
|
|
if !fields.contains(&&*(key.1)) {
|
2018-11-16 14:37:02 -06:00
|
|
|
Some(key.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2019-09-16 16:32:45 -05:00
|
|
|
.collect::<Vec<_>>();
|
2018-11-11 15:09:30 -06:00
|
|
|
|
2018-11-16 14:37:02 -06:00
|
|
|
if !extra_fields.is_empty() {
|
2019-07-30 12:20:18 -05:00
|
|
|
return Err(Error::from_kind(
|
|
|
|
Some(self.value.start),
|
|
|
|
ErrorKind::UnexpectedKeys {
|
|
|
|
keys: extra_fields
|
|
|
|
.iter()
|
2019-09-16 16:32:45 -05:00
|
|
|
.map(|k| k.1.to_string())
|
2019-07-30 12:20:18 -05:00
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
available: fields,
|
|
|
|
},
|
|
|
|
));
|
2018-11-16 14:37:02 -06:00
|
|
|
}
|
2018-11-11 15:09:30 -06:00
|
|
|
}
|
2018-11-16 14:37:02 -06:00
|
|
|
_ => {}
|
2018-11-11 15:09:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 21:52:24 -05:00
|
|
|
if name == spanned::NAME && fields == [spanned::START, spanned::END, spanned::VALUE] {
|
2018-05-05 16:41:57 -05:00
|
|
|
let start = self.value.start;
|
|
|
|
let end = self.value.end;
|
2018-05-06 20:47:09 -05:00
|
|
|
|
2018-05-05 16:41:57 -05:00
|
|
|
return visitor.visit_map(SpannedDeserializer {
|
2019-09-05 08:38:54 -05:00
|
|
|
phantom_data: PhantomData,
|
2018-05-05 16:41:57 -05:00
|
|
|
start: Some(start),
|
|
|
|
value: Some(self.value),
|
|
|
|
end: Some(end),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-04-20 12:16:00 -05:00
|
|
|
self.deserialize_any(visitor)
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// `None` is interpreted as a missing field so be sure to implement `Some`
|
|
|
|
// as a present field.
|
|
|
|
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
visitor.visit_some(self)
|
|
|
|
}
|
|
|
|
|
2017-04-27 23:00:37 -05:00
|
|
|
fn deserialize_enum<V>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variants: &'static [&'static str],
|
2018-12-17 19:45:35 -06:00
|
|
|
visitor: V,
|
2017-04-27 23:00:37 -05:00
|
|
|
) -> Result<V::Value, Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
2017-04-27 23:00:37 -05:00
|
|
|
{
|
2018-05-05 16:41:57 -05:00
|
|
|
match self.value.e {
|
|
|
|
E::String(val) => visitor.visit_enum(val.into_deserializer()),
|
2018-10-21 15:40:16 -05:00
|
|
|
E::InlineTable(values) => {
|
2018-10-09 14:59:46 -05:00
|
|
|
if values.len() != 1 {
|
2019-07-30 12:20:18 -05:00
|
|
|
Err(Error::from_kind(
|
|
|
|
Some(self.value.start),
|
|
|
|
ErrorKind::Wanted {
|
|
|
|
expected: "exactly 1 element",
|
|
|
|
found: if values.is_empty() {
|
|
|
|
"zero elements"
|
|
|
|
} else {
|
|
|
|
"more than 1 element"
|
|
|
|
},
|
2018-10-09 14:59:46 -05:00
|
|
|
},
|
2019-07-30 12:20:18 -05:00
|
|
|
))
|
2018-10-09 14:59:46 -05:00
|
|
|
} else {
|
|
|
|
visitor.visit_enum(InlineTableDeserializer {
|
|
|
|
values: values.into_iter(),
|
|
|
|
next_value: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-08-14 21:52:24 -05:00
|
|
|
e => Err(Error::from_kind(
|
2019-07-30 12:20:18 -05:00
|
|
|
Some(self.value.start),
|
|
|
|
ErrorKind::Wanted {
|
|
|
|
expected: "string or inline table",
|
|
|
|
found: e.type_name(),
|
|
|
|
},
|
|
|
|
)),
|
2017-04-27 23:00:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 09:34:45 -05:00
|
|
|
fn deserialize_newtype_struct<V>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
2018-12-17 19:45:35 -06:00
|
|
|
visitor: V,
|
2017-07-06 09:34:45 -05:00
|
|
|
) -> Result<V::Value, Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
2017-07-06 09:34:45 -05:00
|
|
|
{
|
|
|
|
visitor.visit_newtype_struct(self)
|
|
|
|
}
|
|
|
|
|
2019-05-08 19:37:38 -05:00
|
|
|
serde::forward_to_deserialize_any! {
|
2017-01-29 18:53:20 -06:00
|
|
|
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq
|
2017-07-06 09:34:45 -05:00
|
|
|
bytes byte_buf map unit identifier
|
2017-04-27 23:00:37 -05:00
|
|
|
ignored_any unit_struct tuple_struct tuple
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 09:01:23 -05:00
|
|
|
impl<'de, 'b> de::IntoDeserializer<'de, Error> for MapVisitor<'de, 'b> {
|
|
|
|
type Deserializer = MapVisitor<'de, 'b>;
|
|
|
|
|
|
|
|
fn into_deserializer(self) -> Self::Deserializer {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 08:38:54 -05:00
|
|
|
impl<'de, 'b> de::IntoDeserializer<'de, Error> for &'b mut Deserializer<'de> {
|
|
|
|
type Deserializer = Self;
|
|
|
|
|
|
|
|
fn into_deserializer(self) -> Self::Deserializer {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 12:16:00 -05:00
|
|
|
impl<'de> de::IntoDeserializer<'de, Error> for Value<'de> {
|
|
|
|
type Deserializer = ValueDeserializer<'de>;
|
2017-01-29 18:53:20 -06:00
|
|
|
|
|
|
|
fn into_deserializer(self) -> Self::Deserializer {
|
|
|
|
ValueDeserializer::new(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 08:38:54 -05:00
|
|
|
struct SpannedDeserializer<'de, T: de::IntoDeserializer<'de, Error>> {
|
|
|
|
phantom_data: PhantomData<&'de ()>,
|
2018-05-05 16:41:57 -05:00
|
|
|
start: Option<usize>,
|
|
|
|
end: Option<usize>,
|
2019-09-05 08:38:54 -05:00
|
|
|
value: Option<T>,
|
2018-05-05 16:41:57 -05:00
|
|
|
}
|
|
|
|
|
2019-09-05 08:38:54 -05:00
|
|
|
impl<'de, T> de::MapAccess<'de> for SpannedDeserializer<'de, T>
|
|
|
|
where
|
|
|
|
T: de::IntoDeserializer<'de, Error>,
|
|
|
|
{
|
2018-05-05 16:41:57 -05:00
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
|
|
|
|
where
|
|
|
|
K: de::DeserializeSeed<'de>,
|
|
|
|
{
|
|
|
|
if self.start.is_some() {
|
2018-12-17 19:45:35 -06:00
|
|
|
seed.deserialize(BorrowedStrDeserializer::new(spanned::START))
|
|
|
|
.map(Some)
|
2018-05-05 16:41:57 -05:00
|
|
|
} else if self.end.is_some() {
|
2018-12-17 19:45:35 -06:00
|
|
|
seed.deserialize(BorrowedStrDeserializer::new(spanned::END))
|
|
|
|
.map(Some)
|
2018-05-06 20:47:09 -05:00
|
|
|
} else if self.value.is_some() {
|
2018-12-17 19:45:35 -06:00
|
|
|
seed.deserialize(BorrowedStrDeserializer::new(spanned::VALUE))
|
|
|
|
.map(Some)
|
2018-05-05 16:41:57 -05:00
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error>
|
|
|
|
where
|
|
|
|
V: de::DeserializeSeed<'de>,
|
|
|
|
{
|
|
|
|
if let Some(start) = self.start.take() {
|
|
|
|
seed.deserialize(start.into_deserializer())
|
2018-12-17 19:45:35 -06:00
|
|
|
} else if let Some(end) = self.end.take() {
|
2018-05-06 20:47:09 -05:00
|
|
|
seed.deserialize(end.into_deserializer())
|
2018-05-05 16:41:57 -05:00
|
|
|
} else if let Some(value) = self.value.take() {
|
|
|
|
seed.deserialize(value.into_deserializer())
|
|
|
|
} else {
|
|
|
|
panic!("next_value_seed called before next_key_seed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-29 18:53:20 -06:00
|
|
|
struct DatetimeDeserializer<'a> {
|
|
|
|
visited: bool,
|
|
|
|
date: &'a str,
|
|
|
|
}
|
|
|
|
|
2017-04-20 12:16:00 -05:00
|
|
|
impl<'de> de::MapAccess<'de> for DatetimeDeserializer<'de> {
|
2017-01-29 18:53:20 -06:00
|
|
|
type Error = Error;
|
|
|
|
|
2017-04-20 12:16:00 -05:00
|
|
|
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
K: de::DeserializeSeed<'de>,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
if self.visited {
|
2018-12-17 19:45:35 -06:00
|
|
|
return Ok(None);
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
self.visited = true;
|
|
|
|
seed.deserialize(DatetimeFieldDeserializer).map(Some)
|
|
|
|
}
|
|
|
|
|
2017-04-20 12:16:00 -05:00
|
|
|
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
V: de::DeserializeSeed<'de>,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
seed.deserialize(StrDeserializer::new(self.date.into()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DatetimeFieldDeserializer;
|
|
|
|
|
2017-04-20 12:16:00 -05:00
|
|
|
impl<'de> de::Deserializer<'de> for DatetimeFieldDeserializer {
|
2017-01-29 18:53:20 -06:00
|
|
|
type Error = Error;
|
|
|
|
|
2017-04-20 12:16:00 -05:00
|
|
|
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
2018-05-06 20:47:09 -05:00
|
|
|
visitor.visit_borrowed_str(datetime::FIELD)
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2019-05-08 19:37:38 -05:00
|
|
|
serde::forward_to_deserialize_any! {
|
2017-01-29 18:53:20 -06:00
|
|
|
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq
|
2017-04-20 12:16:00 -05:00
|
|
|
bytes byte_buf map struct option unit newtype_struct
|
|
|
|
ignored_any unit_struct tuple_struct tuple enum identifier
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 15:40:16 -05:00
|
|
|
struct DottedTableDeserializer<'a> {
|
|
|
|
name: Cow<'a, str>,
|
|
|
|
value: Value<'a>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> de::EnumAccess<'de> for DottedTableDeserializer<'de> {
|
|
|
|
type Error = Error;
|
|
|
|
type Variant = TableEnumDeserializer<'de>;
|
|
|
|
|
|
|
|
fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
|
|
|
|
where
|
|
|
|
V: de::DeserializeSeed<'de>,
|
|
|
|
{
|
|
|
|
let (name, value) = (self.name, self.value);
|
|
|
|
seed.deserialize(StrDeserializer::new(name))
|
2019-08-14 21:52:24 -05:00
|
|
|
.map(|val| (val, TableEnumDeserializer { value }))
|
2018-10-21 15:40:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-29 18:53:20 -06:00
|
|
|
struct InlineTableDeserializer<'a> {
|
2019-08-18 20:07:23 -05:00
|
|
|
values: vec::IntoIter<TablePair<'a>>,
|
2017-01-29 18:53:20 -06:00
|
|
|
next_value: Option<Value<'a>>,
|
|
|
|
}
|
|
|
|
|
2017-04-20 12:16:00 -05:00
|
|
|
impl<'de> de::MapAccess<'de> for InlineTableDeserializer<'de> {
|
2017-01-29 18:53:20 -06:00
|
|
|
type Error = Error;
|
|
|
|
|
2017-04-20 12:16:00 -05:00
|
|
|
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
K: de::DeserializeSeed<'de>,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
|
|
|
let (key, value) = match self.values.next() {
|
|
|
|
Some(pair) => pair,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
self.next_value = Some(value);
|
2019-09-16 16:32:45 -05:00
|
|
|
seed.deserialize(StrDeserializer::spanned(key)).map(Some)
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2017-04-20 12:16:00 -05:00
|
|
|
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
V: de::DeserializeSeed<'de>,
|
2017-01-29 18:53:20 -06:00
|
|
|
{
|
2017-04-05 14:32:42 -05:00
|
|
|
let value = self.next_value.take().expect("Unable to read table values");
|
2017-01-29 18:53:20 -06:00
|
|
|
seed.deserialize(ValueDeserializer::new(value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 14:59:46 -05:00
|
|
|
impl<'de> de::EnumAccess<'de> for InlineTableDeserializer<'de> {
|
|
|
|
type Error = Error;
|
2018-10-21 15:40:16 -05:00
|
|
|
type Variant = TableEnumDeserializer<'de>;
|
2018-10-09 14:59:46 -05:00
|
|
|
|
|
|
|
fn variant_seed<V>(mut self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
|
|
|
|
where
|
|
|
|
V: de::DeserializeSeed<'de>,
|
|
|
|
{
|
|
|
|
let (key, value) = match self.values.next() {
|
|
|
|
Some(pair) => pair,
|
|
|
|
None => {
|
2019-07-30 12:20:18 -05:00
|
|
|
return Err(Error::from_kind(
|
|
|
|
None, // FIXME: How do we get an offset here?
|
|
|
|
ErrorKind::Wanted {
|
|
|
|
expected: "table with exactly 1 entry",
|
|
|
|
found: "empty table",
|
|
|
|
},
|
|
|
|
));
|
2018-10-09 14:59:46 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-16 16:32:45 -05:00
|
|
|
seed.deserialize(StrDeserializer::new(key.1))
|
2019-08-14 21:52:24 -05:00
|
|
|
.map(|val| (val, TableEnumDeserializer { value }))
|
2018-10-09 14:59:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 15:40:16 -05:00
|
|
|
/// Deserializes table values into enum variants.
|
|
|
|
struct TableEnumDeserializer<'a> {
|
|
|
|
value: Value<'a>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> de::VariantAccess<'de> for TableEnumDeserializer<'de> {
|
2018-10-09 14:59:46 -05:00
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn unit_variant(self) -> Result<(), Self::Error> {
|
2018-10-21 15:40:16 -05:00
|
|
|
match self.value.e {
|
2018-10-09 17:36:46 -05:00
|
|
|
E::InlineTable(values) | E::DottedTable(values) => {
|
2019-08-14 21:52:24 -05:00
|
|
|
if values.is_empty() {
|
2018-10-09 17:36:46 -05:00
|
|
|
Ok(())
|
|
|
|
} else {
|
2019-07-30 12:20:18 -05:00
|
|
|
Err(Error::from_kind(
|
|
|
|
Some(self.value.start),
|
|
|
|
ErrorKind::ExpectedEmptyTable,
|
|
|
|
))
|
2018-10-09 17:36:46 -05:00
|
|
|
}
|
|
|
|
}
|
2019-08-14 21:52:24 -05:00
|
|
|
e => Err(Error::from_kind(
|
2019-07-30 12:20:18 -05:00
|
|
|
Some(self.value.start),
|
|
|
|
ErrorKind::Wanted {
|
|
|
|
expected: "table",
|
|
|
|
found: e.type_name(),
|
|
|
|
},
|
|
|
|
)),
|
2018-10-09 17:36:46 -05:00
|
|
|
}
|
2018-10-09 14:59:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>
|
|
|
|
where
|
|
|
|
T: de::DeserializeSeed<'de>,
|
|
|
|
{
|
2018-10-21 15:40:16 -05:00
|
|
|
seed.deserialize(ValueDeserializer::new(self.value))
|
2018-10-09 14:59:46 -05:00
|
|
|
}
|
|
|
|
|
2018-10-09 17:36:46 -05:00
|
|
|
fn tuple_variant<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
|
2018-10-09 14:59:46 -05:00
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
2018-10-21 15:40:16 -05:00
|
|
|
match self.value.e {
|
2018-10-09 17:36:46 -05:00
|
|
|
E::InlineTable(values) | E::DottedTable(values) => {
|
|
|
|
let tuple_values = values
|
|
|
|
.into_iter()
|
|
|
|
.enumerate()
|
2019-09-16 16:32:45 -05:00
|
|
|
.map(|(index, (key, value))| match key.1.parse::<usize>() {
|
2018-10-19 22:24:16 -05:00
|
|
|
Ok(key_index) if key_index == index => Ok(value),
|
2019-07-30 12:20:18 -05:00
|
|
|
Ok(_) | Err(_) => Err(Error::from_kind(
|
2019-09-16 16:32:45 -05:00
|
|
|
Some(key.0.start),
|
2019-07-30 12:20:18 -05:00
|
|
|
ErrorKind::ExpectedTupleIndex {
|
|
|
|
expected: index,
|
2019-09-16 16:32:45 -05:00
|
|
|
found: key.1.to_string(),
|
2019-07-30 12:20:18 -05:00
|
|
|
},
|
|
|
|
)),
|
2018-10-09 17:36:46 -05:00
|
|
|
})
|
2018-10-19 22:24:16 -05:00
|
|
|
// Fold all values into a `Vec`, or return the first error.
|
|
|
|
.fold(Ok(Vec::with_capacity(len)), |result, value_result| {
|
2018-10-21 16:33:20 -05:00
|
|
|
result.and_then(move |mut tuple_values| match value_result {
|
2018-10-19 22:24:16 -05:00
|
|
|
Ok(value) => {
|
|
|
|
tuple_values.push(value);
|
|
|
|
Ok(tuple_values)
|
|
|
|
}
|
|
|
|
// `Result<de::Value, Self::Error>` to `Result<Vec<_>, Self::Error>`
|
|
|
|
Err(e) => Err(e),
|
|
|
|
})
|
|
|
|
})?;
|
2018-10-09 17:36:46 -05:00
|
|
|
|
|
|
|
if tuple_values.len() == len {
|
|
|
|
de::Deserializer::deserialize_seq(
|
|
|
|
ValueDeserializer::new(Value {
|
|
|
|
e: E::Array(tuple_values),
|
2018-10-21 15:40:16 -05:00
|
|
|
start: self.value.start,
|
|
|
|
end: self.value.end,
|
2018-10-09 17:36:46 -05:00
|
|
|
}),
|
|
|
|
visitor,
|
|
|
|
)
|
|
|
|
} else {
|
2019-07-30 12:20:18 -05:00
|
|
|
Err(Error::from_kind(
|
|
|
|
Some(self.value.start),
|
|
|
|
ErrorKind::ExpectedTuple(len),
|
|
|
|
))
|
2018-10-09 17:36:46 -05:00
|
|
|
}
|
|
|
|
}
|
2019-08-14 21:52:24 -05:00
|
|
|
e => Err(Error::from_kind(
|
2019-07-30 12:20:18 -05:00
|
|
|
Some(self.value.start),
|
|
|
|
ErrorKind::Wanted {
|
|
|
|
expected: "table",
|
|
|
|
found: e.type_name(),
|
|
|
|
},
|
|
|
|
)),
|
2018-10-09 17:36:46 -05:00
|
|
|
}
|
2018-10-09 14:59:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn struct_variant<V>(
|
|
|
|
self,
|
|
|
|
fields: &'static [&'static str],
|
|
|
|
visitor: V,
|
|
|
|
) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
|
|
|
de::Deserializer::deserialize_struct(
|
2018-11-16 14:37:02 -06:00
|
|
|
ValueDeserializer::new(self.value).with_struct_key_validation(),
|
2018-10-09 14:59:46 -05:00
|
|
|
"", // TODO: this should be the variant name
|
|
|
|
fields,
|
|
|
|
visitor,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2017-04-24 23:57:35 -05:00
|
|
|
|
2017-01-29 18:53:20 -06:00
|
|
|
impl<'a> Deserializer<'a> {
|
|
|
|
/// Creates a new deserializer which will be deserializing the string
|
|
|
|
/// provided.
|
|
|
|
pub fn new(input: &'a str) -> Deserializer<'a> {
|
|
|
|
Deserializer {
|
|
|
|
tokens: Tokenizer::new(input),
|
2019-08-14 21:52:24 -05:00
|
|
|
input,
|
2017-02-08 23:36:38 -06:00
|
|
|
require_newline_after_table: true,
|
2019-01-07 11:06:04 -06:00
|
|
|
allow_duplciate_after_longer_table: false,
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The `Deserializer::end` method should be called after a value has been
|
|
|
|
/// fully deserialized. This allows the `Deserializer` to validate that the
|
|
|
|
/// input stream is at the end or that it only has trailing
|
|
|
|
/// whitespace/comments.
|
|
|
|
pub fn end(&mut self) -> Result<(), Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Historical versions of toml-rs accidentally allowed a newline after a
|
|
|
|
/// table definition, but the TOML spec requires a newline after a table
|
|
|
|
/// definition header.
|
|
|
|
///
|
|
|
|
/// This option can be set to `false` (the default is `true`) to emulate
|
|
|
|
/// this behavior for backwards compatibility with older toml-rs versions.
|
|
|
|
pub fn set_require_newline_after_table(&mut self, require: bool) {
|
|
|
|
self.require_newline_after_table = require;
|
|
|
|
}
|
|
|
|
|
2019-01-07 11:06:04 -06:00
|
|
|
/// Historical versions of toml-rs accidentally allowed a duplicate table
|
|
|
|
/// header after a longer table header was previously defined. This is
|
|
|
|
/// invalid according to the TOML spec, however.
|
|
|
|
///
|
|
|
|
/// This option can be set to `true` (the default is `false`) to emulate
|
|
|
|
/// this behavior for backwards compatibility with older toml-rs versions.
|
|
|
|
pub fn set_allow_duplicate_after_longer_table(&mut self, allow: bool) {
|
|
|
|
self.allow_duplciate_after_longer_table = allow;
|
|
|
|
}
|
|
|
|
|
2018-10-21 15:40:16 -05:00
|
|
|
fn tables(&mut self) -> Result<Vec<Table<'a>>, Error> {
|
|
|
|
let mut tables = Vec::new();
|
|
|
|
let mut cur_table = Table {
|
|
|
|
at: 0,
|
|
|
|
header: Vec::new(),
|
|
|
|
values: None,
|
|
|
|
array: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
while let Some(line) = self.line()? {
|
|
|
|
match line {
|
|
|
|
Line::Table {
|
|
|
|
at,
|
|
|
|
mut header,
|
|
|
|
array,
|
|
|
|
} => {
|
|
|
|
if !cur_table.header.is_empty() || cur_table.values.is_some() {
|
|
|
|
tables.push(cur_table);
|
|
|
|
}
|
|
|
|
cur_table = Table {
|
2019-08-14 21:52:24 -05:00
|
|
|
at,
|
2018-10-21 15:40:16 -05:00
|
|
|
header: Vec::new(),
|
|
|
|
values: Some(Vec::new()),
|
2019-08-14 21:52:24 -05:00
|
|
|
array,
|
2018-10-21 15:40:16 -05:00
|
|
|
};
|
|
|
|
loop {
|
|
|
|
let part = header.next().map_err(|e| self.token_error(e));
|
|
|
|
match part? {
|
|
|
|
Some(part) => cur_table.header.push(part),
|
|
|
|
None => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Line::KeyValue(key, value) => {
|
|
|
|
if cur_table.values.is_none() {
|
|
|
|
cur_table.values = Some(Vec::new());
|
|
|
|
}
|
|
|
|
self.add_dotted_key(key, value, cur_table.values.as_mut().unwrap())?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !cur_table.header.is_empty() || cur_table.values.is_some() {
|
|
|
|
tables.push(cur_table);
|
|
|
|
}
|
|
|
|
Ok(tables)
|
|
|
|
}
|
|
|
|
|
2017-01-29 18:53:20 -06:00
|
|
|
fn line(&mut self) -> Result<Option<Line<'a>>, Error> {
|
|
|
|
loop {
|
|
|
|
self.eat_whitespace()?;
|
|
|
|
if self.eat_comment()? {
|
2018-12-17 19:45:35 -06:00
|
|
|
continue;
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
if self.eat(Token::Newline)? {
|
2018-12-17 19:45:35 -06:00
|
|
|
continue;
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
2018-12-17 19:45:35 -06:00
|
|
|
break;
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
match self.peek()? {
|
2018-05-05 16:41:57 -05:00
|
|
|
Some((_, Token::LeftBracket)) => self.table_header().map(Some),
|
2017-01-29 18:53:20 -06:00
|
|
|
Some(_) => self.key_value().map(Some),
|
|
|
|
None => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn table_header(&mut self) -> Result<Line<'a>, Error> {
|
|
|
|
let start = self.tokens.current();
|
|
|
|
self.expect(Token::LeftBracket)?;
|
|
|
|
let array = self.eat(Token::LeftBracket)?;
|
2018-12-17 19:45:35 -06:00
|
|
|
let ret = Header::new(self.tokens.clone(), array, self.require_newline_after_table);
|
2017-02-08 23:36:38 -06:00
|
|
|
if self.require_newline_after_table {
|
|
|
|
self.tokens.skip_to_newline();
|
|
|
|
} else {
|
|
|
|
loop {
|
|
|
|
match self.next()? {
|
2018-05-05 16:41:57 -05:00
|
|
|
Some((_, Token::RightBracket)) => {
|
2017-03-31 20:45:00 -05:00
|
|
|
if array {
|
|
|
|
self.eat(Token::RightBracket)?;
|
|
|
|
}
|
2018-12-17 19:45:35 -06:00
|
|
|
break;
|
2017-03-31 20:45:00 -05:00
|
|
|
}
|
2018-12-17 19:45:35 -06:00
|
|
|
Some((_, Token::Newline)) | None => break,
|
2017-02-08 23:36:38 -06:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.eat_whitespace()?;
|
|
|
|
}
|
2018-12-17 19:45:35 -06:00
|
|
|
Ok(Line::Table {
|
|
|
|
at: start,
|
|
|
|
header: ret,
|
2019-08-14 21:52:24 -05:00
|
|
|
array,
|
2018-12-17 19:45:35 -06:00
|
|
|
})
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn key_value(&mut self) -> Result<Line<'a>, Error> {
|
2018-07-11 01:29:47 -05:00
|
|
|
let key = self.dotted_key()?;
|
2017-01-29 18:53:20 -06:00
|
|
|
self.eat_whitespace()?;
|
|
|
|
self.expect(Token::Equals)?;
|
|
|
|
self.eat_whitespace()?;
|
|
|
|
|
|
|
|
let value = self.value()?;
|
|
|
|
self.eat_whitespace()?;
|
|
|
|
if !self.eat_comment()? {
|
|
|
|
self.eat_newline_or_eof()?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Line::KeyValue(key, value))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn value(&mut self) -> Result<Value<'a>, Error> {
|
|
|
|
let at = self.tokens.current();
|
|
|
|
let value = match self.next()? {
|
2018-12-17 19:45:35 -06:00
|
|
|
Some((Span { start, end }, Token::String { val, .. })) => Value {
|
|
|
|
e: E::String(val),
|
2019-08-14 21:52:24 -05:00
|
|
|
start,
|
|
|
|
end,
|
2018-12-17 19:45:35 -06:00
|
|
|
},
|
|
|
|
Some((Span { start, end }, Token::Keylike("true"))) => Value {
|
|
|
|
e: E::Boolean(true),
|
2019-08-14 21:52:24 -05:00
|
|
|
start,
|
|
|
|
end,
|
2018-12-17 19:45:35 -06:00
|
|
|
},
|
|
|
|
Some((Span { start, end }, Token::Keylike("false"))) => Value {
|
|
|
|
e: E::Boolean(false),
|
2019-08-14 21:52:24 -05:00
|
|
|
start,
|
|
|
|
end,
|
2018-12-17 19:45:35 -06:00
|
|
|
},
|
2018-05-06 20:47:09 -05:00
|
|
|
Some((span, Token::Keylike(key))) => self.number_or_date(span, key)?,
|
2018-05-06 21:05:05 -05:00
|
|
|
Some((span, Token::Plus)) => self.number_leading_plus(span)?,
|
|
|
|
Some((Span { start, .. }, Token::LeftBrace)) => {
|
|
|
|
self.inline_table().map(|(Span { end, .. }, table)| Value {
|
2018-05-06 20:47:09 -05:00
|
|
|
e: E::InlineTable(table),
|
2019-08-14 21:52:24 -05:00
|
|
|
start,
|
|
|
|
end,
|
2018-05-06 20:47:09 -05:00
|
|
|
})?
|
2018-05-05 16:41:57 -05:00
|
|
|
}
|
2018-05-06 21:05:05 -05:00
|
|
|
Some((Span { start, .. }, Token::LeftBracket)) => {
|
|
|
|
self.array().map(|(Span { end, .. }, array)| Value {
|
|
|
|
e: E::Array(array),
|
2019-08-14 21:52:24 -05:00
|
|
|
start,
|
|
|
|
end,
|
2018-05-06 21:05:05 -05:00
|
|
|
})?
|
2018-05-05 16:41:57 -05:00
|
|
|
}
|
2017-01-29 18:53:20 -06:00
|
|
|
Some(token) => {
|
2018-12-17 19:45:35 -06:00
|
|
|
return Err(self.error(
|
|
|
|
at,
|
|
|
|
ErrorKind::Wanted {
|
|
|
|
expected: "a value",
|
|
|
|
found: token.1.describe(),
|
|
|
|
},
|
|
|
|
))
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
None => return Err(self.eof()),
|
|
|
|
};
|
|
|
|
Ok(value)
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn number_or_date(&mut self, span: Span, s: &'a str) -> Result<Value<'a>, Error> {
|
2019-02-19 12:59:42 -06:00
|
|
|
if s.contains('T')
|
|
|
|
|| s.contains('t')
|
|
|
|
|| (s.len() > 1 && s[1..].contains('-') && !s.contains("e-") && !s.contains("E-"))
|
|
|
|
{
|
2018-12-17 19:45:35 -06:00
|
|
|
self.datetime(span, s, false)
|
|
|
|
.map(|(Span { start, end }, d)| Value {
|
|
|
|
e: E::Datetime(d),
|
2019-08-14 21:52:24 -05:00
|
|
|
start,
|
|
|
|
end,
|
2018-12-17 19:45:35 -06:00
|
|
|
})
|
2017-01-29 18:53:20 -06:00
|
|
|
} else if self.eat(Token::Colon)? {
|
2018-12-17 19:45:35 -06:00
|
|
|
self.datetime(span, s, true)
|
|
|
|
.map(|(Span { start, end }, d)| Value {
|
|
|
|
e: E::Datetime(d),
|
2019-08-14 21:52:24 -05:00
|
|
|
start,
|
|
|
|
end,
|
2018-12-17 19:45:35 -06:00
|
|
|
})
|
2017-01-29 18:53:20 -06:00
|
|
|
} else {
|
2018-05-06 20:47:09 -05:00
|
|
|
self.number(span, s)
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 15:40:16 -05:00
|
|
|
/// Returns a string or table value type.
|
|
|
|
///
|
|
|
|
/// Used to deserialize enums. Unit enums may be represented as a string or a table, all other
|
|
|
|
/// structures (tuple, newtype, struct) must be represented as a table.
|
|
|
|
fn string_or_table(&mut self) -> Result<(Value<'a>, Option<Cow<'a, str>>), Error> {
|
|
|
|
match self.peek()? {
|
2019-07-30 12:20:18 -05:00
|
|
|
Some((span, Token::LeftBracket)) => {
|
2018-10-21 15:40:16 -05:00
|
|
|
let tables = self.tables()?;
|
|
|
|
if tables.len() != 1 {
|
2019-07-30 12:20:18 -05:00
|
|
|
return Err(Error::from_kind(
|
|
|
|
Some(span.start),
|
|
|
|
ErrorKind::Wanted {
|
|
|
|
expected: "exactly 1 table",
|
|
|
|
found: if tables.is_empty() {
|
|
|
|
"zero tables"
|
|
|
|
} else {
|
|
|
|
"more than 1 table"
|
|
|
|
},
|
2018-10-21 15:40:16 -05:00
|
|
|
},
|
2019-07-30 12:20:18 -05:00
|
|
|
));
|
2018-10-21 15:40:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let table = tables
|
|
|
|
.into_iter()
|
|
|
|
.next()
|
|
|
|
.expect("Expected exactly one table");
|
|
|
|
let header = table
|
|
|
|
.header
|
|
|
|
.last()
|
|
|
|
.expect("Expected at least one header value for table.");
|
|
|
|
|
|
|
|
let start = table.at;
|
|
|
|
let end = table
|
|
|
|
.values
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|values| values.last())
|
|
|
|
.map(|&(_, ref val)| val.end)
|
2019-09-16 16:32:45 -05:00
|
|
|
.unwrap_or_else(|| header.1.len());
|
2018-10-21 15:40:16 -05:00
|
|
|
Ok((
|
|
|
|
Value {
|
|
|
|
e: E::DottedTable(table.values.unwrap_or_else(Vec::new)),
|
2019-08-14 21:52:24 -05:00
|
|
|
start,
|
|
|
|
end,
|
2018-10-21 15:40:16 -05:00
|
|
|
},
|
2019-09-16 16:32:45 -05:00
|
|
|
Some(header.1.clone()),
|
2018-10-21 15:40:16 -05:00
|
|
|
))
|
|
|
|
}
|
|
|
|
Some(_) => self.value().map(|val| (val, None)),
|
|
|
|
None => Err(self.eof()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn number(&mut self, Span { start, end }: Span, s: &'a str) -> Result<Value<'a>, Error> {
|
2018-12-17 19:45:35 -06:00
|
|
|
let to_integer = |f| Value {
|
|
|
|
e: E::Integer(f),
|
2019-08-14 21:52:24 -05:00
|
|
|
start,
|
|
|
|
end,
|
2018-12-17 19:45:35 -06:00
|
|
|
};
|
2018-07-10 20:00:12 -05:00
|
|
|
if s.starts_with("0x") {
|
|
|
|
self.integer(&s[2..], 16).map(to_integer)
|
|
|
|
} else if s.starts_with("0o") {
|
|
|
|
self.integer(&s[2..], 8).map(to_integer)
|
|
|
|
} else if s.starts_with("0b") {
|
|
|
|
self.integer(&s[2..], 2).map(to_integer)
|
|
|
|
} else if s.contains('e') || s.contains('E') {
|
2018-12-17 19:45:35 -06:00
|
|
|
self.float(s, None).map(|f| Value {
|
|
|
|
e: E::Float(f),
|
2019-08-14 21:52:24 -05:00
|
|
|
start,
|
|
|
|
end,
|
2018-12-17 19:45:35 -06:00
|
|
|
})
|
2017-01-29 18:53:20 -06:00
|
|
|
} else if self.eat(Token::Period)? {
|
|
|
|
let at = self.tokens.current();
|
|
|
|
match self.next()? {
|
2018-05-06 20:47:09 -05:00
|
|
|
Some((Span { start, end }, Token::Keylike(after))) => {
|
|
|
|
self.float(s, Some(after)).map(|f| Value {
|
2018-12-17 19:45:35 -06:00
|
|
|
e: E::Float(f),
|
2019-08-14 21:52:24 -05:00
|
|
|
start,
|
|
|
|
end,
|
2018-05-06 20:47:09 -05:00
|
|
|
})
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
_ => Err(self.error(at, ErrorKind::NumberInvalid)),
|
|
|
|
}
|
2018-07-10 18:27:58 -05:00
|
|
|
} else if s == "inf" {
|
2018-12-17 19:45:35 -06:00
|
|
|
Ok(Value {
|
|
|
|
e: E::Float(f64::INFINITY),
|
2019-08-14 21:52:24 -05:00
|
|
|
start,
|
|
|
|
end,
|
2018-12-17 19:45:35 -06:00
|
|
|
})
|
2018-07-10 18:27:58 -05:00
|
|
|
} else if s == "-inf" {
|
2018-12-17 19:45:35 -06:00
|
|
|
Ok(Value {
|
|
|
|
e: E::Float(f64::NEG_INFINITY),
|
2019-08-14 21:52:24 -05:00
|
|
|
start,
|
|
|
|
end,
|
2018-12-17 19:45:35 -06:00
|
|
|
})
|
2018-07-10 18:27:58 -05:00
|
|
|
} else if s == "nan" {
|
2018-12-17 19:45:35 -06:00
|
|
|
Ok(Value {
|
|
|
|
e: E::Float(f64::NAN),
|
2019-08-14 21:52:24 -05:00
|
|
|
start,
|
|
|
|
end,
|
2018-12-17 19:45:35 -06:00
|
|
|
})
|
2018-07-10 18:27:58 -05:00
|
|
|
} else if s == "-nan" {
|
2018-12-17 19:45:35 -06:00
|
|
|
Ok(Value {
|
|
|
|
e: E::Float(-f64::NAN),
|
2019-08-14 21:52:24 -05:00
|
|
|
start,
|
|
|
|
end,
|
2018-12-17 19:45:35 -06:00
|
|
|
})
|
2017-01-29 18:53:20 -06:00
|
|
|
} else {
|
2018-07-10 20:00:12 -05:00
|
|
|
self.integer(s, 10).map(to_integer)
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-06 21:05:05 -05:00
|
|
|
fn number_leading_plus(&mut self, Span { start, .. }: Span) -> Result<Value<'a>, Error> {
|
|
|
|
let start_token = self.tokens.current();
|
2017-01-29 18:53:20 -06:00
|
|
|
match self.next()? {
|
2019-08-14 21:52:24 -05:00
|
|
|
Some((Span { end, .. }, Token::Keylike(s))) => self.number(Span { start, end }, s),
|
2018-05-06 21:05:05 -05:00
|
|
|
_ => Err(self.error(start_token, ErrorKind::NumberInvalid)),
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 20:00:12 -05:00
|
|
|
fn integer(&self, s: &'a str, radix: u32) -> Result<i64, Error> {
|
|
|
|
let allow_sign = radix == 10;
|
|
|
|
let allow_leading_zeros = radix != 10;
|
|
|
|
let (prefix, suffix) = self.parse_integer(s, allow_sign, allow_leading_zeros, radix)?;
|
2017-01-29 18:53:20 -06:00
|
|
|
let start = self.tokens.substr_offset(s);
|
|
|
|
if suffix != "" {
|
2018-12-17 19:45:35 -06:00
|
|
|
return Err(self.error(start, ErrorKind::NumberInvalid));
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
2019-04-02 10:35:28 -05:00
|
|
|
i64::from_str_radix(&prefix.replace("_", "").trim_start_matches('+'), radix)
|
2018-07-10 20:00:12 -05:00
|
|
|
.map_err(|_e| self.error(start, ErrorKind::NumberInvalid))
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2018-07-10 20:00:12 -05:00
|
|
|
fn parse_integer(
|
|
|
|
&self,
|
|
|
|
s: &'a str,
|
|
|
|
allow_sign: bool,
|
|
|
|
allow_leading_zeros: bool,
|
|
|
|
radix: u32,
|
|
|
|
) -> Result<(&'a str, &'a str), Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
let start = self.tokens.substr_offset(s);
|
|
|
|
|
|
|
|
let mut first = true;
|
|
|
|
let mut first_zero = false;
|
|
|
|
let mut underscore = false;
|
|
|
|
let mut end = s.len();
|
|
|
|
for (i, c) in s.char_indices() {
|
|
|
|
let at = i + start;
|
|
|
|
if i == 0 && (c == '+' || c == '-') && allow_sign {
|
2018-12-17 19:45:35 -06:00
|
|
|
continue;
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2018-07-10 20:00:12 -05:00
|
|
|
if c == '0' && first {
|
|
|
|
first_zero = true;
|
2020-02-25 12:08:29 -06:00
|
|
|
} else if c.is_digit(radix) {
|
2018-07-10 20:00:12 -05:00
|
|
|
if !first && first_zero && !allow_leading_zeros {
|
|
|
|
return Err(self.error(at, ErrorKind::NumberInvalid));
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
2018-07-10 20:00:12 -05:00
|
|
|
underscore = false;
|
|
|
|
} else if c == '_' && first {
|
|
|
|
return Err(self.error(at, ErrorKind::NumberInvalid));
|
|
|
|
} else if c == '_' && !underscore {
|
|
|
|
underscore = true;
|
|
|
|
} else {
|
|
|
|
end = i;
|
|
|
|
break;
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
if first || underscore {
|
2018-12-17 19:45:35 -06:00
|
|
|
return Err(self.error(start, ErrorKind::NumberInvalid));
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
Ok((&s[..end], &s[end..]))
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn float(&mut self, s: &'a str, after_decimal: Option<&'a str>) -> Result<f64, Error> {
|
2018-07-10 20:00:12 -05:00
|
|
|
let (integral, mut suffix) = self.parse_integer(s, true, false, 10)?;
|
2017-01-29 18:53:20 -06:00
|
|
|
let start = self.tokens.substr_offset(integral);
|
|
|
|
|
|
|
|
let mut fraction = None;
|
|
|
|
if let Some(after) = after_decimal {
|
|
|
|
if suffix != "" {
|
2018-12-17 19:45:35 -06:00
|
|
|
return Err(self.error(start, ErrorKind::NumberInvalid));
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
2018-07-10 20:00:12 -05:00
|
|
|
let (a, b) = self.parse_integer(after, false, true, 10)?;
|
2017-01-29 18:53:20 -06:00
|
|
|
fraction = Some(a);
|
|
|
|
suffix = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut exponent = None;
|
2017-03-30 06:40:27 -05:00
|
|
|
if suffix.starts_with('e') || suffix.starts_with('E') {
|
2017-01-29 18:53:20 -06:00
|
|
|
let (a, b) = if suffix.len() == 1 {
|
|
|
|
self.eat(Token::Plus)?;
|
|
|
|
match self.next()? {
|
2019-08-23 11:55:15 -05:00
|
|
|
Some((_, Token::Keylike(s))) => self.parse_integer(s, false, true, 10)?,
|
2017-01-29 18:53:20 -06:00
|
|
|
_ => return Err(self.error(start, ErrorKind::NumberInvalid)),
|
|
|
|
}
|
|
|
|
} else {
|
2019-08-23 11:55:15 -05:00
|
|
|
self.parse_integer(&suffix[1..], true, true, 10)?
|
2017-01-29 18:53:20 -06:00
|
|
|
};
|
|
|
|
if b != "" {
|
2018-12-17 19:45:35 -06:00
|
|
|
return Err(self.error(start, ErrorKind::NumberInvalid));
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
exponent = Some(a);
|
2019-04-05 15:51:28 -05:00
|
|
|
} else if !suffix.is_empty() {
|
|
|
|
return Err(self.error(start, ErrorKind::NumberInvalid));
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
let mut number = integral
|
2019-04-02 10:35:28 -05:00
|
|
|
.trim_start_matches('+')
|
2018-12-17 19:45:35 -06:00
|
|
|
.chars()
|
|
|
|
.filter(|c| *c != '_')
|
|
|
|
.collect::<String>();
|
2017-01-29 18:53:20 -06:00
|
|
|
if let Some(fraction) = fraction {
|
|
|
|
number.push_str(".");
|
|
|
|
number.extend(fraction.chars().filter(|c| *c != '_'));
|
|
|
|
}
|
|
|
|
if let Some(exponent) = exponent {
|
|
|
|
number.push_str("E");
|
|
|
|
number.extend(exponent.chars().filter(|c| *c != '_'));
|
|
|
|
}
|
2018-12-17 19:45:35 -06:00
|
|
|
number
|
|
|
|
.parse()
|
|
|
|
.map_err(|_e| self.error(start, ErrorKind::NumberInvalid))
|
|
|
|
.and_then(|n: f64| {
|
|
|
|
if n.is_finite() {
|
|
|
|
Ok(n)
|
|
|
|
} else {
|
|
|
|
Err(self.error(start, ErrorKind::NumberInvalid))
|
|
|
|
}
|
|
|
|
})
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:35 -06:00
|
|
|
fn datetime(
|
|
|
|
&mut self,
|
|
|
|
mut span: Span,
|
|
|
|
date: &'a str,
|
|
|
|
colon_eaten: bool,
|
|
|
|
) -> Result<(Span, &'a str), Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
let start = self.tokens.substr_offset(date);
|
2018-05-06 22:36:09 -05:00
|
|
|
|
2018-07-10 19:14:16 -05:00
|
|
|
// Check for space separated date and time.
|
2018-11-21 11:35:50 -06:00
|
|
|
let mut lookahead = self.tokens.clone();
|
|
|
|
if let Ok(Some((_, Token::Whitespace(" ")))) = lookahead.next() {
|
|
|
|
// Check if hour follows.
|
|
|
|
if let Ok(Some((_, Token::Keylike(_)))) = lookahead.next() {
|
2018-12-17 19:45:35 -06:00
|
|
|
self.next()?; // skip space
|
|
|
|
self.next()?; // skip keylike hour
|
2018-07-10 19:14:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-29 18:53:20 -06:00
|
|
|
if colon_eaten || self.eat(Token::Colon)? {
|
|
|
|
// minutes
|
|
|
|
match self.next()? {
|
2018-05-05 16:41:57 -05:00
|
|
|
Some((_, Token::Keylike(_))) => {}
|
2017-01-29 18:53:20 -06:00
|
|
|
_ => return Err(self.error(start, ErrorKind::DateInvalid)),
|
|
|
|
}
|
|
|
|
// Seconds
|
|
|
|
self.expect(Token::Colon)?;
|
|
|
|
match self.next()? {
|
2018-05-06 22:36:09 -05:00
|
|
|
Some((Span { end, .. }, Token::Keylike(_))) => {
|
|
|
|
span.end = end;
|
2018-12-17 19:45:35 -06:00
|
|
|
}
|
2017-01-29 18:53:20 -06:00
|
|
|
_ => return Err(self.error(start, ErrorKind::DateInvalid)),
|
|
|
|
}
|
|
|
|
// Fractional seconds
|
|
|
|
if self.eat(Token::Period)? {
|
|
|
|
match self.next()? {
|
2018-05-06 22:36:09 -05:00
|
|
|
Some((Span { end, .. }, Token::Keylike(_))) => {
|
|
|
|
span.end = end;
|
2018-12-17 19:45:35 -06:00
|
|
|
}
|
2017-01-29 18:53:20 -06:00
|
|
|
_ => return Err(self.error(start, ErrorKind::DateInvalid)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// offset
|
|
|
|
if self.eat(Token::Plus)? {
|
|
|
|
match self.next()? {
|
2018-05-06 22:36:09 -05:00
|
|
|
Some((Span { end, .. }, Token::Keylike(_))) => {
|
|
|
|
span.end = end;
|
2018-12-17 19:45:35 -06:00
|
|
|
}
|
2017-01-29 18:53:20 -06:00
|
|
|
_ => return Err(self.error(start, ErrorKind::DateInvalid)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if self.eat(Token::Colon)? {
|
|
|
|
match self.next()? {
|
2018-05-06 22:36:09 -05:00
|
|
|
Some((Span { end, .. }, Token::Keylike(_))) => {
|
|
|
|
span.end = end;
|
2018-12-17 19:45:35 -06:00
|
|
|
}
|
2017-01-29 18:53:20 -06:00
|
|
|
_ => return Err(self.error(start, ErrorKind::DateInvalid)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-06 22:36:09 -05:00
|
|
|
|
2017-01-29 18:53:20 -06:00
|
|
|
let end = self.tokens.current();
|
2018-05-06 22:36:09 -05:00
|
|
|
Ok((span, &self.tokens.input()[start..end]))
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(#140): shouldn't buffer up this entire table in memory, it'd be
|
|
|
|
// great to defer parsing everything until later.
|
2019-08-18 20:07:23 -05:00
|
|
|
fn inline_table(&mut self) -> Result<(Span, Vec<TablePair<'a>>), Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
let mut ret = Vec::new();
|
|
|
|
self.eat_whitespace()?;
|
2018-05-06 21:05:05 -05:00
|
|
|
if let Some(span) = self.eat_spanned(Token::RightBrace)? {
|
2018-12-17 19:45:35 -06:00
|
|
|
return Ok((span, ret));
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
loop {
|
2018-07-11 01:29:47 -05:00
|
|
|
let key = self.dotted_key()?;
|
2017-01-29 18:53:20 -06:00
|
|
|
self.eat_whitespace()?;
|
|
|
|
self.expect(Token::Equals)?;
|
|
|
|
self.eat_whitespace()?;
|
2018-07-11 01:29:47 -05:00
|
|
|
let value = self.value()?;
|
|
|
|
self.add_dotted_key(key, value, &mut ret)?;
|
2017-01-29 18:53:20 -06:00
|
|
|
|
|
|
|
self.eat_whitespace()?;
|
2018-05-06 21:05:05 -05:00
|
|
|
if let Some(span) = self.eat_spanned(Token::RightBrace)? {
|
2018-12-17 19:45:35 -06:00
|
|
|
return Ok((span, ret));
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
self.expect(Token::Comma)?;
|
|
|
|
self.eat_whitespace()?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(#140): shouldn't buffer up this entire array in memory, it'd be
|
|
|
|
// great to defer parsing everything until later.
|
2018-05-06 21:05:05 -05:00
|
|
|
fn array(&mut self) -> Result<(Span, Vec<Value<'a>>), Error> {
|
2017-01-29 18:53:20 -06:00
|
|
|
let mut ret = Vec::new();
|
|
|
|
|
2019-05-08 19:37:38 -05:00
|
|
|
let intermediate = |me: &mut Deserializer<'_>| {
|
2017-01-29 18:53:20 -06:00
|
|
|
loop {
|
|
|
|
me.eat_whitespace()?;
|
|
|
|
if !me.eat(Token::Newline)? && !me.eat_comment()? {
|
2018-12-17 19:45:35 -06:00
|
|
|
break;
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
|
|
|
|
loop {
|
|
|
|
intermediate(self)?;
|
2018-05-06 21:05:05 -05:00
|
|
|
if let Some(span) = self.eat_spanned(Token::RightBracket)? {
|
2018-12-17 19:45:35 -06:00
|
|
|
return Ok((span, ret));
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
let value = self.value()?;
|
|
|
|
ret.push(value);
|
|
|
|
intermediate(self)?;
|
|
|
|
if !self.eat(Token::Comma)? {
|
2018-12-17 19:45:35 -06:00
|
|
|
break;
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
intermediate(self)?;
|
2018-05-06 21:05:05 -05:00
|
|
|
let span = self.expect_spanned(Token::RightBracket)?;
|
|
|
|
Ok((span, ret))
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2019-09-16 16:32:45 -05:00
|
|
|
fn table_key(&mut self) -> Result<(Span, Cow<'a, str>), Error> {
|
|
|
|
self.tokens.table_key().map_err(|e| self.token_error(e))
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2019-09-16 16:32:45 -05:00
|
|
|
fn dotted_key(&mut self) -> Result<Vec<(Span, Cow<'a, str>)>, Error> {
|
2018-07-11 01:29:47 -05:00
|
|
|
let mut result = Vec::new();
|
|
|
|
result.push(self.table_key()?);
|
|
|
|
self.eat_whitespace()?;
|
|
|
|
while self.eat(Token::Period)? {
|
|
|
|
self.eat_whitespace()?;
|
|
|
|
result.push(self.table_key()?);
|
|
|
|
self.eat_whitespace()?;
|
|
|
|
}
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
2018-10-21 15:40:16 -05:00
|
|
|
/// Stores a value in the appropriate hierachical structure positioned based on the dotted key.
|
|
|
|
///
|
|
|
|
/// Given the following definition: `multi.part.key = "value"`, `multi` and `part` are
|
|
|
|
/// intermediate parts which are mapped to the relevant fields in the deserialized type's data
|
|
|
|
/// hierarchy.
|
|
|
|
///
|
|
|
|
/// # Parameters
|
|
|
|
///
|
|
|
|
/// * `key_parts`: Each segment of the dotted key, e.g. `part.one` maps to
|
|
|
|
/// `vec![Cow::Borrowed("part"), Cow::Borrowed("one")].`
|
|
|
|
/// * `value`: The parsed value.
|
|
|
|
/// * `values`: The `Vec` to store the value in.
|
2018-07-11 01:29:47 -05:00
|
|
|
fn add_dotted_key(
|
|
|
|
&self,
|
2019-09-16 16:32:45 -05:00
|
|
|
mut key_parts: Vec<(Span, Cow<'a, str>)>,
|
2018-07-11 01:29:47 -05:00
|
|
|
value: Value<'a>,
|
2019-08-18 20:07:23 -05:00
|
|
|
values: &mut Vec<TablePair<'a>>,
|
2018-07-11 01:29:47 -05:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
let key = key_parts.remove(0);
|
|
|
|
if key_parts.is_empty() {
|
|
|
|
values.push((key, value));
|
|
|
|
return Ok(());
|
|
|
|
}
|
2019-09-16 16:32:45 -05:00
|
|
|
match values.iter_mut().find(|&&mut (ref k, _)| *k.1 == key.1) {
|
2018-12-17 19:45:35 -06:00
|
|
|
Some(&mut (
|
|
|
|
_,
|
|
|
|
Value {
|
|
|
|
e: E::DottedTable(ref mut v),
|
|
|
|
..
|
|
|
|
},
|
|
|
|
)) => {
|
2018-07-11 01:29:47 -05:00
|
|
|
return self.add_dotted_key(key_parts, value, v);
|
|
|
|
}
|
2018-07-11 02:13:47 -05:00
|
|
|
Some(&mut (_, Value { start, .. })) => {
|
|
|
|
return Err(self.error(start, ErrorKind::DottedKeyInvalidType));
|
2018-07-11 01:29:47 -05:00
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
// The start/end value is somewhat misleading here.
|
2018-10-21 15:40:16 -05:00
|
|
|
let table_values = Value {
|
2018-07-27 13:49:30 -05:00
|
|
|
e: E::DottedTable(Vec::new()),
|
2018-07-11 01:29:47 -05:00
|
|
|
start: value.start,
|
|
|
|
end: value.end,
|
|
|
|
};
|
2018-10-21 15:40:16 -05:00
|
|
|
values.push((key, table_values));
|
2018-07-11 01:29:47 -05:00
|
|
|
let last_i = values.len() - 1;
|
2018-12-17 19:45:35 -06:00
|
|
|
if let (
|
|
|
|
_,
|
|
|
|
Value {
|
|
|
|
e: E::DottedTable(ref mut v),
|
|
|
|
..
|
|
|
|
},
|
|
|
|
) = values[last_i]
|
|
|
|
{
|
2018-07-11 01:29:47 -05:00
|
|
|
self.add_dotted_key(key_parts, value, v)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-29 18:53:20 -06:00
|
|
|
fn eat_whitespace(&mut self) -> Result<(), Error> {
|
2018-12-17 19:45:35 -06:00
|
|
|
self.tokens
|
|
|
|
.eat_whitespace()
|
|
|
|
.map_err(|e| self.token_error(e))
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn eat_comment(&mut self) -> Result<bool, Error> {
|
|
|
|
self.tokens.eat_comment().map_err(|e| self.token_error(e))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eat_newline_or_eof(&mut self) -> Result<(), Error> {
|
2018-12-17 19:45:35 -06:00
|
|
|
self.tokens
|
|
|
|
.eat_newline_or_eof()
|
|
|
|
.map_err(|e| self.token_error(e))
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn eat(&mut self, expected: Token<'a>) -> Result<bool, Error> {
|
|
|
|
self.tokens.eat(expected).map_err(|e| self.token_error(e))
|
|
|
|
}
|
|
|
|
|
2018-05-06 21:05:05 -05:00
|
|
|
fn eat_spanned(&mut self, expected: Token<'a>) -> Result<Option<Span>, Error> {
|
2018-12-17 19:45:35 -06:00
|
|
|
self.tokens
|
|
|
|
.eat_spanned(expected)
|
|
|
|
.map_err(|e| self.token_error(e))
|
2018-05-06 21:05:05 -05:00
|
|
|
}
|
|
|
|
|
2017-01-29 18:53:20 -06:00
|
|
|
fn expect(&mut self, expected: Token<'a>) -> Result<(), Error> {
|
2018-12-17 19:45:35 -06:00
|
|
|
self.tokens
|
|
|
|
.expect(expected)
|
|
|
|
.map_err(|e| self.token_error(e))
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2018-05-06 21:05:05 -05:00
|
|
|
fn expect_spanned(&mut self, expected: Token<'a>) -> Result<Span, Error> {
|
2018-12-17 19:45:35 -06:00
|
|
|
self.tokens
|
|
|
|
.expect_spanned(expected)
|
|
|
|
.map_err(|e| self.token_error(e))
|
2018-05-06 21:05:05 -05:00
|
|
|
}
|
|
|
|
|
2018-05-05 16:41:57 -05:00
|
|
|
fn next(&mut self) -> Result<Option<(Span, Token<'a>)>, Error> {
|
|
|
|
self.tokens.next().map_err(|e| self.token_error(e))
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2018-05-05 16:41:57 -05:00
|
|
|
fn peek(&mut self) -> Result<Option<(Span, Token<'a>)>, Error> {
|
|
|
|
self.tokens.peek().map_err(|e| self.token_error(e))
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn eof(&self) -> Error {
|
|
|
|
self.error(self.input.len(), ErrorKind::UnexpectedEof)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn token_error(&self, error: TokenError) -> Error {
|
|
|
|
match error {
|
|
|
|
TokenError::InvalidCharInString(at, ch) => {
|
|
|
|
self.error(at, ErrorKind::InvalidCharInString(ch))
|
|
|
|
}
|
2018-12-17 19:45:35 -06:00
|
|
|
TokenError::InvalidEscape(at, ch) => self.error(at, ErrorKind::InvalidEscape(ch)),
|
2017-01-29 18:53:20 -06:00
|
|
|
TokenError::InvalidEscapeValue(at, v) => {
|
|
|
|
self.error(at, ErrorKind::InvalidEscapeValue(v))
|
|
|
|
}
|
2018-12-17 19:45:35 -06:00
|
|
|
TokenError::InvalidHexEscape(at, ch) => self.error(at, ErrorKind::InvalidHexEscape(ch)),
|
|
|
|
TokenError::NewlineInString(at) => self.error(at, ErrorKind::NewlineInString),
|
|
|
|
TokenError::Unexpected(at, ch) => self.error(at, ErrorKind::Unexpected(ch)),
|
|
|
|
TokenError::UnterminatedString(at) => self.error(at, ErrorKind::UnterminatedString),
|
|
|
|
TokenError::NewlineInTableKey(at) => self.error(at, ErrorKind::NewlineInTableKey),
|
|
|
|
TokenError::Wanted {
|
|
|
|
at,
|
|
|
|
expected,
|
|
|
|
found,
|
2019-08-14 21:52:24 -05:00
|
|
|
} => self.error(at, ErrorKind::Wanted { expected, found }),
|
2018-12-17 19:45:35 -06:00
|
|
|
TokenError::EmptyTableKey(at) => self.error(at, ErrorKind::EmptyTableKey),
|
|
|
|
TokenError::MultilineStringKey(at) => self.error(at, ErrorKind::MultilineStringKey),
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn error(&self, at: usize, kind: ErrorKind) -> Error {
|
2019-07-30 12:20:18 -05:00
|
|
|
let mut err = Error::from_kind(Some(at), kind);
|
|
|
|
err.fix_linecol(|at| self.to_linecol(at));
|
2017-03-30 06:38:48 -05:00
|
|
|
err
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts a byte offset from an error message to a (line, column) pair
|
|
|
|
///
|
|
|
|
/// All indexes are 0-based.
|
|
|
|
fn to_linecol(&self, offset: usize) -> (usize, usize) {
|
|
|
|
let mut cur = 0;
|
2019-08-13 15:48:54 -05:00
|
|
|
// Use split_terminator instead of lines so that if there is a `\r`,
|
|
|
|
// it is included in the offset calculation. The `+1` values below
|
|
|
|
// account for the `\n`.
|
|
|
|
for (i, line) in self.input.split_terminator('\n').enumerate() {
|
2017-01-29 18:53:20 -06:00
|
|
|
if cur + line.len() + 1 > offset {
|
2018-12-17 19:45:35 -06:00
|
|
|
return (i, offset - cur);
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
cur += line.len() + 1;
|
|
|
|
}
|
|
|
|
(self.input.lines().count(), 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error {
|
2017-05-09 04:22:18 -05:00
|
|
|
/// Produces a (line, column) pair of the position of the error if available
|
2017-05-09 10:08:26 -05:00
|
|
|
///
|
|
|
|
/// All indexes are 0-based.
|
2017-05-09 04:22:18 -05:00
|
|
|
pub fn line_col(&self) -> Option<(usize, usize)> {
|
|
|
|
self.inner.line.map(|line| (line, self.inner.col))
|
|
|
|
}
|
|
|
|
|
2019-07-30 12:20:18 -05:00
|
|
|
fn from_kind(at: Option<usize>, kind: ErrorKind) -> Error {
|
2017-01-29 18:53:20 -06:00
|
|
|
Error {
|
|
|
|
inner: Box::new(ErrorInner {
|
2019-08-14 21:52:24 -05:00
|
|
|
kind,
|
2017-01-29 18:53:20 -06:00
|
|
|
line: None,
|
|
|
|
col: 0,
|
2019-07-30 12:20:18 -05:00
|
|
|
at,
|
2017-01-29 18:53:20 -06:00
|
|
|
message: String::new(),
|
|
|
|
key: Vec::new(),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 12:20:18 -05:00
|
|
|
fn custom(at: Option<usize>, s: String) -> Error {
|
2017-01-29 18:53:20 -06:00
|
|
|
Error {
|
|
|
|
inner: Box::new(ErrorInner {
|
|
|
|
kind: ErrorKind::Custom,
|
|
|
|
line: None,
|
|
|
|
col: 0,
|
2019-07-30 12:20:18 -05:00
|
|
|
at,
|
2017-01-29 18:53:20 -06:00
|
|
|
message: s,
|
|
|
|
key: Vec::new(),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 13:04:47 -05:00
|
|
|
pub(crate) fn add_key_context(&mut self, key: &str) {
|
2017-01-29 18:53:20 -06:00
|
|
|
self.inner.key.insert(0, key.to_string());
|
|
|
|
}
|
2019-07-30 12:20:18 -05:00
|
|
|
|
2019-08-14 21:52:24 -05:00
|
|
|
fn fix_offset<F>(&mut self, f: F)
|
2019-07-30 12:20:18 -05:00
|
|
|
where
|
|
|
|
F: FnOnce() -> Option<usize>,
|
|
|
|
{
|
|
|
|
// An existing offset is always better positioned than anything we
|
|
|
|
// might want to add later.
|
|
|
|
if self.inner.at.is_none() {
|
|
|
|
self.inner.at = f();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 21:52:24 -05:00
|
|
|
fn fix_linecol<F>(&mut self, f: F)
|
2019-07-30 12:20:18 -05:00
|
|
|
where
|
|
|
|
F: FnOnce(usize) -> (usize, usize),
|
|
|
|
{
|
|
|
|
if let Some(at) = self.inner.at {
|
|
|
|
let (line, col) = f(at);
|
|
|
|
self.inner.line = Some(line);
|
|
|
|
self.inner.col = col;
|
|
|
|
}
|
|
|
|
}
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2019-08-12 15:50:44 -05:00
|
|
|
impl std::convert::From<Error> for std::io::Error {
|
|
|
|
fn from(e: Error) -> Self {
|
2019-08-14 21:52:24 -05:00
|
|
|
std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string())
|
2019-08-12 15:50:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-29 18:53:20 -06:00
|
|
|
impl fmt::Display for Error {
|
2019-05-08 19:37:38 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2017-01-29 18:53:20 -06:00
|
|
|
match self.inner.kind {
|
|
|
|
ErrorKind::UnexpectedEof => "unexpected eof encountered".fmt(f)?,
|
2018-12-17 19:45:35 -06:00
|
|
|
ErrorKind::InvalidCharInString(c) => write!(
|
|
|
|
f,
|
|
|
|
"invalid character in string: `{}`",
|
|
|
|
c.escape_default().collect::<String>()
|
|
|
|
)?,
|
|
|
|
ErrorKind::InvalidEscape(c) => write!(
|
|
|
|
f,
|
|
|
|
"invalid escape character in string: `{}`",
|
|
|
|
c.escape_default().collect::<String>()
|
|
|
|
)?,
|
|
|
|
ErrorKind::InvalidHexEscape(c) => write!(
|
|
|
|
f,
|
|
|
|
"invalid hex escape character in string: `{}`",
|
|
|
|
c.escape_default().collect::<String>()
|
|
|
|
)?,
|
|
|
|
ErrorKind::InvalidEscapeValue(c) => write!(f, "invalid escape value: `{}`", c)?,
|
2017-01-29 18:53:20 -06:00
|
|
|
ErrorKind::NewlineInString => "newline in string found".fmt(f)?,
|
2018-12-17 19:45:35 -06:00
|
|
|
ErrorKind::Unexpected(ch) => write!(
|
|
|
|
f,
|
|
|
|
"unexpected character found: `{}`",
|
|
|
|
ch.escape_default().collect::<String>()
|
|
|
|
)?,
|
2017-01-29 18:53:20 -06:00
|
|
|
ErrorKind::UnterminatedString => "unterminated string".fmt(f)?,
|
|
|
|
ErrorKind::NewlineInTableKey => "found newline in table key".fmt(f)?,
|
|
|
|
ErrorKind::Wanted { expected, found } => {
|
|
|
|
write!(f, "expected {}, found {}", expected, found)?
|
|
|
|
}
|
|
|
|
ErrorKind::NumberInvalid => "invalid number".fmt(f)?,
|
|
|
|
ErrorKind::DateInvalid => "invalid date".fmt(f)?,
|
|
|
|
ErrorKind::DuplicateTable(ref s) => {
|
|
|
|
write!(f, "redefinition of table `{}`", s)?;
|
|
|
|
}
|
|
|
|
ErrorKind::RedefineAsArray => "table redefined as array".fmt(f)?,
|
|
|
|
ErrorKind::EmptyTableKey => "empty table key found".fmt(f)?,
|
2018-09-25 02:33:52 -05:00
|
|
|
ErrorKind::MultilineStringKey => "multiline strings are not allowed for key".fmt(f)?,
|
2017-01-29 18:53:20 -06:00
|
|
|
ErrorKind::Custom => self.inner.message.fmt(f)?,
|
2018-10-19 22:24:16 -05:00
|
|
|
ErrorKind::ExpectedTuple(l) => write!(f, "expected table with length {}", l)?,
|
|
|
|
ErrorKind::ExpectedTupleIndex {
|
|
|
|
expected,
|
|
|
|
ref found,
|
|
|
|
} => write!(f, "expected table key `{}`, but was `{}`", expected, found)?,
|
2018-10-09 17:36:46 -05:00
|
|
|
ErrorKind::ExpectedEmptyTable => "expected empty table".fmt(f)?,
|
|
|
|
ErrorKind::DottedKeyInvalidType => {
|
|
|
|
"dotted key attempted to extend non-table type".fmt(f)?
|
|
|
|
}
|
2018-12-17 19:45:35 -06:00
|
|
|
ErrorKind::UnexpectedKeys {
|
|
|
|
ref keys,
|
|
|
|
available,
|
|
|
|
} => write!(
|
|
|
|
f,
|
|
|
|
"unexpected keys in table: `{:?}`, available keys: `{:?}`",
|
|
|
|
keys, available
|
|
|
|
)?,
|
2017-01-29 18:53:20 -06:00
|
|
|
ErrorKind::__Nonexhaustive => panic!(),
|
|
|
|
}
|
|
|
|
|
2017-03-30 06:39:46 -05:00
|
|
|
if !self.inner.key.is_empty() {
|
2017-01-29 18:53:20 -06:00
|
|
|
write!(f, " for key `")?;
|
|
|
|
for (i, k) in self.inner.key.iter().enumerate() {
|
|
|
|
if i > 0 {
|
|
|
|
write!(f, ".")?;
|
|
|
|
}
|
|
|
|
write!(f, "{}", k)?;
|
|
|
|
}
|
|
|
|
write!(f, "`")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(line) = self.inner.line {
|
2019-07-28 12:40:49 -05:00
|
|
|
write!(f, " at line {} column {}", line + 1, self.inner.col + 1)?;
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-21 17:06:00 -06:00
|
|
|
impl error::Error for Error {}
|
2017-01-29 18:53:20 -06:00
|
|
|
|
|
|
|
impl de::Error for Error {
|
|
|
|
fn custom<T: fmt::Display>(msg: T) -> Error {
|
2019-07-30 12:20:18 -05:00
|
|
|
Error::custom(None, msg.to_string())
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Line<'a> {
|
2018-12-17 19:45:35 -06:00
|
|
|
Table {
|
|
|
|
at: usize,
|
|
|
|
header: Header<'a>,
|
|
|
|
array: bool,
|
|
|
|
},
|
2019-09-16 16:32:45 -05:00
|
|
|
KeyValue(Vec<(Span, Cow<'a, str>)>, Value<'a>),
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Header<'a> {
|
|
|
|
first: bool,
|
|
|
|
array: bool,
|
2017-02-08 23:36:38 -06:00
|
|
|
require_newline_after_table: bool,
|
2017-01-29 18:53:20 -06:00
|
|
|
tokens: Tokenizer<'a>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Header<'a> {
|
2018-12-17 19:45:35 -06:00
|
|
|
fn new(tokens: Tokenizer<'a>, array: bool, require_newline_after_table: bool) -> Header<'a> {
|
2017-01-29 18:53:20 -06:00
|
|
|
Header {
|
|
|
|
first: true,
|
2019-08-14 21:52:24 -05:00
|
|
|
array,
|
|
|
|
tokens,
|
|
|
|
require_newline_after_table,
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 16:32:45 -05:00
|
|
|
fn next(&mut self) -> Result<Option<(Span, Cow<'a, str>)>, TokenError> {
|
2017-01-29 18:53:20 -06:00
|
|
|
self.tokens.eat_whitespace()?;
|
|
|
|
|
|
|
|
if self.first || self.tokens.eat(Token::Period)? {
|
|
|
|
self.first = false;
|
|
|
|
self.tokens.eat_whitespace()?;
|
2019-09-16 16:32:45 -05:00
|
|
|
self.tokens.table_key().map(|t| t).map(Some)
|
2017-01-29 18:53:20 -06:00
|
|
|
} else {
|
|
|
|
self.tokens.expect(Token::RightBracket)?;
|
|
|
|
if self.array {
|
|
|
|
self.tokens.expect(Token::RightBracket)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.tokens.eat_whitespace()?;
|
2019-08-14 21:52:24 -05:00
|
|
|
if self.require_newline_after_table && !self.tokens.eat_comment()? {
|
|
|
|
self.tokens.eat_newline_or_eof()?;
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-24 23:57:35 -05:00
|
|
|
#[derive(Debug)]
|
2018-05-05 16:41:57 -05:00
|
|
|
struct Value<'a> {
|
|
|
|
e: E<'a>,
|
|
|
|
start: usize,
|
|
|
|
end: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum E<'a> {
|
2017-01-29 18:53:20 -06:00
|
|
|
Integer(i64),
|
|
|
|
Float(f64),
|
|
|
|
Boolean(bool),
|
|
|
|
String(Cow<'a, str>),
|
|
|
|
Datetime(&'a str),
|
|
|
|
Array(Vec<Value<'a>>),
|
2019-08-18 20:07:23 -05:00
|
|
|
InlineTable(Vec<TablePair<'a>>),
|
|
|
|
DottedTable(Vec<TablePair<'a>>),
|
2017-01-29 18:53:20 -06:00
|
|
|
}
|
|
|
|
|
2018-10-09 14:59:46 -05:00
|
|
|
impl<'a> E<'a> {
|
|
|
|
fn type_name(&self) -> &'static str {
|
|
|
|
match *self {
|
|
|
|
E::String(..) => "string",
|
|
|
|
E::Integer(..) => "integer",
|
|
|
|
E::Float(..) => "float",
|
|
|
|
E::Boolean(..) => "boolean",
|
|
|
|
E::Datetime(..) => "datetime",
|
|
|
|
E::Array(..) => "array",
|
|
|
|
E::InlineTable(..) => "inline table",
|
|
|
|
E::DottedTable(..) => "dotted table",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|