Run cargo fmt

This commit is contained in:
Alex Crichton 2018-12-17 17:45:35 -08:00
parent b2013e4548
commit cbfc4e18f8
8 changed files with 1097 additions and 792 deletions

View file

@ -1,24 +1,24 @@
#![deny(warnings)]
extern crate toml;
extern crate serde_json;
extern crate toml;
use std::fs::File;
use std::env;
use std::fs::File;
use std::io;
use std::io::prelude::*;
use toml::Value as Toml;
use serde_json::Value as Json;
use toml::Value as Toml;
fn main() {
let mut args = env::args();
let mut input = String::new();
if args.len() > 1 {
let name = args.nth(1).unwrap();
File::open(&name).and_then(|mut f| {
f.read_to_string(&mut input)
}).unwrap();
File::open(&name)
.and_then(|mut f| f.read_to_string(&mut input))
.unwrap();
} else {
io::stdin().read_to_string(&mut input).unwrap();
}
@ -37,15 +37,14 @@ fn convert(toml: Toml) -> Json {
Toml::String(s) => Json::String(s),
Toml::Integer(i) => Json::Number(i.into()),
Toml::Float(f) => {
let n = serde_json::Number::from_f64(f)
.expect("float infinite and nan not allowed");
let n = serde_json::Number::from_f64(f).expect("float infinite and nan not allowed");
Json::Number(n)
}
Toml::Boolean(b) => Json::Bool(b),
Toml::Array(arr) => Json::Array(arr.into_iter().map(convert).collect()),
Toml::Table(table) => Json::Object(table.into_iter().map(|(k, v)| {
(k, convert(v))
}).collect()),
Toml::Table(table) => {
Json::Object(table.into_iter().map(|(k, v)| (k, convert(v))).collect())
}
Toml::Datetime(dt) => Json::String(dt.to_string()),
}
}

View file

