diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/datetime.rs | 22 | ||||
-rw-r--r-- | src/de.rs | 134 | ||||
-rw-r--r-- | src/map.rs | 4 | ||||
-rw-r--r-- | src/ser.rs | 59 | ||||
-rw-r--r-- | src/spanned.rs | 16 | ||||
-rw-r--r-- | src/tokens.rs | 13 | ||||
-rw-r--r-- | src/value.rs | 2 |
7 files changed, 113 insertions, 137 deletions
diff --git a/src/datetime.rs b/src/datetime.rs index 61eb3b1..683bbda 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -40,8 +40,8 @@ pub struct DatetimeParseError { // // In general the TOML encoder/decoder will catch this and not literally emit // these strings but rather emit datetimes as they're intended. -pub const FIELD: &'static str = "$__toml_private_datetime"; -pub const NAME: &'static str = "$__toml_private_Datetime"; +pub const FIELD: &str = "$__toml_private_datetime"; +pub const NAME: &str = "$__toml_private_Datetime"; #[derive(PartialEq, Clone)] struct Date { @@ -135,10 +135,10 @@ impl FromStr for Datetime { offset_allowed = false; None } else { - let y1 = digit(&mut chars)? as u16; - let y2 = digit(&mut chars)? as u16; - let y3 = digit(&mut chars)? as u16; - let y4 = digit(&mut chars)? as u16; + let y1 = u16::from(digit(&mut chars)?); + let y2 = u16::from(digit(&mut chars)?); + let y3 = u16::from(digit(&mut chars)?); + let y4 = u16::from(digit(&mut chars)?); match chars.next() { Some('-') => {} @@ -210,7 +210,7 @@ impl FromStr for Datetime { b'0'..=b'9' => { if i < 9 { 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, minute: m1 * 10 + m2, second: s1 * 10 + s2, - nanosecond: nanosecond, + nanosecond, }; if time.hour > 24 { @@ -292,8 +292,8 @@ impl FromStr for Datetime { Ok(Datetime { date: full_date, - time: time, - offset: offset, + time, + 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) } } @@ -270,9 +270,9 @@ impl<'de, 'b> de::Deserializer<'de> for &'b mut Deserializer<'de> { } E::DottedTable(_) => visitor.visit_enum(DottedTableDeserializer { 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), ErrorKind::Wanted { expected: "string or table", @@ -425,7 +425,7 @@ impl<'de, 'b> de::MapAccess<'de> for MapVisitor<'de, 'b> { cur_parent: self.cur - 1, cur: 0, max: self.max, - array: array, + array, tables: &mut *self.tables, de: &mut *self.de, }); @@ -525,7 +525,7 @@ struct StrDeserializer<'a> { impl<'a> StrDeserializer<'a> { fn new(key: Cow<'a, str>) -> StrDeserializer<'a> { - StrDeserializer { key: key } + StrDeserializer { key } } } @@ -557,7 +557,7 @@ struct ValueDeserializer<'a> { impl<'a> ValueDeserializer<'a> { fn new(value: Value<'a>) -> ValueDeserializer<'a> { ValueDeserializer { - value: value, + value, validate_struct_keys: false, } } @@ -615,7 +615,7 @@ impl<'de> de::Deserializer<'de> for ValueDeserializer<'de> { where 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 { return visitor.visit_map(DatetimeDeserializer { date: s, @@ -625,8 +625,8 @@ impl<'de> de::Deserializer<'de> for ValueDeserializer<'de> { } if self.validate_struct_keys { - match &self.value.e { - &E::InlineTable(ref values) | &E::DottedTable(ref values) => { + match self.value.e { + E::InlineTable(ref values) | E::DottedTable(ref values) => { let extra_fields = values .iter() .filter_map(|key_value| { @@ -656,7 +656,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 end = self.value.end; @@ -710,7 +710,7 @@ impl<'de> de::Deserializer<'de> for ValueDeserializer<'de> { }) } } - e @ _ => Err(Error::from_kind( + e => Err(Error::from_kind( Some(self.value.start), ErrorKind::Wanted { expected: "string or inline table", @@ -850,7 +850,7 @@ impl<'de> de::EnumAccess<'de> for DottedTableDeserializer<'de> { { let (name, value) = (self.name, self.value); seed.deserialize(StrDeserializer::new(name)) - .map(|val| (val, TableEnumDeserializer { value: value })) + .map(|val| (val, TableEnumDeserializer { value })) } } @@ -905,7 +905,7 @@ impl<'de> de::EnumAccess<'de> for InlineTableDeserializer<'de> { }; seed.deserialize(StrDeserializer::new(key)) - .map(|val| (val, TableEnumDeserializer { value: value })) + .map(|val| (val, TableEnumDeserializer { value })) } } @@ -920,7 +920,7 @@ impl<'de> de::VariantAccess<'de> for TableEnumDeserializer<'de> { fn unit_variant(self) -> Result<(), Self::Error> { match self.value.e { E::InlineTable(values) | E::DottedTable(values) => { - if values.len() == 0 { + if values.is_empty() { Ok(()) } else { Err(Error::from_kind( @@ -929,7 +929,7 @@ impl<'de> de::VariantAccess<'de> for TableEnumDeserializer<'de> { )) } } - e @ _ => Err(Error::from_kind( + e => Err(Error::from_kind( Some(self.value.start), ErrorKind::Wanted { expected: "table", @@ -993,7 +993,7 @@ impl<'de> de::VariantAccess<'de> for TableEnumDeserializer<'de> { )) } } - e @ _ => Err(Error::from_kind( + e => Err(Error::from_kind( Some(self.value.start), ErrorKind::Wanted { expected: "table", @@ -1026,7 +1026,7 @@ impl<'a> Deserializer<'a> { pub fn new(input: &'a str) -> Deserializer<'a> { Deserializer { tokens: Tokenizer::new(input), - input: input, + input, require_newline_after_table: true, allow_duplciate_after_longer_table: false, } @@ -1080,10 +1080,10 @@ impl<'a> Deserializer<'a> { tables.push(cur_table); } cur_table = Table { - at: at, + at, header: Vec::new(), values: Some(Vec::new()), - array: array, + array, }; loop { let part = header.next().map_err(|e| self.token_error(e)); @@ -1151,7 +1151,7 @@ impl<'a> Deserializer<'a> { Ok(Line::Table { at: start, header: ret, - array: array, + array, }) } @@ -1175,33 +1175,33 @@ impl<'a> Deserializer<'a> { let value = match self.next()? { Some((Span { start, end }, Token::String { val, .. })) => Value { e: E::String(val), - start: start, - end: end, + start, + end, }, Some((Span { start, end }, Token::Keylike("true"))) => Value { e: E::Boolean(true), - start: start, - end: end, + start, + end, }, Some((Span { start, end }, Token::Keylike("false"))) => Value { e: E::Boolean(false), - start: start, - end: end, + start, + end, }, Some((span, Token::Keylike(key))) => self.number_or_date(span, key)?, Some((span, Token::Plus)) => self.number_leading_plus(span)?, Some((Span { start, .. }, Token::LeftBrace)) => { self.inline_table().map(|(Span { end, .. }, table)| Value { e: E::InlineTable(table), - start: start, - end: end, + start, + end, })? } Some((Span { start, .. }, Token::LeftBracket)) => { self.array().map(|(Span { end, .. }, array)| Value { e: E::Array(array), - start: start, - end: end, + start, + end, })? } Some(token) => { @@ -1226,15 +1226,15 @@ impl<'a> Deserializer<'a> { self.datetime(span, s, false) .map(|(Span { start, end }, d)| Value { e: E::Datetime(d), - start: start, - end: end, + start, + end, }) } else if self.eat(Token::Colon)? { self.datetime(span, s, true) .map(|(Span { start, end }, d)| Value { e: E::Datetime(d), - start: start, - end: end, + start, + end, }) } else { self.number(span, s) @@ -1282,8 +1282,8 @@ impl<'a> Deserializer<'a> { Ok(( Value { e: E::DottedTable(table.values.unwrap_or_else(Vec::new)), - start: start, - end: end, + start, + end, }, Some(header.clone()), )) @@ -1296,8 +1296,8 @@ impl<'a> Deserializer<'a> { fn number(&mut self, Span { start, end }: Span, s: &'a str) -> Result<Value<'a>, Error> { let to_integer = |f| Value { e: E::Integer(f), - start: start, - end: end, + start, + end, }; if s.starts_with("0x") { self.integer(&s[2..], 16).map(to_integer) @@ -1308,8 +1308,8 @@ impl<'a> Deserializer<'a> { } else if s.contains('e') || s.contains('E') { self.float(s, None).map(|f| Value { e: E::Float(f), - start: start, - end: end, + start, + end, }) } else if self.eat(Token::Period)? { let at = self.tokens.current(); @@ -1317,8 +1317,8 @@ impl<'a> Deserializer<'a> { Some((Span { start, end }, Token::Keylike(after))) => { self.float(s, Some(after)).map(|f| Value { e: E::Float(f), - start: start, - end: end, + start, + end, }) } _ => Err(self.error(at, ErrorKind::NumberInvalid)), @@ -1326,26 +1326,26 @@ impl<'a> Deserializer<'a> { } else if s == "inf" { Ok(Value { e: E::Float(f64::INFINITY), - start: start, - end: end, + start, + end, }) } else if s == "-inf" { Ok(Value { e: E::Float(f64::NEG_INFINITY), - start: start, - end: end, + start, + end, }) } else if s == "nan" { Ok(Value { e: E::Float(f64::NAN), - start: start, - end: end, + start, + end, }) } else if s == "-nan" { Ok(Value { e: E::Float(-f64::NAN), - start: start, - end: end, + start, + end, }) } else { self.integer(s, 10).map(to_integer) @@ -1355,13 +1355,7 @@ impl<'a> Deserializer<'a> { fn number_leading_plus(&mut self, Span { start, .. }: Span) -> Result<Value<'a>, Error> { let start_token = self.tokens.current(); match self.next()? { - Some((Span { end, .. }, Token::Keylike(s))) => self.number( - Span { - start: start, - end: end, - }, - s, - ), + Some((Span { end, .. }, Token::Keylike(s))) => self.number(Span { start, end }, s), _ => Err(self.error(start_token, ErrorKind::NumberInvalid)), } } @@ -1752,13 +1746,7 @@ impl<'a> Deserializer<'a> { at, expected, found, - } => self.error( - at, - ErrorKind::Wanted { - expected: expected, - found: found, - }, - ), + } => self.error(at, ErrorKind::Wanted { expected, found }), TokenError::EmptyTableKey(at) => self.error(at, ErrorKind::EmptyTableKey), TokenError::MultilineStringKey(at) => self.error(at, ErrorKind::MultilineStringKey), } @@ -1799,7 +1787,7 @@ impl Error { fn from_kind(at: Option<usize>, kind: ErrorKind) -> Error { Error { inner: Box::new(ErrorInner { - kind: kind, + kind, line: None, col: 0, at, @@ -1829,7 +1817,7 @@ impl Error { 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 F: FnOnce() -> Option<usize>, { @@ -1840,7 +1828,7 @@ impl Error { } } - fn fix_linecol<F>(&mut self, f: F) -> () + fn fix_linecol<F>(&mut self, f: F) where F: FnOnce(usize) -> (usize, usize), { @@ -1854,7 +1842,7 @@ impl Error { impl std::convert::From<Error> for std::io::Error { 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 +1983,9 @@ impl<'a> Header<'a> { fn new(tokens: Tokenizer<'a>, array: bool, require_newline_after_table: bool) -> Header<'a> { Header { first: true, - array: array, - tokens: tokens, - require_newline_after_table: require_newline_after_table, + array, + tokens, + require_newline_after_table, } } @@ -2015,10 +2003,8 @@ impl<'a> Header<'a> { } self.tokens.eat_whitespace()?; - if self.require_newline_after_table { - if !self.tokens.eat_comment()? { - self.tokens.eat_newline_or_eof()?; - } + if self.require_newline_after_table && !self.tokens.eat_comment()? { + self.tokens.eat_newline_or_eof()?; } Ok(None) } @@ -150,8 +150,8 @@ impl Map<String, Value> { use std::collections::btree_map::Entry as EntryImpl; match self.map.entry(key.into()) { - EntryImpl::Vacant(vacant) => Entry::Vacant(VacantEntry { vacant: vacant }), - EntryImpl::Occupied(occupied) => Entry::Occupied(OccupiedEntry { occupied: occupied }), + EntryImpl::Vacant(vacant) => Entry::Vacant(VacantEntry { vacant }), + EntryImpl::Occupied(occupied) => Entry::Occupied(OccupiedEntry { occupied }), } } @@ -247,7 +247,7 @@ impl<'a> Serializer<'a> { /// will be present in `dst`. pub fn new(dst: &'a mut String) -> Serializer<'a> { Serializer { - dst: dst, + dst, state: State::End, settings: Rc::new(Settings::default()), } @@ -263,7 +263,7 @@ impl<'a> Serializer<'a> { /// have a trailing comma. See `Serializer::pretty_array` pub fn pretty(dst: &'a mut String) -> Serializer<'a> { Serializer { - dst: dst, + dst, state: State::End, settings: Rc::new(Settings { array: Some(ArraySettings::pretty()), @@ -331,13 +331,12 @@ 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 { - s.literal = value; - false - } else { - true - }; + let use_default = if let Some(ref mut s) = Rc::get_mut(&mut self.settings).unwrap().string { + s.literal = value; + false + } else { + true + }; if use_default { let mut string = StringSettings::pretty(); @@ -387,13 +386,12 @@ 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 { - a.indent = value; - false - } else { - true - }; + let use_default = if let Some(ref mut a) = Rc::get_mut(&mut self.settings).unwrap().array { + a.indent = value; + false + } else { + true + }; if use_default { let mut array = ArraySettings::pretty(); @@ -407,13 +405,12 @@ 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 { - a.trailing_comma = value; - false - } else { - true - }; + let use_default = if let Some(ref mut a) = Rc::get_mut(&mut self.settings).unwrap().array { + a.trailing_comma = value; + false + } else { + true + }; if use_default { let mut array = ArraySettings::pretty(); @@ -610,7 +607,7 @@ impl<'a> Serializer<'a> { (&Some(StringSettings { literal: false, .. }), Repr::Literal(_, ty)) => { Repr::Std(ty) } - (_, r @ _) => r, + (_, r) => r, } } else { Repr::Std(Type::OnelineSingle) @@ -902,7 +899,7 @@ impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> { ser: self, first: Cell::new(true), type_: Cell::new(None), - len: len, + len, }) } @@ -1099,10 +1096,10 @@ impl<'a, 'b> ser::SerializeMap for SerializeTable<'a, 'b> { let res = value.serialize(&mut Serializer { dst: &mut *ser.dst, state: State::Table { - key: key, + key, parent: &ser.state, - first: first, - table_emitted: table_emitted, + first, + table_emitted, }, settings: ser.settings.clone(), }); @@ -1155,10 +1152,10 @@ impl<'a, 'b> ser::SerializeStruct for SerializeTable<'a, 'b> { let res = value.serialize(&mut Serializer { dst: &mut *ser.dst, state: State::Table { - key: key, + key, parent: &ser.state, - first: first, - table_emitted: table_emitted, + first, + table_emitted, }, settings: ser.settings.clone(), }); diff --git a/src/spanned.rs b/src/spanned.rs index d9fec3a..f2fc180 100644 --- a/src/spanned.rs +++ b/src/spanned.rs @@ -2,13 +2,13 @@ use serde::{de, ser}; use std::fmt; #[doc(hidden)] -pub const NAME: &'static str = "$__toml_private_Spanned"; +pub const NAME: &str = "$__toml_private_Spanned"; #[doc(hidden)] -pub const START: &'static str = "$__toml_private_start"; +pub const START: &str = "$__toml_private_start"; #[doc(hidden)] -pub const END: &'static str = "$__toml_private_end"; +pub const END: &str = "$__toml_private_end"; #[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. /// @@ -116,17 +116,13 @@ where let value: T = visitor.next_value()?; - Ok(Spanned { - start: start, - end: end, - value: value, - }) + Ok(Spanned { start, end, value }) } } 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) } } diff --git a/src/tokens.rs b/src/tokens.rs index f9d3eb4..91824c5 100644 --- a/src/tokens.rs +++ b/src/tokens.rs @@ -84,7 +84,7 @@ enum MaybeString { impl<'a> Tokenizer<'a> { pub fn new(input: &'a str) -> Tokenizer<'a> { let mut t = Tokenizer { - input: input, + input, chars: CrlfFold { chars: input.char_indices(), }, @@ -266,7 +266,7 @@ impl<'a> Tokenizer<'a> { .clone() .next() .map(|i| i.0) - .unwrap_or(self.input.len()) + .unwrap_or_else(|| self.input.len()) } pub fn input(&self) -> &'a str { @@ -349,7 +349,7 @@ impl<'a> Tokenizer<'a> { return Ok(String { src: &self.input[start..self.current()], val: val.into_cow(&self.input[..i]), - multiline: multiline, + multiline, }); } Some((i, c)) => new_ch(self, &mut val, multiline, i, c)?, @@ -463,10 +463,7 @@ impl<'a> Tokenizer<'a> { .peek_one() .map(|t| t.0) .unwrap_or_else(|| self.input.len()); - Span { - start: start, - end: end, - } + Span { start, end } } /// Peek one char without consuming it. @@ -642,7 +639,7 @@ mod tests { err(r#""\U00""#, Error::InvalidHexEscape(5, '"')); err(r#""\U00"#, Error::UnterminatedString(0)); err(r#""\uD800"#, Error::InvalidEscapeValue(2, 0xd800)); - err(r#""\UFFFFFFFF"#, Error::InvalidEscapeValue(2, 0xffffffff)); + err(r#""\UFFFFFFFF"#, Error::InvalidEscapeValue(2, 0xffff_ffff)); } #[test] diff --git a/src/value.rs b/src/value.rs index 6f940b4..38dfe1f 100644 --- a/src/value.rs +++ b/src/value.rs @@ -749,7 +749,7 @@ impl ser::Serializer for Serializer { } 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> { |