Merge pull request #321 from NickHackman/fix-clippy-warnings-and-errors

Fix clippy warnings and errors
This commit is contained in:
Alex Crichton 2019-08-20 14:51:26 -05:00 committed by GitHub
commit 9d7890a6d0
7 changed files with 133 additions and 152 deletions

View file

@ -40,8 +40,8 @@ pub struct DatetimeParseError {
// //
// In general the TOML encoder/decoder will catch this and not literally emit // In general the TOML encoder/decoder will catch this and not literally emit
// these strings but rather emit datetimes as they're intended. // these strings but rather emit datetimes as they're intended.
pub const FIELD: &'static str = "$__toml_private_datetime"; pub const FIELD: &str = "$__toml_private_datetime";
pub const NAME: &'static str = "$__toml_private_Datetime"; pub const NAME: &str = "$__toml_private_Datetime";
#[derive(PartialEq, Clone)] #[derive(PartialEq, Clone)]
struct Date { struct Date {
@ -135,10 +135,10 @@ impl FromStr for Datetime {
offset_allowed = false; offset_allowed = false;
None None
} else { } else {
let y1 = digit(&mut chars)? as u16; let y1 = u16::from(digit(&mut chars)?);
let y2 = digit(&mut chars)? as u16; let y2 = u16::from(digit(&mut chars)?);
let y3 = digit(&mut chars)? as u16; let y3 = u16::from(digit(&mut chars)?);
let y4 = digit(&mut chars)? as u16; let y4 = u16::from(digit(&mut chars)?);
match chars.next() { match chars.next() {
Some('-') => {} Some('-') => {}
@ -210,7 +210,7 @@ impl FromStr for Datetime {
b'0'..=b'9' => { b'0'..=b'9' => {
if i < 9 { if i < 9 {
let p = 10_u32.pow(8 - i as u32); let p = 10_u32.pow(8 - i as u32);
nanosecond += p * (byte - b'0') as u32; nanosecond += p * u32::from(byte - b'0');
} }
} }
_ => { _ => {
@ -229,7 +229,7 @@ impl FromStr for Datetime {
hour: h1 * 10 + h2, hour: h1 * 10 + h2,
minute: m1 * 10 + m2, minute: m1 * 10 + m2,
second: s1 * 10 + s2, second: s1 * 10 + s2,
nanosecond: nanosecond, nanosecond,
}; };
if time.hour > 24 { if time.hour > 24 {
@ -292,8 +292,8 @@ impl FromStr for Datetime {
Ok(Datetime { Ok(Datetime {
date: full_date, date: full_date,
time: time, time,
offset: offset, offset,
}) })
} }
} }
@ -345,7 +345,7 @@ impl<'de> de::Deserialize<'de> for Datetime {
} }
} }
static FIELDS: [&'static str; 1] = [FIELD]; static FIELDS: [&str; 1] = [FIELD];
deserializer.deserialize_struct(NAME, &FIELDS, DatetimeVisitor) deserializer.deserialize_struct(NAME, &FIELDS, DatetimeVisitor)
} }
} }

153
src/de.rs
View file

@ -20,6 +20,9 @@ use crate::datetime;
use crate::spanned; use crate::spanned;
use crate::tokens::{Error as TokenError, Span, Token, Tokenizer}; use crate::tokens::{Error as TokenError, Span, Token, Tokenizer};
/// Type Alias for a TOML Table pair
type TablePair<'a> = (Cow<'a, str>, Value<'a>);
/// Deserializes a byte slice into a type. /// Deserializes a byte slice into a type.
/// ///
/// This function will attempt to interpret `bytes` as UTF-8 data and then /// This function will attempt to interpret `bytes` as UTF-8 data and then
@ -270,9 +273,9 @@ impl<'de, 'b> de::Deserializer<'de> for &'b mut Deserializer<'de> {
} }
E::DottedTable(_) => visitor.visit_enum(DottedTableDeserializer { E::DottedTable(_) => visitor.visit_enum(DottedTableDeserializer {
name: name.expect("Expected table header to be passed."), name: name.expect("Expected table header to be passed."),
value: value, value,
}), }),
e @ _ => Err(Error::from_kind( e => Err(Error::from_kind(
Some(value.start), Some(value.start),
ErrorKind::Wanted { ErrorKind::Wanted {
expected: "string or table", expected: "string or table",
@ -292,14 +295,14 @@ impl<'de, 'b> de::Deserializer<'de> for &'b mut Deserializer<'de> {
struct Table<'a> { struct Table<'a> {
at: usize, at: usize,
header: Vec<Cow<'a, str>>, header: Vec<Cow<'a, str>>,
values: Option<Vec<(Cow<'a, str>, Value<'a>)>>, values: Option<Vec<TablePair<'a>>>,
array: bool, array: bool,
} }
#[doc(hidden)] #[doc(hidden)]
pub struct MapVisitor<'de, 'b> { pub struct MapVisitor<'de, 'b> {
values: vec::IntoIter<(Cow<'de, str>, Value<'de>)>, values: vec::IntoIter<TablePair<'de>>,
next_value: Option<(Cow<'de, str>, Value<'de>)>, next_value: Option<TablePair<'de>>,
depth: usize, depth: usize,
cur: usize, cur: usize,
cur_parent: usize, cur_parent: usize,
@ -425,7 +428,7 @@ impl<'de, 'b> de::MapAccess<'de> for MapVisitor<'de, 'b> {
cur_parent: self.cur - 1, cur_parent: self.cur - 1,
cur: 0, cur: 0,
max: self.max, max: self.max,
array: array, array,
tables: &mut *self.tables, tables: &mut *self.tables,
de: &mut *self.de, de: &mut *self.de,
}); });
@ -525,7 +528,7 @@ struct StrDeserializer<'a> {
impl<'a> StrDeserializer<'a> { impl<'a> StrDeserializer<'a> {
fn new(key: Cow<'a, str>) -> StrDeserializer<'a> { fn new(key: Cow<'a, str>) -> StrDeserializer<'a> {
StrDeserializer { key: key } StrDeserializer { key }
} }
} }
@ -557,7 +560,7 @@ struct ValueDeserializer<'a> {
impl<'a> ValueDeserializer<'a> { impl<'a> ValueDeserializer<'a> {
fn new(value: Value<'a>) -> ValueDeserializer<'a> { fn new(value: Value<'a>) -> ValueDeserializer<'a> {
ValueDeserializer { ValueDeserializer {
value: value, value,
validate_struct_keys: false, validate_struct_keys: false,
} }
} }
@ -615,7 +618,7 @@ impl<'de> de::Deserializer<'de> for ValueDeserializer<'de> {
where where
V: de::Visitor<'de>, V: de::Visitor<'de>,
{ {
if name == datetime::NAME && fields == &[datetime::FIELD] { if name == datetime::NAME && fields == [datetime::FIELD] {
if let E::Datetime(s) = self.value.e { if let E::Datetime(s) = self.value.e {
return visitor.visit_map(DatetimeDeserializer { return visitor.visit_map(DatetimeDeserializer {
date: s, date: s,
@ -625,8 +628,8 @@ impl<'de> de::Deserializer<'de> for ValueDeserializer<'de> {
} }
if self.validate_struct_keys { if self.validate_struct_keys {
match &self.value.e { match self.value.e {
&E::InlineTable(ref values) | &E::DottedTable(ref values) => { E::InlineTable(ref values) | E::DottedTable(ref values) => {
let extra_fields = values let extra_fields = values
.iter() .iter()
.filter_map(|key_value| { .filter_map(|key_value| {
@ -656,7 +659,7 @@ impl<'de> de::Deserializer<'de> for ValueDeserializer<'de> {
} }
} }
if name == spanned::NAME && fields == &[spanned::START, spanned::END, spanned::VALUE] { if name == spanned::NAME && fields == [spanned::START, spanned::END, spanned::VALUE] {
let start = self.value.start; let start = self.value.start;
let end = self.value.end; let end = self.value.end;
@ -710,7 +713,7 @@ impl<'de> de::Deserializer<'de> for ValueDeserializer<'de> {
}) })
} }
} }
e @ _ => Err(Error::from_kind( e => Err(Error::from_kind(
Some(self.value.start), Some(self.value.start),
ErrorKind::Wanted { ErrorKind::Wanted {
expected: "string or inline table", expected: "string or inline table",
@ -850,12 +853,12 @@ impl<'de> de::EnumAccess<'de> for DottedTableDeserializer<'de> {
{ {
let (name, value) = (self.name, self.value); let (name, value) = (self.name, self.value);
seed.deserialize(StrDeserializer::new(name)) seed.deserialize(StrDeserializer::new(name))
.map(|val| (val, TableEnumDeserializer { value: value })) .map(|val| (val, TableEnumDeserializer { value }))
} }
} }
struct InlineTableDeserializer<'a> { struct InlineTableDeserializer<'a> {
values: vec::IntoIter<(Cow<'a, str>, Value<'a>)>, values: vec::IntoIter<TablePair<'a>>,
next_value: Option<Value<'a>>, next_value: Option<Value<'a>>,
} }
@ -905,7 +908,7 @@ impl<'de> de::EnumAccess<'de> for InlineTableDeserializer<'de> {
}; };
seed.deserialize(StrDeserializer::new(key)) seed.deserialize(StrDeserializer::new(key))
.map(|val| (val, TableEnumDeserializer { value: value })) .map(|val| (val, TableEnumDeserializer { value }))
} }
} }
@ -920,7 +923,7 @@ impl<'de> de::VariantAccess<'de> for TableEnumDeserializer<'de> {
fn unit_variant(self) -> Result<(), Self::Error> { fn unit_variant(self) -> Result<(), Self::Error> {
match self.value.e { match self.value.e {
E::InlineTable(values) | E::DottedTable(values) => { E::InlineTable(values) | E::DottedTable(values) => {
if values.len() == 0 { if values.is_empty() {
Ok(()) Ok(())
} else { } else {
Err(Error::from_kind( Err(Error::from_kind(
@ -929,7 +932,7 @@ impl<'de> de::VariantAccess<'de> for TableEnumDeserializer<'de> {
)) ))
} }
} }
e @ _ => Err(Error::from_kind( e => Err(Error::from_kind(
Some(self.value.start), Some(self.value.start),
ErrorKind::Wanted { ErrorKind::Wanted {
expected: "table", expected: "table",
@ -993,7 +996,7 @@ impl<'de> de::VariantAccess<'de> for TableEnumDeserializer<'de> {
)) ))
} }
} }
e @ _ => Err(Error::from_kind( e => Err(Error::from_kind(
Some(self.value.start), Some(self.value.start),
ErrorKind::Wanted { ErrorKind::Wanted {
expected: "table", expected: "table",
@ -1026,7 +1029,7 @@ impl<'a> Deserializer<'a> {
pub fn new(input: &'a str) -> Deserializer<'a> { pub fn new(input: &'a str) -> Deserializer<'a> {
Deserializer { Deserializer {
tokens: Tokenizer::new(input), tokens: Tokenizer::new(input),
input: input, input,
require_newline_after_table: true, require_newline_after_table: true,
allow_duplciate_after_longer_table: false, allow_duplciate_after_longer_table: false,
} }
@ -1080,10 +1083,10 @@ impl<'a> Deserializer<'a> {
tables.push(cur_table); tables.push(cur_table);
} }
cur_table = Table { cur_table = Table {
at: at, at,
header: Vec::new(), header: Vec::new(),
values: Some(Vec::new()), values: Some(Vec::new()),
array: array, array,
}; };
loop { loop {
let part = header.next().map_err(|e| self.token_error(e)); let part = header.next().map_err(|e| self.token_error(e));
@ -1151,7 +1154,7 @@ impl<'a> Deserializer<'a> {
Ok(Line::Table { Ok(Line::Table {
at: start, at: start,
header: ret, header: ret,
array: array, array,
}) })
} }
@ -1175,33 +1178,33 @@ impl<'a> Deserializer<'a> {
let value = match self.next()? { let value = match self.next()? {
Some((Span { start, end }, Token::String { val, .. })) => Value { Some((Span { start, end }, Token::String { val, .. })) => Value {
e: E::String(val), e: E::String(val),
start: start, start,
end: end, end,
}, },
Some((Span { start, end }, Token::Keylike("true"))) => Value { Some((Span { start, end }, Token::Keylike("true"))) => Value {
e: E::Boolean(true), e: E::Boolean(true),
start: start, start,
end: end, end,
}, },
Some((Span { start, end }, Token::Keylike("false"))) => Value { Some((Span { start, end }, Token::Keylike("false"))) => Value {
e: E::Boolean(false), e: E::Boolean(false),
start: start, start,
end: end, end,
}, },
Some((span, Token::Keylike(key))) => self.number_or_date(span, key)?, Some((span, Token::Keylike(key))) => self.number_or_date(span, key)?,
Some((span, Token::Plus)) => self.number_leading_plus(span)?, Some((span, Token::Plus)) => self.number_leading_plus(span)?,
Some((Span { start, .. }, Token::LeftBrace)) => { Some((Span { start, .. }, Token::LeftBrace)) => {
self.inline_table().map(|(Span { end, .. }, table)| Value { self.inline_table().map(|(Span { end, .. }, table)| Value {
e: E::InlineTable(table), e: E::InlineTable(table),
start: start, start,
end: end, end,
})? })?
} }
Some((Span { start, .. }, Token::LeftBracket)) => { Some((Span { start, .. }, Token::LeftBracket)) => {
self.array().map(|(Span { end, .. }, array)| Value { self.array().map(|(Span { end, .. }, array)| Value {
e: E::Array(array), e: E::Array(array),
start: start, start,
end: end, end,
})? })?
} }
Some(token) => { Some(token) => {
@ -1226,15 +1229,15 @@ impl<'a> Deserializer<'a> {
self.datetime(span, s, false) self.datetime(span, s, false)
.map(|(Span { start, end }, d)| Value { .map(|(Span { start, end }, d)| Value {
e: E::Datetime(d), e: E::Datetime(d),
start: start, start,
end: end, end,
}) })
} else if self.eat(Token::Colon)? { } else if self.eat(Token::Colon)? {
self.datetime(span, s, true) self.datetime(span, s, true)
.map(|(Span { start, end }, d)| Value { .map(|(Span { start, end }, d)| Value {
e: E::Datetime(d), e: E::Datetime(d),
start: start, start,
end: end, end,
}) })
} else { } else {
self.number(span, s) self.number(span, s)
@ -1282,8 +1285,8 @@ impl<'a> Deserializer<'a> {
Ok(( Ok((
Value { Value {
e: E::DottedTable(table.values.unwrap_or_else(Vec::new)), e: E::DottedTable(table.values.unwrap_or_else(Vec::new)),
start: start, start,
end: end, end,
}, },
Some(header.clone()), Some(header.clone()),
)) ))
@ -1296,8 +1299,8 @@ impl<'a> Deserializer<'a> {
fn number(&mut self, Span { start, end }: Span, s: &'a str) -> Result<Value<'a>, Error> { fn number(&mut self, Span { start, end }: Span, s: &'a str) -> Result<Value<'a>, Error> {
let to_integer = |f| Value { let to_integer = |f| Value {
e: E::Integer(f), e: E::Integer(f),
start: start, start,
end: end, end,
}; };
if s.starts_with("0x") { if s.starts_with("0x") {
self.integer(&s[2..], 16).map(to_integer) self.integer(&s[2..], 16).map(to_integer)
@ -1308,8 +1311,8 @@ impl<'a> Deserializer<'a> {
} else if s.contains('e') || s.contains('E') { } else if s.contains('e') || s.contains('E') {
self.float(s, None).map(|f| Value { self.float(s, None).map(|f| Value {
e: E::Float(f), e: E::Float(f),
start: start, start,
end: end, end,
}) })
} else if self.eat(Token::Period)? { } else if self.eat(Token::Period)? {
let at = self.tokens.current(); let at = self.tokens.current();
@ -1317,8 +1320,8 @@ impl<'a> Deserializer<'a> {
Some((Span { start, end }, Token::Keylike(after))) => { Some((Span { start, end }, Token::Keylike(after))) => {
self.float(s, Some(after)).map(|f| Value { self.float(s, Some(after)).map(|f| Value {
e: E::Float(f), e: E::Float(f),
start: start, start,
end: end, end,
}) })
} }
_ => Err(self.error(at, ErrorKind::NumberInvalid)), _ => Err(self.error(at, ErrorKind::NumberInvalid)),
@ -1326,26 +1329,26 @@ impl<'a> Deserializer<'a> {
} else if s == "inf" { } else if s == "inf" {
Ok(Value { Ok(Value {
e: E::Float(f64::INFINITY), e: E::Float(f64::INFINITY),
start: start, start,
end: end, end,
}) })
} else if s == "-inf" { } else if s == "-inf" {
Ok(Value { Ok(Value {
e: E::Float(f64::NEG_INFINITY), e: E::Float(f64::NEG_INFINITY),
start: start, start,
end: end, end,
}) })
} else if s == "nan" { } else if s == "nan" {
Ok(Value { Ok(Value {
e: E::Float(f64::NAN), e: E::Float(f64::NAN),
start: start, start,
end: end, end,
}) })
} else if s == "-nan" { } else if s == "-nan" {
Ok(Value { Ok(Value {
e: E::Float(-f64::NAN), e: E::Float(-f64::NAN),
start: start, start,
end: end, end,
}) })
} else { } else {
self.integer(s, 10).map(to_integer) self.integer(s, 10).map(to_integer)
@ -1355,13 +1358,7 @@ impl<'a> Deserializer<'a> {
fn number_leading_plus(&mut self, Span { start, .. }: Span) -> Result<Value<'a>, Error> { fn number_leading_plus(&mut self, Span { start, .. }: Span) -> Result<Value<'a>, Error> {
let start_token = self.tokens.current(); let start_token = self.tokens.current();
match self.next()? { match self.next()? {
Some((Span { end, .. }, Token::Keylike(s))) => self.number( Some((Span { end, .. }, Token::Keylike(s))) => self.number(Span { start, end }, s),
Span {
start: start,
end: end,
},
s,
),
_ => Err(self.error(start_token, ErrorKind::NumberInvalid)), _ => Err(self.error(start_token, ErrorKind::NumberInvalid)),
} }
} }
@ -1545,7 +1542,7 @@ impl<'a> Deserializer<'a> {
// TODO(#140): shouldn't buffer up this entire table in memory, it'd be // TODO(#140): shouldn't buffer up this entire table in memory, it'd be
// great to defer parsing everything until later. // great to defer parsing everything until later.
fn inline_table(&mut self) -> Result<(Span, Vec<(Cow<'a, str>, Value<'a>)>), Error> { fn inline_table(&mut self) -> Result<(Span, Vec<TablePair<'a>>), Error> {
let mut ret = Vec::new(); let mut ret = Vec::new();
self.eat_whitespace()?; self.eat_whitespace()?;
if let Some(span) = self.eat_spanned(Token::RightBrace)? { if let Some(span) = self.eat_spanned(Token::RightBrace)? {
@ -1641,7 +1638,7 @@ impl<'a> Deserializer<'a> {
&self, &self,
mut key_parts: Vec<Cow<'a, str>>, mut key_parts: Vec<Cow<'a, str>>,
value: Value<'a>, value: Value<'a>,
values: &mut Vec<(Cow<'a, str>, Value<'a>)>, values: &mut Vec<TablePair<'a>>,
) -> Result<(), Error> { ) -> Result<(), Error> {
let key = key_parts.remove(0); let key = key_parts.remove(0);
if key_parts.is_empty() { if key_parts.is_empty() {
@ -1752,13 +1749,7 @@ impl<'a> Deserializer<'a> {
at, at,
expected, expected,
found, found,
} => self.error( } => self.error(at, ErrorKind::Wanted { expected, found }),
at,
ErrorKind::Wanted {
expected: expected,
found: found,
},
),
TokenError::EmptyTableKey(at) => self.error(at, ErrorKind::EmptyTableKey), TokenError::EmptyTableKey(at) => self.error(at, ErrorKind::EmptyTableKey),
TokenError::MultilineStringKey(at) => self.error(at, ErrorKind::MultilineStringKey), TokenError::MultilineStringKey(at) => self.error(at, ErrorKind::MultilineStringKey),
} }
@ -1799,7 +1790,7 @@ impl Error {
fn from_kind(at: Option<usize>, kind: ErrorKind) -> Error { fn from_kind(at: Option<usize>, kind: ErrorKind) -> Error {
Error { Error {
inner: Box::new(ErrorInner { inner: Box::new(ErrorInner {
kind: kind, kind,
line: None, line: None,
col: 0, col: 0,
at, at,
@ -1829,7 +1820,7 @@ impl Error {
self.inner.key.insert(0, key.to_string()); self.inner.key.insert(0, key.to_string());
} }
fn fix_offset<F>(&mut self, f: F) -> () fn fix_offset<F>(&mut self, f: F)
where where
F: FnOnce() -> Option<usize>, F: FnOnce() -> Option<usize>,
{ {
@ -1840,7 +1831,7 @@ impl Error {
} }
} }
fn fix_linecol<F>(&mut self, f: F) -> () fn fix_linecol<F>(&mut self, f: F)
where where
F: FnOnce(usize) -> (usize, usize), F: FnOnce(usize) -> (usize, usize),
{ {
@ -1854,7 +1845,7 @@ impl Error {
impl std::convert::From<Error> for std::io::Error { impl std::convert::From<Error> for std::io::Error {
fn from(e: Error) -> Self { fn from(e: Error) -> Self {
return std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()); std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string())
} }
} }
@ -1995,9 +1986,9 @@ impl<'a> Header<'a> {
fn new(tokens: Tokenizer<'a>, array: bool, require_newline_after_table: bool) -> Header<'a> { fn new(tokens: Tokenizer<'a>, array: bool, require_newline_after_table: bool) -> Header<'a> {
Header { Header {
first: true, first: true,
array: array, array,
tokens: tokens, tokens,
require_newline_after_table: require_newline_after_table, require_newline_after_table,
} }
} }
@ -2015,10 +2006,8 @@ impl<'a> Header<'a> {
} }
self.tokens.eat_whitespace()?; self.tokens.eat_whitespace()?;
if self.require_newline_after_table { if self.require_newline_after_table && !self.tokens.eat_comment()? {
if !self.tokens.eat_comment()? { self.tokens.eat_newline_or_eof()?;
self.tokens.eat_newline_or_eof()?;
}
} }
Ok(None) Ok(None)
} }
@ -2040,8 +2029,8 @@ enum E<'a> {
String(Cow<'a, str>), String(Cow<'a, str>),
Datetime(&'a str), Datetime(&'a str),
Array(Vec<Value<'a>>), Array(Vec<Value<'a>>),
InlineTable(Vec<(Cow<'a, str>, Value<'a>)>), InlineTable(Vec<TablePair<'a>>),
DottedTable(Vec<(Cow<'a, str>, Value<'a>)>), DottedTable(Vec<TablePair<'a>>),
} }
impl<'a> E<'a> { impl<'a> E<'a> {

View file

@ -150,8 +150,8 @@ impl Map<String, Value> {
use std::collections::btree_map::Entry as EntryImpl; use std::collections::btree_map::Entry as EntryImpl;
match self.map.entry(key.into()) { match self.map.entry(key.into()) {
EntryImpl::Vacant(vacant) => Entry::Vacant(VacantEntry { vacant: vacant }), EntryImpl::Vacant(vacant) => Entry::Vacant(VacantEntry { vacant }),
EntryImpl::Occupied(occupied) => Entry::Occupied(OccupiedEntry { occupied: occupied }), EntryImpl::Occupied(occupied) => Entry::Occupied(OccupiedEntry { occupied }),
} }
} }

View file

@ -247,7 +247,7 @@ impl<'a> Serializer<'a> {
/// will be present in `dst`. /// will be present in `dst`.
pub fn new(dst: &'a mut String) -> Serializer<'a> { pub fn new(dst: &'a mut String) -> Serializer<'a> {
Serializer { Serializer {
dst: dst, dst,
state: State::End, state: State::End,
settings: Rc::new(Settings::default()), settings: Rc::new(Settings::default()),
} }
@ -263,7 +263,7 @@ impl<'a> Serializer<'a> {
/// have a trailing comma. See `Serializer::pretty_array` /// have a trailing comma. See `Serializer::pretty_array`
pub fn pretty(dst: &'a mut String) -> Serializer<'a> { pub fn pretty(dst: &'a mut String) -> Serializer<'a> {
Serializer { Serializer {
dst: dst, dst,
state: State::End, state: State::End,
settings: Rc::new(Settings { settings: Rc::new(Settings {
array: Some(ArraySettings::pretty()), array: Some(ArraySettings::pretty()),
@ -331,13 +331,12 @@ impl<'a> Serializer<'a> {
/// """ /// """
/// ``` /// ```
pub fn pretty_string_literal(&mut self, value: bool) -> &mut Self { pub fn pretty_string_literal(&mut self, value: bool) -> &mut Self {
let use_default = let use_default = if let Some(ref mut s) = Rc::get_mut(&mut self.settings).unwrap().string {
if let &mut Some(ref mut s) = &mut Rc::get_mut(&mut self.settings).unwrap().string { s.literal = value;
s.literal = value; false
false } else {
} else { true
true };
};
if use_default { if use_default {
let mut string = StringSettings::pretty(); let mut string = StringSettings::pretty();
@ -387,13 +386,12 @@ impl<'a> Serializer<'a> {
/// ///
/// See `Serializer::pretty_array` for more details. /// See `Serializer::pretty_array` for more details.
pub fn pretty_array_indent(&mut self, value: usize) -> &mut Self { pub fn pretty_array_indent(&mut self, value: usize) -> &mut Self {
let use_default = let use_default = if let Some(ref mut a) = Rc::get_mut(&mut self.settings).unwrap().array {
if let &mut Some(ref mut a) = &mut Rc::get_mut(&mut self.settings).unwrap().array { a.indent = value;
a.indent = value; false
false } else {
} else { true
true };
};
if use_default { if use_default {
let mut array = ArraySettings::pretty(); let mut array = ArraySettings::pretty();
@ -407,13 +405,12 @@ impl<'a> Serializer<'a> {
/// ///
/// See `Serializer::pretty_array` for more details. /// See `Serializer::pretty_array` for more details.
pub fn pretty_array_trailing_comma(&mut self, value: bool) -> &mut Self { pub fn pretty_array_trailing_comma(&mut self, value: bool) -> &mut Self {
let use_default = let use_default = if let Some(ref mut a) = Rc::get_mut(&mut self.settings).unwrap().array {
if let &mut Some(ref mut a) = &mut Rc::get_mut(&mut self.settings).unwrap().array { a.trailing_comma = value;
a.trailing_comma = value; false
false } else {
} else { true
true };
};
if use_default { if use_default {
let mut array = ArraySettings::pretty(); let mut array = ArraySettings::pretty();
@ -425,7 +422,7 @@ impl<'a> Serializer<'a> {
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_)?; self.emit_key(type_)?;
drop(write!(self.dst, "{}", t)); write!(self.dst, "{}", t).map_err(ser::Error::custom)?;
if let State::Table { .. } = self.state { if let State::Table { .. } = self.state {
self.dst.push_str("\n"); self.dst.push_str("\n");
} }
@ -518,7 +515,7 @@ impl<'a> Serializer<'a> {
_ => false, _ => false,
}); });
if ok { if ok {
drop(write!(self.dst, "{}", key)); write!(self.dst, "{}", key).map_err(ser::Error::custom)?;
} else { } else {
self.emit_str(key, true)?; self.emit_str(key, true)?;
} }
@ -610,7 +607,7 @@ impl<'a> Serializer<'a> {
(&Some(StringSettings { literal: false, .. }), Repr::Literal(_, ty)) => { (&Some(StringSettings { literal: false, .. }), Repr::Literal(_, ty)) => {
Repr::Std(ty) Repr::Std(ty)
} }
(_, r @ _) => r, (_, r) => r,
} }
} else { } else {
Repr::Std(Type::OnelineSingle) Repr::Std(Type::OnelineSingle)
@ -650,7 +647,9 @@ impl<'a> Serializer<'a> {
'\u{d}' => self.dst.push_str("\\r"), '\u{d}' => self.dst.push_str("\\r"),
'\u{22}' => self.dst.push_str("\\\""), '\u{22}' => self.dst.push_str("\\\""),
'\u{5c}' => self.dst.push_str("\\\\"), '\u{5c}' => self.dst.push_str("\\\\"),
c if c < '\u{1f}' => drop(write!(self.dst, "\\u{:04X}", ch as u32)), c if c < '\u{1f}' => {
write!(self.dst, "\\u{:04X}", ch as u32).map_err(ser::Error::custom)?;
}
ch => self.dst.push(ch), ch => self.dst.push(ch),
} }
} }
@ -753,15 +752,15 @@ macro_rules! serialize_float {
($this:expr, $v:expr) => {{ ($this:expr, $v:expr) => {{
$this.emit_key("float")?; $this.emit_key("float")?;
if ($v.is_nan() || $v == 0.0) && $v.is_sign_negative() { if ($v.is_nan() || $v == 0.0) && $v.is_sign_negative() {
drop(write!($this.dst, "-")); write!($this.dst, "-").map_err(ser::Error::custom)?;
} }
if $v.is_nan() { if $v.is_nan() {
drop(write!($this.dst, "nan")); write!($this.dst, "nan").map_err(ser::Error::custom)?;
} else { } else {
drop(write!($this.dst, "{}", $v)); write!($this.dst, "{}", $v).map_err(ser::Error::custom)?;
} }
if $v % 1.0 == 0.0 { if $v % 1.0 == 0.0 {
drop(write!($this.dst, ".0")); write!($this.dst, ".0").map_err(ser::Error::custom)?;
} }
if let State::Table { .. } = $this.state { if let State::Table { .. } = $this.state {
$this.dst.push_str("\n"); $this.dst.push_str("\n");
@ -902,7 +901,7 @@ impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> {
ser: self, ser: self,
first: Cell::new(true), first: Cell::new(true),
type_: Cell::new(None), type_: Cell::new(None),
len: len, len,
}) })
} }
@ -1099,10 +1098,10 @@ impl<'a, 'b> ser::SerializeMap for SerializeTable<'a, 'b> {
let res = value.serialize(&mut Serializer { let res = value.serialize(&mut Serializer {
dst: &mut *ser.dst, dst: &mut *ser.dst,
state: State::Table { state: State::Table {
key: key, key,
parent: &ser.state, parent: &ser.state,
first: first, first,
table_emitted: table_emitted, table_emitted,
}, },
settings: ser.settings.clone(), settings: ser.settings.clone(),
}); });
@ -1155,10 +1154,10 @@ impl<'a, 'b> ser::SerializeStruct for SerializeTable<'a, 'b> {
let res = value.serialize(&mut Serializer { let res = value.serialize(&mut Serializer {
dst: &mut *ser.dst, dst: &mut *ser.dst,
state: State::Table { state: State::Table {
key: key, key,
parent: &ser.state, parent: &ser.state,
first: first, first,
table_emitted: table_emitted, table_emitted,
}, },
settings: ser.settings.clone(), settings: ser.settings.clone(),
}); });

View file

@ -2,13 +2,13 @@ use serde::{de, ser};
use std::fmt; use std::fmt;
#[doc(hidden)] #[doc(hidden)]
pub const NAME: &'static str = "$__toml_private_Spanned"; pub const NAME: &str = "$__toml_private_Spanned";
#[doc(hidden)] #[doc(hidden)]
pub const START: &'static str = "$__toml_private_start"; pub const START: &str = "$__toml_private_start";
#[doc(hidden)] #[doc(hidden)]
pub const END: &'static str = "$__toml_private_end"; pub const END: &str = "$__toml_private_end";
#[doc(hidden)] #[doc(hidden)]
pub const VALUE: &'static str = "$__toml_private_value"; pub const VALUE: &str = "$__toml_private_value";
/// A spanned value, indicating the range at which it is defined in the source. /// A spanned value, indicating the range at which it is defined in the source.
/// ///
@ -116,17 +116,13 @@ where
let value: T = visitor.next_value()?; let value: T = visitor.next_value()?;
Ok(Spanned { Ok(Spanned { start, end, value })
start: start,
end: end,
value: value,
})
} }
} }
let visitor = SpannedVisitor(::std::marker::PhantomData); let visitor = SpannedVisitor(::std::marker::PhantomData);
static FIELDS: [&'static str; 3] = [START, END, VALUE]; static FIELDS: [&str; 3] = [START, END, VALUE];
deserializer.deserialize_struct(NAME, &FIELDS, visitor) deserializer.deserialize_struct(NAME, &FIELDS, visitor)
} }
} }

View file

@ -84,7 +84,7 @@ enum MaybeString {
impl<'a> Tokenizer<'a> { impl<'a> Tokenizer<'a> {
pub fn new(input: &'a str) -> Tokenizer<'a> { pub fn new(input: &'a str) -> Tokenizer<'a> {
let mut t = Tokenizer { let mut t = Tokenizer {
input: input, input,
chars: CrlfFold { chars: CrlfFold {
chars: input.char_indices(), chars: input.char_indices(),
}, },
@ -266,7 +266,7 @@ impl<'a> Tokenizer<'a> {
.clone() .clone()
.next() .next()
.map(|i| i.0) .map(|i| i.0)
.unwrap_or(self.input.len()) .unwrap_or_else(|| self.input.len())
} }
pub fn input(&self) -> &'a str { pub fn input(&self) -> &'a str {
@ -349,7 +349,7 @@ impl<'a> Tokenizer<'a> {
return Ok(String { return Ok(String {
src: &self.input[start..self.current()], src: &self.input[start..self.current()],
val: val.into_cow(&self.input[..i]), val: val.into_cow(&self.input[..i]),
multiline: multiline, multiline,
}); });
} }
Some((i, c)) => new_ch(self, &mut val, multiline, i, c)?, Some((i, c)) => new_ch(self, &mut val, multiline, i, c)?,
@ -463,10 +463,7 @@ impl<'a> Tokenizer<'a> {
.peek_one() .peek_one()
.map(|t| t.0) .map(|t| t.0)
.unwrap_or_else(|| self.input.len()); .unwrap_or_else(|| self.input.len());
Span { Span { start, end }
start: start,
end: end,
}
} }
/// Peek one char without consuming it. /// Peek one char without consuming it.
@ -642,7 +639,7 @@ mod tests {
err(r#""\U00""#, Error::InvalidHexEscape(5, '"')); err(r#""\U00""#, Error::InvalidHexEscape(5, '"'));
err(r#""\U00"#, Error::UnterminatedString(0)); err(r#""\U00"#, Error::UnterminatedString(0));
err(r#""\uD800"#, Error::InvalidEscapeValue(2, 0xd800)); err(r#""\uD800"#, Error::InvalidEscapeValue(2, 0xd800));
err(r#""\UFFFFFFFF"#, Error::InvalidEscapeValue(2, 0xffffffff)); err(r#""\UFFFFFFFF"#, Error::InvalidEscapeValue(2, 0xffff_ffff));
} }
#[test] #[test]

View file

@ -749,7 +749,7 @@ impl ser::Serializer for Serializer {
} }
fn serialize_i64(self, value: i64) -> Result<Value, crate::ser::Error> { fn serialize_i64(self, value: i64) -> Result<Value, crate::ser::Error> {
Ok(Value::Integer(value.into())) Ok(Value::Integer(value))
} }
fn serialize_u8(self, value: u8) -> Result<Value, crate::ser::Error> { fn serialize_u8(self, value: u8) -> Result<Value, crate::ser::Error> {