@ -1,6 +1,6 @@
use std::error;
use std::fmt;
use std::str::{self, FromStr};
use std::error;
use serde::{de, ser};
@ -109,9 +109,7 @@ impl fmt::Display for Offset {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Offset::Z => write!(f, "Z"),
Offset::Custom { hours, minutes } => {
write!(f, "{:+03}:{:02}", hours, minutes)
}
Offset::Custom { hours, minutes } => write!(f, "{:+03}:{:02}", hours, minutes),
}
}
}
@ -127,7 +125,7 @@ impl FromStr for Datetime {
// 0000-00-00
// 00:00:00.00
if date.len() < 3 {
return Err(DatetimeParseError { _private: () })
return Err(DatetimeParseError { _private: () });
}
let mut offset_allowed = true;
let mut chars = date.chars();
@ -165,19 +163,19 @@ impl FromStr for Datetime {
};
if date.month < 1 || date.month > 12 {
return Err(DatetimeParseError { _private: () })
return Err(DatetimeParseError { _private: () });
}
if date.day < 1 || date.day > 31 {
return Err(DatetimeParseError { _private: () })
return Err(DatetimeParseError { _private: () });
}
Some(date)
};
// Next parse the "partial-time" if available
let partial_time = if full_date.is_some() &&
(chars.clone().next() == Some('T')
|| chars.clone().next() == Some(' ')) {
let partial_time = if full_date.is_some()
&& (chars.clone().next() == Some('T') || chars.clone().next() == Some(' '))
{
chars.next();
true
} else {
@ -221,7 +219,7 @@ impl FromStr for Datetime {
}
}
if end == 0 {
return Err(DatetimeParseError { _private: () })
return Err(DatetimeParseError { _private: () });
}
chars = whole[end..].chars();
}
@ -234,16 +232,16 @@ impl FromStr for Datetime {
};
if time.hour > 24 {
return Err(DatetimeParseError { _private: () })
return Err(DatetimeParseError { _private: () });
}
if time.minute > 59 {
return Err(DatetimeParseError { _private: () })
return Err(DatetimeParseError { _private: () });
}
if time.second > 59 {
return Err(DatetimeParseError { _private: () })
return Err(DatetimeParseError { _private: () });
}
if time.nanosecond > 999_999_999 {
return Err(DatetimeParseError { _private: () })
return Err(DatetimeParseError { _private: () });
}
Some(time)
@ -288,7 +286,7 @@ impl FromStr for Datetime {
// Return an error if we didn't hit eof, otherwise return our parsed
// date
if chars.next().is_some() {
return Err(DatetimeParseError { _private: () })
return Err(DatetimeParseError { _private: () });
}
Ok(Datetime {
@ -308,7 +306,8 @@ fn digit(chars: &mut str::Chars) -> Result<u8, DatetimeParseError> {
impl ser::Serialize for Datetime {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: ser::Serializer
where
S: ser::Serializer,
{
use serde::ser::SerializeStruct;
@ -320,7 +319,8 @@ impl ser::Serialize for Datetime {
impl<'de> de::Deserialize<'de> for Datetime {
fn deserialize<D>(deserializer: D) -> Result<Datetime, D::Error>
where D: de::Deserializer<'de>
where
D: de::Deserializer<'de>,
{
struct DatetimeVisitor;
@ -332,15 +332,15 @@ impl<'de> de::Deserialize<'de> for Datetime {
}
fn visit_map<V>(self, mut visitor: V) -> Result<Datetime, V::Error>
where V: de::MapAccess<'de>
where
V: de::MapAccess<'de>,
{
let value = visitor.next_key::<DatetimeKey>()?;
if value.is_none() {
return Err(de::Error::custom("datetime key not found"))
return Err(de::Error::custom("datetime key not found"));
}
let v: DatetimeFromString = visitor.next_value()?;
Ok(v.value)
}
}
@ -353,7 +353,8 @@ struct DatetimeKey;
impl<'de> de::Deserialize<'de> for DatetimeKey {
fn deserialize<D>(deserializer: D) -> Result<DatetimeKey, D::Error>
where D: de::Deserializer<'de>
where
D: de::Deserializer<'de>,
{
struct FieldVisitor;
@ -365,7 +366,8 @@ impl<'de> de::Deserialize<'de> for DatetimeKey {
}
fn visit_str<E>(self, s: &str) -> Result<(), E>
where E: de::Error
where
E: de::Error,
{
if s == FIELD {
Ok(())
@ -386,7 +388,8 @@ pub struct DatetimeFromString {
impl<'de> de::Deserialize<'de> for DatetimeFromString {
fn deserialize<D>(deserializer: D) -> Result<DatetimeFromString, D::Error>
where D: de::Deserializer<'de>
where
D: de::Deserializer<'de>,
{
struct Visitor;
@ -398,7 +401,8 @@ impl<'de> de::Deserialize<'de> for DatetimeFromString {
}
fn visit_str<E>(self, s: &str) -> Result<DatetimeFromString, E>
where E: de::Error,
where
E: de::Error,
{
match s.parse() {
Ok(date) => Ok(DatetimeFromString { value: date }),

499
src/de.rs

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
pub use serde::de::{Deserialize, IntoDeserializer};
use value::{Value, Table, Array};
use value::{Array, Table, Value};
/// Construct a [`toml::Value`] from TOML syntax.
///
@ -424,7 +424,10 @@ pub fn push_toml(root: &mut Value, path: &[&str]) {
if !target.is_array() {
*target = Value::Array(Array::new());
}
target.as_array_mut().unwrap().push(Value::Table(Table::new()));
target
.as_array_mut()
.unwrap()
.push(Value::Table(Table::new()));
}
fn traverse<'a>(root: &'a mut Value, path: &[&str]) -> &'a mut Value {

View file

@ -32,8 +32,8 @@ use std::fmt::{self, Write};
use std::marker;
use std::rc::Rc;
use serde::ser;
use datetime;
use serde::ser;
/// Serialize the given data structure as a TOML byte vector.
///
@ -41,7 +41,8 @@ use datetime;
/// fail, if `T` contains a map with non-string keys, or if `T` attempts to
/// serialize an unsupported datatype such as an enum, tuple, or tuple struct.
pub fn to_vec<T: ?Sized>(value: &T) -> Result<Vec<u8>, Error>
where T: ser::Serialize,
where
T: ser::Serialize,
{
to_string(value).map(|e| e.into_bytes())
}
@ -87,7 +88,8 @@ pub fn to_vec<T: ?Sized>(value: &T) -> Result<Vec<u8>, Error>
/// }
/// ```
pub fn to_string<T: ?Sized>(value: &T) -> Result<String, Error>
where T: ser::Serialize,
where
T: ser::Serialize,
{
let mut dst = String::with_capacity(128);
value.serialize(&mut Serializer::new(&mut dst))?;
@ -99,7 +101,8 @@ pub fn to_string<T: ?Sized>(value: &T) -> Result<String, Error>
/// This is identical to `to_string` except the output string has a more
/// "pretty" output. See `Serializer::pretty` for more details.
pub fn to_string_pretty<T: ?Sized>(value: &T) -> Result<String, Error>
where T: ser::Serialize,
where
T: ser::Serialize,
{
let mut dst = String::with_capacity(128);
value.serialize(&mut Serializer::pretty(&mut dst))?;
@ -177,9 +180,7 @@ struct StringSettings {
impl StringSettings {
fn pretty() -> StringSettings {
StringSettings {
literal: true,
}
StringSettings { literal: true }
}
}
@ -239,7 +240,7 @@ pub enum SerializeTable<'a: 'b, 'b> {
key: String,
first: Cell<bool>,
table_emitted: Cell<bool>,
}
},
}
impl<'a> Serializer<'a> {
@ -333,8 +334,8 @@ impl<'a> Serializer<'a> {
/// """
/// ```
pub fn pretty_string_literal(&mut self, value: bool) -> &mut Self {
let use_default = if let &mut Some(ref mut s) = &mut Rc::get_mut(&mut self.settings)
.unwrap().string {
let use_default =
if let &mut Some(ref mut s) = &mut Rc::get_mut(&mut self.settings).unwrap().string {
s.literal = value;
false
} else {
@ -389,8 +390,8 @@ impl<'a> Serializer<'a> {
///
/// See `Serializer::pretty_array` for more details.
pub fn pretty_array_indent(&mut self, value: usize) -> &mut Self {
let use_default = if let &mut Some(ref mut a) = &mut Rc::get_mut(&mut self.settings)
.unwrap().array {
let use_default =
if let &mut Some(ref mut a) = &mut Rc::get_mut(&mut self.settings).unwrap().array {
a.indent = value;
false
} else {
@ -409,8 +410,8 @@ impl<'a> Serializer<'a> {
///
/// See `Serializer::pretty_array` for more details.
pub fn pretty_array_trailing_comma(&mut self, value: bool) -> &mut Self {
let use_default = if let &mut Some(ref mut a) = &mut Rc::get_mut(&mut self.settings)
.unwrap().array {
let use_default =
if let &mut Some(ref mut a) = &mut Rc::get_mut(&mut self.settings).unwrap().array {
a.trailing_comma = value;
false
} else {
@ -425,9 +426,7 @@ impl<'a> Serializer<'a> {
self
}
fn display<T: fmt::Display>(&mut self,
t: T,
type_: &'static str) -> Result<(), Error> {
fn display<T: fmt::Display>(&mut self, t: T, type_: &'static str) -> Result<(), Error> {
self.emit_key(type_)?;
drop(write!(self.dst, "{}", t));
if let State::Table { .. } = self.state {
@ -446,16 +445,26 @@ impl<'a> Serializer<'a> {
fn _emit_key(&mut self, state: &State) -> Result<(), Error> {
match *state {
State::End => Ok(()),
State::Array { parent, first, type_, len } => {
State::Array {
parent,
first,
type_,
len,
} => {
assert!(type_.get().is_some());
if first.get() {
self._emit_key(parent)?;
}
self.emit_array(first, len)
}
State::Table { parent, first, table_emitted, key } => {
State::Table {
parent,
first,
table_emitted,
key,
} => {
if table_emitted.get() {
return Err(Error::ValueAfterTable)
return Err(Error::ValueAfterTable);
}
if first.get() {
self.emit_table_header(parent)?;
@ -476,7 +485,7 @@ impl<'a> Serializer<'a> {
} else {
self.dst.push_str(", ")
}
},
}
(_, &Some(ref a)) => {
if first.get() {
self.dst.push_str("[\n")
@ -486,7 +495,7 @@ impl<'a> Serializer<'a> {
for _ in 0..a.indent {
self.dst.push_str(" ");
}
},
}
}
Ok(())
}
@ -498,7 +507,7 @@ impl<'a> Serializer<'a> {
};
if let Some(prev) = prev.get() {
if prev != type_ {
return Err(Error::ArrayMixedType)
return Err(Error::ArrayMixedType);
}
} else {
prev.set(Some(type_));
@ -507,14 +516,9 @@ impl<'a> Serializer<'a> {
}
fn escape_key(&mut self, key: &str) -> Result<(), Error> {
let ok = key.chars().all(|c| {
match c {
'a' ... 'z' |
'A' ... 'Z' |
'0' ... '9' |
'-' | '_' => true,
let ok = key.chars().all(|c| match c {
'a'...'z' | 'A'...'Z' | '0'...'9' | '-' | '_' => true,
_ => false,
}
});
if ok {
drop(write!(self.dst, "{}", key));
@ -570,7 +574,7 @@ impl<'a> Serializer<'a> {
found_singles = 0
}
match ch {
'\t' => {},
'\t' => {}
'\n' => ty = Type::NewlineTripple,
// note that the following are invalid: \b \f \r
c if c < '\u{1f}' => can_be_pretty = false, // Invalid control character
@ -606,8 +610,9 @@ impl<'a> Serializer<'a> {
let repr = if !is_key && self.settings.string.is_some() {
match (&self.settings.string, do_pretty(value)) {
(&Some(StringSettings { literal: false, .. }), Repr::Literal(_, ty)) =>
Repr::Std(ty),
(&Some(StringSettings { literal: false, .. }), Repr::Literal(_, ty)) => {
Repr::Std(ty)
}
(_, r @ _) => r,
}
} else {
@ -626,26 +631,23 @@ impl<'a> Serializer<'a> {
Type::OnelineSingle => self.dst.push('\''),
_ => self.dst.push_str("'''"),
}
},
}
Repr::Std(ty) => {
match ty {
Type::NewlineTripple => self.dst.push_str("\"\"\"\n"),
// note: OnelineTripple can happen if do_pretty wants to do
// '''it's one line'''
// but settings.string.literal == false
Type::OnelineSingle |
Type::OnelineTripple => self.dst.push('"'),
Type::OnelineSingle | Type::OnelineTripple => self.dst.push('"'),
}
for ch in value.chars() {
match ch {
'\u{8}' => self.dst.push_str("\\b"),
'\u{9}' => self.dst.push_str("\\t"),
'\u{a}' => {
match ty {
'\u{a}' => match ty {
Type::NewlineTripple => self.dst.push('\n'),
Type::OnelineSingle => self.dst.push_str("\\n"),
_ => unreachable!(),
}
},
'\u{c}' => self.dst.push_str("\\f"),
'\u{d}' => self.dst.push_str("\\r"),
@ -659,7 +661,7 @@ impl<'a> Serializer<'a> {
Type::NewlineTripple => self.dst.push_str("\"\"\""),
Type::OnelineSingle | Type::OnelineTripple => self.dst.push('"'),
}
},
}
}
Ok(())
}
@ -684,7 +686,11 @@ impl<'a> Serializer<'a> {
if !first.get() {
break;
}
if let State::Array { parent: &State::Table {..}, ..} = *parent {
if let State::Array {
parent: &State::Table { .. },
..
} = *parent
{
self.emit_table_header(parent)?;
break;
}
@ -697,7 +703,7 @@ impl<'a> Serializer<'a> {
// table in the document.
self.dst.push('\n');
}
},
}
State::Array { parent, first, .. } => {
if !first.get() {
// Always newline if we are not the first item in the
@ -709,7 +715,7 @@ impl<'a> Serializer<'a> {
self.dst.push('\n');
}
}
},
}
_ => {}
}
self.dst.push_str("[");
@ -728,7 +734,12 @@ impl<'a> Serializer<'a> {
match *key {
State::Array { parent, .. } => self.emit_key_part(parent),
State::End => Ok(true),
State::Table { key, parent, table_emitted, .. } => {
State::Table {
key,
parent,
table_emitted,
..
} => {
table_emitted.set(true);
let first = self.emit_key_part(parent)?;
if !first {
@ -841,7 +852,8 @@ impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> {
}
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<(), Self::Error>
where T: ser::Serialize
where
T: ser::Serialize,
{
value.serialize(self)
}
@ -850,40 +862,44 @@ impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> {
Err(Error::UnsupportedType)
}
fn serialize_unit_struct(self,
_name: &'static str)
-> Result<(), Self::Error> {
fn serialize_unit_struct(self, _name: &'static str) -> Result<(), Self::Error> {
Err(Error::UnsupportedType)
}
fn serialize_unit_variant(self,
fn serialize_unit_variant(
self,
_name: &'static str,
_variant_index: u32,
variant: &'static str)
-> Result<(), Self::Error> {
variant: &'static str,
) -> Result<(), Self::Error> {
self.serialize_str(variant)
}
fn serialize_newtype_struct<T: ?Sized>(self, _name: &'static str, value: &T)
-> Result<(), Self::Error>
where T: ser::Serialize,
fn serialize_newtype_struct<T: ?Sized>(
self,
_name: &'static str,
value: &T,
) -> Result<(), Self::Error>
where
T: ser::Serialize,
{
value.serialize(self)
}
fn serialize_newtype_variant<T: ?Sized>(self,
fn serialize_newtype_variant<T: ?Sized>(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_value: &T)
-> Result<(), Self::Error>
where T: ser::Serialize,
_value: &T,
) -> Result<(), Self::Error>
where
T: ser::Serialize,
{
Err(Error::UnsupportedType)
}
fn serialize_seq(self, len: Option<usize>)
-> Result<Self::SerializeSeq, Self::Error> {
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
self.array_type("array")?;
Ok(SerializeSeq {
ser: self,
@ -893,27 +909,29 @@ impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> {
})
}
fn serialize_tuple(self, len: usize)
-> Result<Self::SerializeTuple, Self::Error> {
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
self.serialize_seq(Some(len))
}
fn serialize_tuple_struct(self, _name: &'static str, len: usize)
-> Result<Self::SerializeTupleStruct, Self::Error> {
fn serialize_tuple_struct(
self,
_name: &'static str,
len: usize,
) -> Result<Self::SerializeTupleStruct, Self::Error> {
self.serialize_seq(Some(len))
}
fn serialize_tuple_variant(self,
fn serialize_tuple_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
len: usize)
-> Result<Self::SerializeTupleVariant, Self::Error> {
len: usize,
) -> Result<Self::SerializeTupleVariant, Self::Error> {
self.serialize_seq(Some(len))
}
fn serialize_map(self, _len: Option<usize>)
-> Result<Self::SerializeMap, Self::Error> {
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
self.array_type("table")?;
Ok(SerializeTable::Table {
ser: self,
@ -923,8 +941,11 @@ impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> {
})
}
fn serialize_struct(self, name: &'static str, _len: usize)
-> Result<Self::SerializeStruct, Self::Error> {
fn serialize_struct(
self,
name: &'static str,
_len: usize,
) -> Result<Self::SerializeStruct, Self::Error> {
if name == datetime::NAME {
self.array_type("datetime")?;
Ok(SerializeTable::Datetime(self))
@ -939,12 +960,13 @@ impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> {
}
}
fn serialize_struct_variant(self,
fn serialize_struct_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize)
-> Result<Self::SerializeStructVariant, Self::Error> {
_len: usize,
) -> Result<Self::SerializeStructVariant, Self::Error> {
Err(Error::UnsupportedType)
}
}
@ -954,7 +976,8 @@ impl<'a, 'b> ser::SerializeSeq for SerializeSeq<'a, 'b> {
type Error = Error;
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
where T: ser::Serialize,
where
T: ser::Serialize,
{
value.serialize(&mut Serializer {
dst: &mut *self.ser.dst,
@ -973,19 +996,17 @@ impl<'a, 'b> ser::SerializeSeq for SerializeSeq<'a, 'b> {
fn end(self) -> Result<(), Error> {
match self.type_.get() {
Some("table") => return Ok(()),
Some(_) => {
match (self.len, &self.ser.settings.array) {
Some(_) => match (self.len, &self.ser.settings.array) {
(Some(0...1), _) | (_, &None) => {
self.ser.dst.push_str("]");
},
}
(_, &Some(ref a)) => {
if a.trailing_comma {
self.ser.dst.push_str(",");
}
self.ser.dst.push_str("\n]");
}
},
}
}
None => {
assert!(self.first.get());
self.ser.emit_key("array")?;
@ -1004,7 +1025,8 @@ impl<'a, 'b> ser::SerializeTuple for SerializeSeq<'a, 'b> {
type Error = Error;
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
where T: ser::Serialize,
where
T: ser::Serialize,
{
ser::SerializeSeq::serialize_element(self, value)
}
@ -1019,7 +1041,8 @@ impl<'a, 'b> ser::SerializeTupleVariant for SerializeSeq<'a, 'b> {
type Error = Error;
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
where T: ser::Serialize,
where
T: ser::Serialize,
{
ser::SerializeSeq::serialize_element(self, value)
}
@ -1034,7 +1057,8 @@ impl<'a, 'b> ser::SerializeTupleStruct for SerializeSeq<'a, 'b> {
type Error = Error;
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
where T: ser::Serialize,
where
T: ser::Serialize,
{
ser::SerializeSeq::serialize_element(self, value)
}
@ -1049,7 +1073,8 @@ impl<'a, 'b> ser::SerializeMap for SerializeTable<'a, 'b> {
type Error = Error;
fn serialize_key<T: ?Sized>(&mut self, input: &T) -> Result<(), Error>
where T: ser::Serialize,
where
T: ser::Serialize,
{
match *self {
SerializeTable::Datetime(_) => panic!(), // shouldn't be possible
@ -1062,7 +1087,8 @@ impl<'a, 'b> ser::SerializeMap for SerializeTable<'a, 'b> {
}
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
where T: ser::Serialize,
where
T: ser::Serialize,
{
match *self {
SerializeTable::Datetime(_) => panic!(), // shouldn't be possible
@ -1085,7 +1111,7 @@ impl<'a, 'b> ser::SerializeMap for SerializeTable<'a, 'b> {
});
match res {
Ok(()) => first.set(false),
Err(Error::UnsupportedNone) => {},
Err(Error::UnsupportedNone) => {}
Err(e) => return Err(e),
}
}
@ -1111,16 +1137,16 @@ impl<'a, 'b> ser::SerializeStruct for SerializeTable<'a, 'b> {
type Ok = ();
type Error = Error;
fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T)
-> Result<(), Error>
where T: ser::Serialize,
fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), Error>
where
T: ser::Serialize,
{
match *self {
SerializeTable::Datetime(ref mut ser) => {
if key == datetime::FIELD {
value.serialize(DateStrEmitter(&mut *ser))?;
} else {
return Err(Error::DateInvalid)
return Err(Error::DateInvalid);
}
}
SerializeTable::Table {
@ -1141,7 +1167,7 @@ impl<'a, 'b> ser::SerializeStruct for SerializeTable<'a, 'b> {
});
match res {
Ok(()) => first.set(false),
Err(Error::UnsupportedNone) => {},
Err(Error::UnsupportedNone) => {}
Err(e) => return Err(e),
}
}
@ -1151,7 +1177,7 @@ impl<'a, 'b> ser::SerializeStruct for SerializeTable<'a, 'b> {
fn end(self) -> Result<(), Error> {
match self {
SerializeTable::Datetime(_) => {},
SerializeTable::Datetime(_) => {}
SerializeTable::Table { ser, first, .. } => {
if first.get() {
let state = ser.state.clone();
@ -1238,7 +1264,8 @@ impl<'a, 'b> ser::Serializer for DateStrEmitter<'a, 'b> {
}
fn serialize_some<T: ?Sized>(self, _value: &T) -> Result<(), Self::Error>
where T: ser::Serialize
where
T: ser::Serialize,
{
Err(Error::KeyNotString)
}
@ -1247,78 +1274,88 @@ impl<'a, 'b> ser::Serializer for DateStrEmitter<'a, 'b> {
Err(Error::KeyNotString)
}
fn serialize_unit_struct(self,
_name: &'static str)
-> Result<(), Self::Error> {
fn serialize_unit_struct(self, _name: &'static str) -> Result<(), Self::Error> {
Err(Error::DateInvalid)
}
fn serialize_unit_variant(self,
fn serialize_unit_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str)
-> Result<(), Self::Error> {
_variant: &'static str,
) -> Result<(), Self::Error> {
Err(Error::DateInvalid)
}
fn serialize_newtype_struct<T: ?Sized>(self, _name: &'static str, _value: &T)
-> Result<(), Self::Error>
where T: ser::Serialize,
fn serialize_newtype_struct<T: ?Sized>(
self,
_name: &'static str,
_value: &T,
) -> Result<(), Self::Error>
where
T: ser::Serialize,
{
Err(Error::DateInvalid)
}
fn serialize_newtype_variant<T: ?Sized>(self,
fn serialize_newtype_variant<T: ?Sized>(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_value: &T)
-> Result<(), Self::Error>
where T: ser::Serialize,
_value: &T,
) -> Result<(), Self::Error>
where
T: ser::Serialize,
{
Err(Error::DateInvalid)
}
fn serialize_seq(self, _len: Option<usize>)
-> Result<Self::SerializeSeq, Self::Error> {
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
Err(Error::DateInvalid)
}
fn serialize_tuple(self, _len: usize)
-> Result<Self::SerializeTuple, Self::Error> {
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
Err(Error::DateInvalid)
}
fn serialize_tuple_struct(self, _name: &'static str, _len: usize)
-> Result<Self::SerializeTupleStruct, Self::Error> {
fn serialize_tuple_struct(
self,
_name: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleStruct, Self::Error> {
Err(Error::DateInvalid)
}
fn serialize_tuple_variant(self,
fn serialize_tuple_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize)
-> Result<Self::SerializeTupleVariant, Self::Error> {
_len: usize,
) -> Result<Self::SerializeTupleVariant, Self::Error> {
Err(Error::DateInvalid)
}
fn serialize_map(self, _len: Option<usize>)
-> Result<Self::SerializeMap, Self::Error> {
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
Err(Error::DateInvalid)
}
fn serialize_struct(self, _name: &'static str, _len: usize)
-> Result<Self::SerializeStruct, Self::Error> {
fn serialize_struct(
self,
_name: &'static str,
_len: usize,
) -> Result<Self::SerializeStruct, Self::Error> {
Err(Error::DateInvalid)
}
fn serialize_struct_variant(self,
fn serialize_struct_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize)
-> Result<Self::SerializeStructVariant, Self::Error> {
_len: usize,
) -> Result<Self::SerializeStructVariant, Self::Error> {
Err(Error::DateInvalid)
}
}
@ -1397,7 +1434,8 @@ impl ser::Serializer for StringExtractor {
}
fn serialize_some<T: ?Sized>(self, _value: &T) -> Result<String, Self::Error>
where T: ser::Serialize
where
T: ser::Serialize,
{
Err(Error::KeyNotString)
}
@ -1406,78 +1444,88 @@ impl ser::Serializer for StringExtractor {
Err(Error::KeyNotString)
}
fn serialize_unit_struct(self,
_name: &'static str)
-> Result<String, Self::Error> {
fn serialize_unit_struct(self, _name: &'static str) -> Result<String, Self::Error> {
Err(Error::KeyNotString)
}
fn serialize_unit_variant(self,
fn serialize_unit_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str)
-> Result<String, Self::Error> {
_variant: &'static str,
) -> Result<String, Self::Error> {
Err(Error::KeyNotString)
}
fn serialize_newtype_struct<T: ?Sized>(self, _name: &'static str, value: &T)
-> Result<String, Self::Error>
where T: ser::Serialize,
fn serialize_newtype_struct<T: ?Sized>(
self,
_name: &'static str,
value: &T,
) -> Result<String, Self::Error>
where
T: ser::Serialize,
{
value.serialize(self)
}
fn serialize_newtype_variant<T: ?Sized>(self,
fn serialize_newtype_variant<T: ?Sized>(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_value: &T)
-> Result<String, Self::Error>
where T: ser::Serialize,
_value: &T,
) -> Result<String, Self::Error>
where
T: ser::Serialize,
{
Err(Error::KeyNotString)
}
fn serialize_seq(self, _len: Option<usize>)
-> Result<Self::SerializeSeq, Self::Error> {
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
Err(Error::KeyNotString)
}
fn serialize_tuple(self, _len: usize)
-> Result<Self::SerializeTuple, Self::Error> {
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
Err(Error::KeyNotString)
}
fn serialize_tuple_struct(self, _name: &'static str, _len: usize)
-> Result<Self::SerializeTupleStruct, Self::Error> {
fn serialize_tuple_struct(
self,
_name: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleStruct, Self::Error> {
Err(Error::KeyNotString)
}
fn serialize_tuple_variant(self,
fn serialize_tuple_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize)
-> Result<Self::SerializeTupleVariant, Self::Error> {
_len: usize,
) -> Result<Self::SerializeTupleVariant, Self::Error> {
Err(Error::KeyNotString)
}
fn serialize_map(self, _len: Option<usize>)
-> Result<Self::SerializeMap, Self::Error> {
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
Err(Error::KeyNotString)
}
fn serialize_struct(self, _name: &'static str, _len: usize)
-> Result<Self::SerializeStruct, Self::Error> {
fn serialize_struct(
self,
_name: &'static str,
_len: usize,
) -> Result<Self::SerializeStruct, Self::Error> {
Err(Error::KeyNotString)
}
fn serialize_struct_variant(self,
fn serialize_struct_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize)
-> Result<Self::SerializeStructVariant, Self::Error> {
_len: usize,
) -> Result<Self::SerializeStructVariant, Self::Error> {
Err(Error::KeyNotString)
}
}
@ -1549,12 +1597,12 @@ enum Category {
/// # type Dependency = String;
/// # fn main() {}
/// ```
pub fn tables_last<'a, I, K, V, S>(data: &'a I, serializer: S)
-> Result<S::Ok, S::Error>
where &'a I: IntoIterator<Item = (K, V)>,
pub fn tables_last<'a, I, K, V, S>(data: &'a I, serializer: S) -> Result<S::Ok, S::Error>
where
&'a I: IntoIterator<Item = (K, V)>,
K: ser::Serialize,
V: ser::Serialize,
S: ser::Serializer
S: ser::Serializer,
{
use serde::ser::SerializeMap;
@ -1668,15 +1716,30 @@ impl<E: ser::Error> ser::Serializer for Categorize<E> {
Err(ser::Error::custom("unsupported"))
}
fn serialize_unit_variant(self, _: &'static str, _: u32, _: &'static str) -> Result<Self::Ok, Self::Error> {
fn serialize_unit_variant(
self,
_: &'static str,
_: u32,
_: &'static str,
) -> Result<Self::Ok, Self::Error> {
Err(ser::Error::custom("unsupported"))
}
fn serialize_newtype_struct<T: ?Sized + ser::Serialize>(self, _: &'static str, v: &T) -> Result<Self::Ok, Self::Error> {
fn serialize_newtype_struct<T: ?Sized + ser::Serialize>(
self,
_: &'static str,
v: &T,
) -> Result<Self::Ok, Self::Error> {
v.serialize(self)
}
fn serialize_newtype_variant<T: ?Sized + ser::Serialize>(self, _: &'static str, _: u32, _: &'static str, _: &T) -> Result<Self::Ok, Self::Error> {
fn serialize_newtype_variant<T: ?Sized + ser::Serialize>(
self,
_: &'static str,
_: u32,
_: &'static str,
_: &T,
) -> Result<Self::Ok, Self::Error> {
Err(ser::Error::custom("unsupported"))
}
@ -1688,11 +1751,21 @@ impl<E: ser::Error> ser::Serializer for Categorize<E> {
Ok(self)
}
fn serialize_tuple_struct(self, _: &'static str, _: usize) -> Result<Self::SerializeTupleStruct, Self::Error> {
fn serialize_tuple_struct(
self,
_: &'static str,
_: usize,
) -> Result<Self::SerializeTupleStruct, Self::Error> {
Ok(self)
}
fn serialize_tuple_variant(self, _: &'static str, _: u32, _: &'static str, _: usize) -> Result<Self::SerializeTupleVariant, Self::Error> {
fn serialize_tuple_variant(
self,
_: &'static str,
_: u32,
_: &'static str,
_: usize,
) -> Result<Self::SerializeTupleVariant, Self::Error> {
Ok(self)
}
@ -1704,7 +1777,13 @@ impl<E: ser::Error> ser::Serializer for Categorize<E> {
Ok(self)
}
fn serialize_struct_variant(self, _: &'static str, _: u32, _: &'static str, _: usize) -> Result<Self::SerializeStructVariant, Self::Error> {
fn serialize_struct_variant(
self,
_: &'static str,
_: u32,
_: &'static str,
_: usize,
) -> Result<Self::SerializeStructVariant, Self::Error> {
Err(ser::Error::custom("unsupported"))
}
}
@ -1713,8 +1792,7 @@ impl<E: ser::Error> ser::SerializeSeq for Categorize<E> {
type Ok = Category;
type Error = E;
fn serialize_element<T: ?Sized + ser::Serialize>(&mut self, _: &T)
-> Result<(), Self::Error> {
fn serialize_element<T: ?Sized + ser::Serialize>(&mut self, _: &T) -> Result<(), Self::Error> {
Ok(())
}
@ -1727,8 +1805,7 @@ impl<E: ser::Error> ser::SerializeTuple for Categorize<E> {
type Ok = Category;
type Error = E;
fn serialize_element<T: ?Sized + ser::Serialize>(&mut self, _: &T)
-> Result<(), Self::Error> {
fn serialize_element<T: ?Sized + ser::Serialize>(&mut self, _: &T) -> Result<(), Self::Error> {
Ok(())
}
@ -1741,8 +1818,7 @@ impl<E: ser::Error> ser::SerializeTupleVariant for Categorize<E> {
type Ok = Category;
type Error = E;
fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, _: &T)
-> Result<(), Self::Error> {
fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, _: &T) -> Result<(), Self::Error> {
Ok(())
}
@ -1755,8 +1831,7 @@ impl<E: ser::Error> ser::SerializeTupleStruct for Categorize<E> {
type Ok = Category;
type Error = E;
fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, _: &T)
-> Result<(), Self::Error> {
fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, _: &T) -> Result<(), Self::Error> {
Ok(())
}
@ -1769,13 +1844,11 @@ impl<E: ser::Error> ser::SerializeMap for Categorize<E> {
type Ok = Category;
type Error = E;
fn serialize_key<T: ?Sized + ser::Serialize>(&mut self, _: &T)
-> Result<(), Self::Error> {
fn serialize_key<T: ?Sized + ser::Serialize>(&mut self, _: &T) -> Result<(), Self::Error> {
Ok(())
}
fn serialize_value<T: ?Sized + ser::Serialize>(&mut self, _: &T)
-> Result<(), Self::Error> {
fn serialize_value<T: ?Sized + ser::Serialize>(&mut self, _: &T) -> Result<(), Self::Error> {
Ok(())
}
@ -1788,10 +1861,9 @@ impl<E: ser::Error> ser::SerializeStruct for Categorize<E> {
type Ok = Category;
type Error = E;
fn serialize_field<T: ?Sized>(&mut self,
_: &'static str,
_: &T) -> Result<(), Self::Error>
where T: ser::Serialize,
fn serialize_field<T: ?Sized>(&mut self, _: &'static str, _: &T) -> Result<(), Self::Error>
where
T: ser::Serialize,
{
Ok(())
}

View file

@ -78,15 +78,18 @@ impl<T> Spanned<T> {
}
impl<'de, T> de::Deserialize<'de> for Spanned<T>
where T: de::Deserialize<'de>
where
T: de::Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Spanned<T>, D::Error>
where D: de::Deserializer<'de>
where
D: de::Deserializer<'de>,
{
struct SpannedVisitor<T>(::std::marker::PhantomData<T>);
impl<'de, T> de::Visitor<'de> for SpannedVisitor<T>
where T: de::Deserialize<'de>
where
T: de::Deserialize<'de>,
{
type Value = Spanned<T>;
@ -95,22 +98,23 @@ impl<'de, T> de::Deserialize<'de> for Spanned<T>
}
fn visit_map<V>(self, mut visitor: V) -> Result<Spanned<T>, V::Error>
where V: de::MapAccess<'de>
where
V: de::MapAccess<'de>,
{
if visitor.next_key()? != Some(START) {
return Err(de::Error::custom("spanned start key not found"))
return Err(de::Error::custom("spanned start key not found"));
}
let start: usize = visitor.next_value()?;
if visitor.next_key()? != Some(END) {
return Err(de::Error::custom("spanned end key not found"))
return Err(de::Error::custom("spanned end key not found"));
}
let end: usize = visitor.next_value()?;
if visitor.next_key()? != Some(VALUE) {
return Err(de::Error::custom("spanned value key not found"))
return Err(de::Error::custom("spanned value key not found"));
}
let value: T = visitor.next_value()?;
@ -118,7 +122,7 @@ impl<'de, T> de::Deserialize<'de> for Spanned<T>
Ok(Spanned {
start: start,
end: end,
value: value
value: value,
})
}
}

View file

@ -38,7 +38,11 @@ pub enum Token<'a> {
RightBracket,
Keylike(&'a str),
String { src: &'a str, val: Cow<'a, str>, multiline: bool },
String {
src: &'a str,
val: Cow<'a, str>,
multiline: bool,
},
}
#[derive(Eq, PartialEq, Debug)]
@ -53,7 +57,11 @@ pub enum Error {
NewlineInTableKey(usize),
MultilineStringKey(usize),
EmptyTableKey(usize),
Wanted { at: usize, expected: &'static str, found: &'static str },
Wanted {
at: usize,
expected: &'static str,
found: &'static str,
},
}
#[derive(Clone)]
@ -101,10 +109,16 @@ impl<'a> Tokenizer<'a> {
Some((start, '}')) => (start, RightBrace),
Some((start, '[')) => (start, LeftBracket),
Some((start, ']')) => (start, RightBracket),
Some((start, '\'')) => return self.literal_string(start)
.map(|t| Some((self.step_span(start), t))),
Some((start, '"')) => return self.basic_string(start)
.map(|t| Some((self.step_span(start), t))),
Some((start, '\'')) => {
return self
.literal_string(start)
.map(|t| Some((self.step_span(start), t)))
}
Some((start, '"')) => {
return self
.basic_string(start)
.map(|t| Some((self.step_span(start), t)))
}
Some((start, ch)) if is_keylike(ch) => (start, self.keylike(start)),
Some((start, ch)) => return Err(Error::Unexpected(start, ch)),
@ -156,13 +170,11 @@ impl<'a> Tokenizer<'a> {
})
}
}
None => {
Err(Error::Wanted {
None => Err(Error::Wanted {
at: self.input.len(),
expected: expected.describe(),
found: "eof",
})
}
}),
}
}
@ -170,33 +182,36 @@ impl<'a> Tokenizer<'a> {
let current = self.current();
match self.next()? {
Some((span, Token::Keylike(k))) => Ok((span, k.into())),
Some((span, Token::String { src, val, multiline })) => {
Some((
span,
Token::String {
src,
val,
multiline,
},
)) => {
let offset = self.substr_offset(src);
if multiline {
return Err(Error::MultilineStringKey(offset))
return Err(Error::MultilineStringKey(offset));
}
if val == "" {
return Err(Error::EmptyTableKey(offset))
return Err(Error::EmptyTableKey(offset));
}
match src.find('\n') {
None => Ok((span, val)),
Some(i) => Err(Error::NewlineInTableKey(offset + i)),
}
}
Some((_, other)) => {
Err(Error::Wanted {
Some((_, other)) => Err(Error::Wanted {
at: current,
expected: "a table key",
found: other.describe(),
})
}
None => {
Err(Error::Wanted {
}),
None => Err(Error::Wanted {
at: self.input.len(),
expected: "a table key",
found: "eof",
})
}
}),
}
}
@ -209,7 +224,7 @@ impl<'a> Tokenizer<'a> {
pub fn eat_comment(&mut self) -> Result<bool, Error> {
if !self.eatc('#') {
return Ok(false)
return Ok(false);
}
drop(self.comment_token(0));
self.eat_newline_or_eof().map(|()| true)
@ -218,23 +233,19 @@ impl<'a> Tokenizer<'a> {
pub fn eat_newline_or_eof(&mut self) -> Result<(), Error> {
let current = self.current();
match self.next()? {
None |
Some((_, Token::Newline)) => Ok(()),
Some((_, other)) => {
Err(Error::Wanted {
None | Some((_, Token::Newline)) => Ok(()),
Some((_, other)) => Err(Error::Wanted {
at: current,
expected: "newline",
found: other.describe(),
})
}
}),
}
}
pub fn skip_to_newline(&mut self) {
loop {
match self.one() {
Some((_, '\n')) |
None => break,
Some((_, '\n')) | None => break,
_ => {}
}
}
@ -251,7 +262,11 @@ impl<'a> Tokenizer<'a> {
}
pub fn current(&mut self) -> usize {
self.chars.clone().next().map(|i| i.0).unwrap_or(self.input.len())
self.chars
.clone()
.next()
.map(|i| i.0)
.unwrap_or(self.input.len())
}
pub fn input(&self) -> &'a str {
@ -268,20 +283,25 @@ impl<'a> Tokenizer<'a> {
fn comment_token(&mut self, start: usize) -> Token<'a> {
while let Some((_, ch)) = self.chars.clone().next() {
if ch != '\t' && (ch < '\u{20}' || ch > '\u{10ffff}') {
break
break;
}
self.one();
}
Comment(&self.input[start..self.current()])
}
fn read_string(&mut self,
fn read_string(
&mut self,
delim: char,
start: usize,
new_ch: &mut FnMut(&mut Tokenizer, &mut MaybeString,
bool, usize, char)
-> Result<(), Error>)
-> Result<Token<'a>, Error> {
new_ch: &mut FnMut(
&mut Tokenizer,
&mut MaybeString,
bool,
usize,
char,
) -> Result<(), Error>,
) -> Result<Token<'a>, Error> {
let mut multiline = false;
if self.eatc(delim) {
if self.eatc(delim) {
@ -291,7 +311,7 @@ impl<'a> Tokenizer<'a> {
src: &self.input[start..start + 2],
val: Cow::Borrowed(""),
multiline: false,
})
});
}
}
let mut val = MaybeString::NotEscaped(self.current());
@ -309,9 +329,9 @@ impl<'a> Tokenizer<'a> {
} else {
val.push('\n');
}
continue
continue;
} else {
return Err(Error::NewlineInString(i))
return Err(Error::NewlineInString(i));
}
}
Some((i, ch)) if ch == delim => {
@ -319,7 +339,7 @@ impl<'a> Tokenizer<'a> {
for _ in 0..2 {
if !self.eatc(delim) {
val.push(delim);
continue 'outer
continue 'outer;
}
}
}
@ -327,10 +347,10 @@ impl<'a> Tokenizer<'a> {
src: &self.input[start..self.current()],
val: val.into_cow(&self.input[..i]),
multiline: multiline,
})
});
}
Some((i, c)) => new_ch(self, &mut val, multiline, i, c)?,
None => return Err(Error::UnterminatedString(start))
None => return Err(Error::UnterminatedString(start)),
}
}
}
@ -347,8 +367,7 @@ impl<'a> Tokenizer<'a> {
}
fn basic_string(&mut self, start: usize) -> Result<Token<'a>, Error> {
self.read_string('"', start, &mut |me, val, multi, i, ch| {
match ch {
self.read_string('"', start, &mut |me, val, multi, i, ch| match ch {
'\\' => {
val.to_owned(&me.input[..i]);
match me.chars.next() {
@ -359,25 +378,22 @@ impl<'a> Tokenizer<'a> {
Some((_, 'n')) => val.push('\n'),
Some((_, 'r')) => val.push('\r'),
Some((_, 't')) => val.push('\t'),
Some((i, c @ 'u')) |
Some((i, c @ 'U')) => {
Some((i, c @ 'u')) | Some((i, c @ 'U')) => {
let len = if c == 'u' { 4 } else { 8 };
val.push(me.hex(start, i, len)?);
}
Some((i, c @ ' ')) |
Some((i, c @ '\t')) |
Some((i, c @ '\n')) if multi => {
Some((i, c @ ' ')) | Some((i, c @ '\t')) | Some((i, c @ '\n')) if multi => {
if c != '\n' {
while let Some((_, ch)) = me.chars.clone().next() {
match ch {
' ' | '\t' => {
me.chars.next();
continue
},
continue;
}
'\n' => {
me.chars.next();
break
},
break;
}
_ => return Err(Error::InvalidEscape(i, c)),
}
}
@ -400,8 +416,7 @@ impl<'a> Tokenizer<'a> {
val.push(ch);
Ok(())
}
_ => Err(Error::InvalidCharInString(i, ch))
}
_ => Err(Error::InvalidCharInString(i, ch)),
})
}
@ -424,7 +439,7 @@ impl<'a> Tokenizer<'a> {
fn keylike(&mut self, start: usize) -> Token<'a> {
while let Some((_, ch)) = self.peek_one() {
if !is_keylike(ch) {
break
break;
}
self.one();
}
@ -441,8 +456,14 @@ impl<'a> Tokenizer<'a> {
/// Calculate the span of a single character.
fn step_span(&mut self, start: usize) -> Span {
let end = self.peek_one().map(|t| t.0).unwrap_or_else(|| self.input.len());
Span { start: start, end: end }
let end = self
.peek_one()
.map(|t| t.0)
.unwrap_or_else(|| self.input.len());
Span {
start: start,
end: end,
}
}
/// Peek one char without consuming it.
@ -465,7 +486,7 @@ impl<'a> Iterator for CrlfFold<'a> {
let mut attempt = self.chars.clone();
if let Some((_, '\n')) = attempt.next() {
self.chars = attempt;
return (i, '\n')
return (i, '\n');
}
}
(i, c)
@ -499,11 +520,11 @@ impl MaybeString {
}
fn is_keylike(ch: char) -> bool {
('A' <= ch && ch <= 'Z') ||
('a' <= ch && ch <= 'z') ||
('0' <= ch && ch <= '9') ||
ch == '-' ||
ch == '_'
('A' <= ch && ch <= 'Z')
|| ('a' <= ch && ch <= 'z')
|| ('0' <= ch && ch <= '9')
|| ch == '-'
|| ch == '_'
}
impl<'a> Token<'a> {
@ -520,7 +541,13 @@ impl<'a> Token<'a> {
Token::LeftBrace => "a left brace",
Token::RightBracket => "a right bracket",
Token::LeftBracket => "a left bracket",
Token::String { multiline, .. } => if multiline { "a multiline string" } else { "a string" },
Token::String { multiline, .. } => {
if multiline {
"a multiline string"
} else {
"a string"
}
}
Token::Colon => "a colon",
Token::Plus => "a plus",
}
@ -529,8 +556,8 @@ impl<'a> Token<'a> {
#[cfg(test)]
mod tests {
use super::{Error, Token, Tokenizer};
use std::borrow::Cow;
use super::{Tokenizer, Token, Error};
fn err(input: &str, err: Error) {
let mut t = Tokenizer::new(input);
@ -544,11 +571,14 @@ mod tests {
fn t(input: &str, val: &str, multiline: bool) {
let mut t = Tokenizer::new(input);
let (_, token) = t.next().unwrap().unwrap();
assert_eq!(token, Token::String {
assert_eq!(
token,
Token::String {
src: input,
val: Cow::Borrowed(val),
multiline: multiline,
});
}
);
assert!(t.next().unwrap().is_none());
}
@ -567,11 +597,14 @@ mod tests {
fn t(input: &str, val: &str, multiline: bool) {
let mut t = Tokenizer::new(input);
let (_, token) = t.next().unwrap().unwrap();
assert_eq!(token, Token::String {
assert_eq!(
token,
Token::String {
src: input,
val: Cow::Borrowed(val),
multiline: multiline,
});
}
);
assert!(t.next().unwrap().is_none());
}
@ -585,7 +618,11 @@ mod tests {
t(r#""\U000A0000""#, "\u{A0000}", false);
t(r#""\\t""#, "\\t", false);
t("\"\"\"\\\n\"\"\"", "", true);
t("\"\"\"\\\n \t \t \\\r\n \t \n \t \r\n\"\"\"", "", true);
t(
"\"\"\"\\\n \t \t \\\r\n \t \n \t \r\n\"\"\"",
"",
true,
);
t(r#""\r""#, "\r", false);
t(r#""\n""#, "\n", false);
t(r#""\b""#, "\u{8}", false);
@ -636,13 +673,18 @@ mod tests {
assert_eq!(actual.len(), expected.len());
}
t(" a ", &[
t(
" a ",
&[
((0, 1), Token::Whitespace(" "), " "),
((1, 2), Token::Keylike("a"), "a"),
((2, 3), Token::Whitespace(" "), " "),
]);
],
);
t(" a\t [[]] \t [] {} , . =\n# foo \r\n#foo \n ", &[
t(
" a\t [[]] \t [] {} , . =\n# foo \r\n#foo \n ",
&[
((0, 1), Token::Whitespace(" "), " "),
((1, 2), Token::Keylike("a"), "a"),
((2, 4), Token::Whitespace("\t "), "\t "),
@ -668,7 +710,8 @@ mod tests {
((31, 36), Token::Comment("#foo "), "#foo "),
((36, 37), Token::Newline, "\n"),
((37, 38), Token::Whitespace(" "), " "),
]);
],
);
}
#[test]

View file

@ -1,18 +1,18 @@
//! Definition of a TOML value
use std::collections::{BTreeMap, HashMap};
use std::hash::Hash;
use std::fmt;
use std::hash::Hash;
use std::ops;
use std::str::FromStr;
use std::vec;
use serde::ser;
use serde::de;
use serde::de::IntoDeserializer;
use serde::ser;
pub use datetime::{Datetime, DatetimeParseError};
use datetime::{self, DatetimeFromString};
pub use datetime::{Datetime, DatetimeParseError};
/// Representation of a TOML value.
#[derive(PartialEq, Clone, Debug)]
@ -46,7 +46,8 @@ impl Value {
/// This conversion can fail if `T`'s implementation of `Serialize` decides to
/// fail, or if `T` contains a map with non-string keys.
pub fn try_from<T>(value: T) -> Result<Value, ::ser::Error>
where T: ser::Serialize,
where
T: ser::Serialize,
{
value.serialize(Serializer)
}
@ -61,7 +62,8 @@ impl Value {
/// missing from the TOML map or some number is too big to fit in the expected
/// primitive type.
pub fn try_into<'de, T>(self) -> Result<T, ::de::Error>
where T: de::Deserialize<'de>,
where
T: de::Deserialize<'de>,
{
de::Deserialize::deserialize(self)
}
@ -92,7 +94,10 @@ impl Value {
/// Extracts the integer value if it is an integer.
pub fn as_integer(&self) -> Option<i64> {
match *self { Value::Integer(i) => Some(i), _ => None }
match *self {
Value::Integer(i) => Some(i),
_ => None,
}
}
/// Tests whether this value is an integer.
@ -102,7 +107,10 @@ impl Value {
/// Extracts the float value if it is a float.
pub fn as_float(&self) -> Option<f64> {
match *self { Value::Float(f) => Some(f), _ => None }
match *self {
Value::Float(f) => Some(f),
_ => None,
}
}
/// Tests whether this value is a float.
@ -112,7 +120,10 @@ impl Value {
/// Extracts the boolean value if it is a boolean.
pub fn as_bool(&self) -> Option<bool> {
match *self { Value::Boolean(b) => Some(b), _ => None }
match *self {
Value::Boolean(b) => Some(b),
_ => None,
}
}
/// Tests whether this value is a boolean.
@ -122,7 +133,10 @@ impl Value {
/// Extracts the string of this value if it is a string.
pub fn as_str(&self) -> Option<&str> {
match *self { Value::String(ref s) => Some(&**s), _ => None }
match *self {
Value::String(ref s) => Some(&**s),
_ => None,
}
}
/// Tests if this value is a string.
@ -139,7 +153,10 @@ impl Value {
/// 1979-05-27T07:32:00Z
/// ```
pub fn as_datetime(&self) -> Option<&Datetime> {
match *self { Value::Datetime(ref s) => Some(s), _ => None }
match *self {
Value::Datetime(ref s) => Some(s),
_ => None,
}
}
/// Tests whether this value is a datetime.
@ -149,12 +166,18 @@ impl Value {
/// Extracts the array value if it is an array.
pub fn as_array(&self) -> Option<&Vec<Value>> {
match *self { Value::Array(ref s) => Some(s), _ => None }
match *self {
Value::Array(ref s) => Some(s),
_ => None,
}
}
/// Extracts the array value if it is an array.
pub fn as_array_mut(&mut self) -> Option<&mut Vec<Value>> {
match *self { Value::Array(ref mut s) => Some(s), _ => None }
match *self {
Value::Array(ref mut s) => Some(s),
_ => None,
}
}
/// Tests whether this value is an array.
@ -164,12 +187,18 @@ impl Value {
/// Extracts the table value if it is a table.
pub fn as_table(&self) -> Option<&Table> {
match *self { Value::Table(ref s) => Some(s), _ => None }
match *self {
Value::Table(ref s) => Some(s),
_ => None,
}
}
/// Extracts the table value if it is a table.
pub fn as_table_mut(&mut self) -> Option<&mut Table> {
match *self { Value::Table(ref mut s) => Some(s), _ => None }
match *self {
Value::Table(ref mut s) => Some(s),
_ => None,
}
}
/// Tests whether this value is a table.
@ -180,13 +209,13 @@ impl Value {
/// Tests whether this and another value have the same type.
pub fn same_type(&self, other: &Value) -> bool {
match (self, other) {
(&Value::String(..), &Value::String(..)) |
(&Value::Integer(..), &Value::Integer(..)) |
(&Value::Float(..), &Value::Float(..)) |
(&Value::Boolean(..), &Value::Boolean(..)) |
(&Value::Datetime(..), &Value::Datetime(..)) |
(&Value::Array(..), &Value::Array(..)) |
(&Value::Table(..), &Value::Table(..)) => true,
(&Value::String(..), &Value::String(..))
| (&Value::Integer(..), &Value::Integer(..))
| (&Value::Float(..), &Value::Float(..))
| (&Value::Boolean(..), &Value::Boolean(..))
| (&Value::Datetime(..), &Value::Datetime(..))
| (&Value::Array(..), &Value::Array(..))
| (&Value::Table(..), &Value::Table(..)) => true,
_ => false,
}
@ -206,7 +235,10 @@ impl Value {
}
}
impl<I> ops::Index<I> for Value where I: Index {
impl<I> ops::Index<I> for Value
where
I: Index,
{
type Output = Value;
fn index(&self, index: I) -> &Value {
@ -214,7 +246,10 @@ impl<I> ops::Index<I> for Value where I: Index {
}
}
impl<I> ops::IndexMut<I> for Value where I: Index {
impl<I> ops::IndexMut<I> for Value
where
I: Index,
{
fn index_mut(&mut self, index: I) -> &mut Value {
self.get_mut(index).expect("index not found")
}
@ -235,9 +270,7 @@ impl<V: Into<Value>> From<Vec<V>> for Value {
impl<S: Into<String>, V: Into<Value>> From<BTreeMap<S, V>> for Value {
fn from(val: BTreeMap<S, V>) -> Value {
let table = val.into_iter()
.map(|(s, v)| (s.into(), v.into()))
.collect();
let table = val.into_iter().map(|(s, v)| (s.into(), v.into())).collect();
Value::Table(table)
}
@ -245,9 +278,7 @@ impl<S: Into<String>, V: Into<Value>> From<BTreeMap<S, V>> for Value {
impl<S: Into<String> + Hash + Eq, V: Into<Value>> From<HashMap<S, V>> for Value {
fn from(val: HashMap<S, V>) -> Value {
let table = val.into_iter()
.map(|(s, v)| (s.into(), v.into()))
.collect();
let table = val.into_iter().map(|(s, v)| (s.into(), v.into())).collect();
Value::Table(table)
}
@ -261,7 +292,7 @@ macro_rules! impl_into_value {
Value::$variant(val.into())
}
}
}
};
}
impl_into_value!(String: String);
@ -340,7 +371,10 @@ impl Index for String {
}
}
impl<'s, T: ?Sized> Index for &'s T where T: Index {
impl<'s, T: ?Sized> Index for &'s T
where
T: Index,
{
fn index<'a>(&self, val: &'a Value) -> Option<&'a Value> {
(**self).index(val)
}
@ -352,7 +386,9 @@ impl<'s, T: ?Sized> Index for &'s T where T: Index {
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
::ser::to_string(self).expect("Unable to represent value as string").fmt(f)
::ser::to_string(self)
.expect("Unable to represent value as string")
.fmt(f)
}
}
@ -365,7 +401,8 @@ impl FromStr for Value {
impl ser::Serialize for Value {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: ser::Serializer
where
S: ser::Serializer,
{
use serde::ser::SerializeMap;
@ -381,13 +418,20 @@ impl ser::Serialize for Value {
// Be sure to visit non-tables first (and also non
// array-of-tables) as all keys must be emitted first.
for (k, v) in t {
if !v.is_table() && !v.is_array() ||
(v.as_array().map(|a| !a.iter().any(|v| v.is_table())).unwrap_or(false)) {
if !v.is_table() && !v.is_array()
|| (v
.as_array()
.map(|a| !a.iter().any(|v| v.is_table()))
.unwrap_or(false))
{
map.serialize_entry(k, v)?;
}
}
for (k, v) in t {
if v.as_array().map(|a| a.iter().any(|v| v.is_table())).unwrap_or(false) {
if v.as_array()
.map(|a| a.iter().any(|v| v.is_table()))
.unwrap_or(false)
{
map.serialize_entry(k, v)?;
}
}
@ -404,7 +448,8 @@ impl ser::Serialize for Value {
impl<'de> de::Deserialize<'de> for Value {
fn deserialize<D>(deserializer: D) -> Result<Value, D::Error>
where D: de::Deserializer<'de>,
where
D: de::Deserializer<'de>,
{
struct ValueVisitor;
@ -452,13 +497,15 @@ impl<'de> de::Deserialize<'de> for Value {
}
fn visit_some<D>(self, deserializer: D) -> Result<Value, D::Error>
where D: de::Deserializer<'de>,
where
D: de::Deserializer<'de>,
{
de::Deserialize::deserialize(deserializer)
}
fn visit_seq<V>(self, mut visitor: V) -> Result<Value, V::Error>
where V: de::SeqAccess<'de>,
where
V: de::SeqAccess<'de>,
{
let mut vec = Vec::new();
while let Some(elem) = visitor.next_element()? {
@ -468,16 +515,15 @@ impl<'de> de::Deserialize<'de> for Value {
}
fn visit_map<V>(self, mut visitor: V) -> Result<Value, V::Error>
where V: de::MapAccess<'de>,
where
V: de::MapAccess<'de>,
{
let mut key = String::new();
let datetime = visitor.next_key_seed(DatetimeOrTable {
key: &mut key,
})?;
let datetime = visitor.next_key_seed(DatetimeOrTable { key: &mut key })?;
match datetime {
Some(true) => {
let date: DatetimeFromString = visitor.next_value()?;
return Ok(Value::Datetime(date.value))
return Ok(Value::Datetime(date.value));
}
None => return Ok(Value::Table(BTreeMap::new())),
Some(false) => {}
@ -487,7 +533,7 @@ impl<'de> de::Deserialize<'de> for Value {
while let Some(key) = visitor.next_key()? {
if map.contains_key(&key) {
let msg = format!("duplicate key: `{}`", key);
return Err(de::Error::custom(msg))
return Err(de::Error::custom(msg));
}
map.insert(key, visitor.next_value()?);
}
@ -503,7 +549,8 @@ impl<'de> de::Deserializer<'de> for Value {
type Error = ::de::Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, ::de::Error>
where V: de::Visitor<'de>,
where
V: de::Visitor<'de>,
{
match self {
Value::Boolean(v) => visitor.visit_bool(v),
@ -548,14 +595,18 @@ impl<'de> de::Deserializer<'de> for Value {
{
match self {
Value::String(variant) => visitor.visit_enum(variant.into_deserializer()),
_ => Err(de::Error::invalid_type(de::Unexpected::UnitVariant, &"string only")),
_ => Err(de::Error::invalid_type(
de::Unexpected::UnitVariant,
&"string only",
)),
}
}
// `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, ::de::Error>
where V: de::Visitor<'de>,
where
V: de::Visitor<'de>,
{
visitor.visit_some(self)
}
@ -563,9 +614,10 @@ impl<'de> de::Deserializer<'de> for Value {
fn deserialize_newtype_struct<V>(
self,
_name: &'static str,
visitor: V
visitor: V,
) -> Result<V::Value, ::de::Error>
where V: de::Visitor<'de>
where
V: de::Visitor<'de>,
{
visitor.visit_newtype_struct(self)
}
@ -592,9 +644,9 @@ impl SeqDeserializer {
impl<'de> de::SeqAccess<'de> for SeqDeserializer {
type Error = ::de::Error;
fn next_element_seed<T>(&mut self, seed: T)
-> Result<Option<T::Value>, ::de::Error>
where T: de::DeserializeSeed<'de>,
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, ::de::Error>
where
T: de::DeserializeSeed<'de>,
{
match self.iter.next() {
Some(value) => seed.deserialize(value).map(Some),
@ -628,7 +680,8 @@ impl<'de> de::MapAccess<'de> for MapDeserializer {
type Error = ::de::Error;
fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, ::de::Error>
where T: de::DeserializeSeed<'de>,
where
T: de::DeserializeSeed<'de>,
{
match self.iter.next() {
Some((key, value)) => {
@ -640,7 +693,8 @@ impl<'de> de::MapAccess<'de> for MapDeserializer {
}
fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, ::de::Error>
where T: de::DeserializeSeed<'de>,
where
T: de::DeserializeSeed<'de>,
{
let (key, res) = match self.value.take() {
Some((key, value)) => (key, seed.deserialize(value)),
@ -749,35 +803,39 @@ impl ser::Serializer for Serializer {
Err(::ser::Error::UnsupportedType)
}
fn serialize_unit_struct(self, _name: &'static str)
-> Result<Value, ::ser::Error> {
fn serialize_unit_struct(self, _name: &'static str) -> Result<Value, ::ser::Error> {
Err(::ser::Error::UnsupportedType)
}
fn serialize_unit_variant(self,
fn serialize_unit_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str)
-> Result<Value, ::ser::Error> {
_variant: &'static str,
) -> Result<Value, ::ser::Error> {
self.serialize_str(_variant)
}
fn serialize_newtype_struct<T: ?Sized>(self,
fn serialize_newtype_struct<T: ?Sized>(
self,
_name: &'static str,
value: &T)
-> Result<Value, ::ser::Error>
where T: ser::Serialize,
value: &T,
) -> Result<Value, ::ser::Error>
where
T: ser::Serialize,
{
value.serialize(self)
}
fn serialize_newtype_variant<T: ?Sized>(self,
fn serialize_newtype_variant<T: ?Sized>(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_value: &T)
-> Result<Value, ::ser::Error>
where T: ser::Serialize,
_value: &T,
) -> Result<Value, ::ser::Error>
where
T: ser::Serialize,
{
Err(::ser::Error::UnsupportedType)
}
@ -787,16 +845,15 @@ impl ser::Serializer for Serializer {
}
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Value, ::ser::Error>
where T: ser::Serialize,
where
T: ser::Serialize,
{
value.serialize(self)
}
fn serialize_seq(self, len: Option<usize>)
-> Result<Self::SerializeSeq, ::ser::Error>
{
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, ::ser::Error> {
Ok(SerializeVec {
vec: Vec::with_capacity(len.unwrap_or(0))
vec: Vec::with_capacity(len.unwrap_or(0)),
})
}
@ -804,42 +861,46 @@ impl ser::Serializer for Serializer {
self.serialize_seq(Some(len))
}
fn serialize_tuple_struct(self, _name: &'static str, len: usize)
-> Result<Self::SerializeTupleStruct, ::ser::Error> {
fn serialize_tuple_struct(
self,
_name: &'static str,
len: usize,
) -> Result<Self::SerializeTupleStruct, ::ser::Error> {
self.serialize_seq(Some(len))
}
fn serialize_tuple_variant(self,
fn serialize_tuple_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
len: usize)
-> Result<Self::SerializeTupleVariant, ::ser::Error>
{
len: usize,
) -> Result<Self::SerializeTupleVariant, ::ser::Error> {
self.serialize_seq(Some(len))
}
fn serialize_map(self, _len: Option<usize>)
-> Result<Self::SerializeMap, ::ser::Error>
{
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, ::ser::Error> {
Ok(SerializeMap {
map: BTreeMap::new(),
next_key: None,
})
}
fn serialize_struct(self, _name: &'static str, len: usize)
-> Result<Self::SerializeStruct, ::ser::Error> {
fn serialize_struct(
self,
_name: &'static str,
len: usize,
) -> Result<Self::SerializeStruct, ::ser::Error> {
self.serialize_map(Some(len))
}
fn serialize_struct_variant(self,
fn serialize_struct_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize)
-> Result<Self::SerializeStructVariant, ::ser::Error>
{
_len: usize,
) -> Result<Self::SerializeStructVariant, ::ser::Error> {
Err(::ser::Error::UnsupportedType)
}
}
@ -858,7 +919,8 @@ impl ser::SerializeSeq for SerializeVec {
type Error = ::ser::Error;
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), ::ser::Error>
where T: ser::Serialize
where
T: ser::Serialize,
{
self.vec.push(Value::try_from(value)?);
Ok(())
@ -874,7 +936,8 @@ impl ser::SerializeTuple for SerializeVec {
type Error = ::ser::Error;
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), ::ser::Error>
where T: ser::Serialize
where
T: ser::Serialize,
{
ser::SerializeSeq::serialize_element(self, value)
}
@ -889,7 +952,8 @@ impl ser::SerializeTupleStruct for SerializeVec {
type Error = ::ser::Error;
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), ::ser::Error>
where T: ser::Serialize
where
T: ser::Serialize,
{
ser::SerializeSeq::serialize_element(self, value)
}
@ -904,7 +968,8 @@ impl ser::SerializeTupleVariant for SerializeVec {
type Error = ::ser::Error;
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), ::ser::Error>
where T: ser::Serialize
where
T: ser::Serialize,
{
ser::SerializeSeq::serialize_element(self, value)
}
@ -919,7 +984,8 @@ impl ser::SerializeMap for SerializeMap {
type Error = ::ser::Error;
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), ::ser::Error>
where T: ser::Serialize
where
T: ser::Serialize,
{
match Value::try_from(key)? {
Value::String(s) => self.next_key = Some(s),
@ -929,12 +995,15 @@ impl ser::SerializeMap for SerializeMap {
}
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), ::ser::Error>
where T: ser::Serialize
where
T: ser::Serialize,
{
let key = self.next_key.take();
let key = key.expect("serialize_value called before serialize_key");
match Value::try_from(value) {
Ok(value) => { self.map.insert(key, value); }
Ok(value) => {
self.map.insert(key, value);
}
Err(::ser::Error::UnsupportedNone) => {}
Err(e) => return Err(e),
}
@ -950,8 +1019,13 @@ impl ser::SerializeStruct for SerializeMap {
type Ok = Value;
type Error = ::ser::Error;
fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), ::ser::Error>
where T: ser::Serialize
fn serialize_field<T: ?Sized>(
&mut self,
key: &'static str,
value: &T,
) -> Result<(), ::ser::Error>
where
T: ser::Serialize,
{
ser::SerializeMap::serialize_key(self, key)?;
ser::SerializeMap::serialize_value(self, value)
@ -970,7 +1044,8 @@ impl<'a, 'de> de::DeserializeSeed<'de> for DatetimeOrTable<'a> {
type Value = bool;
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where D: de::Deserializer<'de>
where
D: de::Deserializer<'de>,
{
deserializer.deserialize_any(self)
}
@ -984,7 +1059,8 @@ impl<'a, 'de> de::Visitor<'de> for DatetimeOrTable<'a> {
}
fn visit_str<E>(self, s: &str) -> Result<bool, E>
where E: de::Error,
where
E: de::Error,
{
if s == datetime::FIELD {
Ok(true)
@ -995,7 +1071,8 @@ impl<'a, 'de> de::Visitor<'de> for DatetimeOrTable<'a> {
}
fn visit_string<E>(self, s: String) -> Result<bool, E>
where E: de::Error,
where
E: de::Error,
{
if s == datetime::FIELD {
Ok(true)