From 8b88c96cfc0a99ec45affc69582e7cca7e7860b3 Mon Sep 17 00:00:00 2001 From: Nick Hackman Date: Wed, 14 Aug 2019 22:52:24 -0400 Subject: [PATCH 1/4] Removed all warnings besides two Majority of warnings removed via RustFix Two remain one being a long return type and another being a name suggestion from Clippy `to_owned` for MaybeString --- src/datetime.rs | 22 ++++---- src/de.rs | 134 ++++++++++++++++++++++-------------------------- src/map.rs | 4 +- src/ser.rs | 59 ++++++++++----------- src/spanned.rs | 16 +++--- src/tokens.rs | 13 ++--- 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) } } diff --git a/src/de.rs b/src/de.rs index e71cd0c..69331ab 100644 --- a/src/de.rs +++ b/src/de.rs @@ -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, 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, 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, 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(&mut self, f: F) -> () + fn fix_offset(&mut self, f: F) where F: FnOnce() -> Option, { @@ -1840,7 +1828,7 @@ impl Error { } } - fn fix_linecol(&mut self, f: F) -> () + fn fix_linecol(&mut self, f: F) where F: FnOnce(usize) -> (usize, usize), { @@ -1854,7 +1842,7 @@ impl Error { impl std::convert::From 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) } diff --git a/src/map.rs b/src/map.rs index b6e00b0..08c2cf3 100644 --- a/src/map.rs +++ b/src/map.rs @@ -150,8 +150,8 @@ impl Map { 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 }), } } diff --git a/src/ser.rs b/src/ser.rs index fe4674e..05918e8 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -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 { - Ok(Value::Integer(value.into())) + Ok(Value::Integer(value)) } fn serialize_u8(self, value: u8) -> Result { From 6699b5be03bdc0aaca4e0e9c0d5ef941f55d6b7f Mon Sep 17 00:00:00 2001 From: Nick Hackman Date: Sun, 18 Aug 2019 20:23:40 -0400 Subject: [PATCH 2/4] Potential Solution to drop clippy errors Calling the write!, the result that it returns doesn't matter or wasn't deemed as important and was dropped before. This was just removed and then the unused Result is ignored via clippy attribute. This is a plausible solution to get past the Clippy Error, but others may be more ideal. --- src/ser.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/ser.rs b/src/ser.rs index 05918e8..6a537e2 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -25,6 +25,8 @@ //! # fn main() {} //! ``` +#![allow(unused_must_use)] + use std::cell::Cell; use std::error; use std::fmt::{self, Write}; @@ -422,7 +424,7 @@ impl<'a> Serializer<'a> { fn display(&mut self, t: T, type_: &'static str) -> Result<(), Error> { self.emit_key(type_)?; - drop(write!(self.dst, "{}", t)); + write!(self.dst, "{}", t); if let State::Table { .. } = self.state { self.dst.push_str("\n"); } @@ -515,7 +517,7 @@ impl<'a> Serializer<'a> { _ => false, }); if ok { - drop(write!(self.dst, "{}", key)); + write!(self.dst, "{}", key); } else { self.emit_str(key, true)?; } @@ -647,7 +649,9 @@ impl<'a> Serializer<'a> { '\u{d}' => self.dst.push_str("\\r"), '\u{22}' => 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); + } ch => self.dst.push(ch), } } @@ -750,15 +754,15 @@ macro_rules! serialize_float { ($this:expr, $v:expr) => {{ $this.emit_key("float")?; if ($v.is_nan() || $v == 0.0) && $v.is_sign_negative() { - drop(write!($this.dst, "-")); + write!($this.dst, "-"); } if $v.is_nan() { - drop(write!($this.dst, "nan")); + write!($this.dst, "nan"); } else { - drop(write!($this.dst, "{}", $v)); + write!($this.dst, "{}", $v); } if $v % 1.0 == 0.0 { - drop(write!($this.dst, ".0")); + write!($this.dst, ".0"); } if let State::Table { .. } = $this.state { $this.dst.push_str("\n"); From 9f25cfa17b65ea8e8c91e453f7f156cecbea525c Mon Sep 17 00:00:00 2001 From: Nick Hackman Date: Sun, 18 Aug 2019 21:07:23 -0400 Subject: [PATCH 3/4] Fixed Clippy warning of long return type Created an alias named TablePair being a (Cow<'a, str>, Value<'a>) --- src/de.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/de.rs b/src/de.rs index 69331ab..4c3acc5 100644 --- a/src/de.rs +++ b/src/de.rs @@ -20,6 +20,9 @@ use crate::datetime; use crate::spanned; 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. /// /// This function will attempt to interpret `bytes` as UTF-8 data and then @@ -292,14 +295,14 @@ impl<'de, 'b> de::Deserializer<'de> for &'b mut Deserializer<'de> { struct Table<'a> { at: usize, header: Vec>, - values: Option, Value<'a>)>>, + values: Option>>, array: bool, } #[doc(hidden)] pub struct MapVisitor<'de, 'b> { - values: vec::IntoIter<(Cow<'de, str>, Value<'de>)>, - next_value: Option<(Cow<'de, str>, Value<'de>)>, + values: vec::IntoIter>, + next_value: Option>, depth: usize, cur: usize, cur_parent: usize, @@ -855,7 +858,7 @@ impl<'de> de::EnumAccess<'de> for DottedTableDeserializer<'de> { } struct InlineTableDeserializer<'a> { - values: vec::IntoIter<(Cow<'a, str>, Value<'a>)>, + values: vec::IntoIter>, next_value: Option>, } @@ -1539,7 +1542,7 @@ impl<'a> Deserializer<'a> { // TODO(#140): shouldn't buffer up this entire table in memory, it'd be // 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>), Error> { let mut ret = Vec::new(); self.eat_whitespace()?; if let Some(span) = self.eat_spanned(Token::RightBrace)? { @@ -1635,7 +1638,7 @@ impl<'a> Deserializer<'a> { &self, mut key_parts: Vec>, value: Value<'a>, - values: &mut Vec<(Cow<'a, str>, Value<'a>)>, + values: &mut Vec>, ) -> Result<(), Error> { let key = key_parts.remove(0); if key_parts.is_empty() { @@ -2026,8 +2029,8 @@ enum E<'a> { String(Cow<'a, str>), Datetime(&'a str), Array(Vec>), - InlineTable(Vec<(Cow<'a, str>, Value<'a>)>), - DottedTable(Vec<(Cow<'a, str>, Value<'a>)>), + InlineTable(Vec>), + DottedTable(Vec>), } impl<'a> E<'a> { From c18d474acfadefcbd85e2ec659ca7a288503a052 Mon Sep 17 00:00:00 2001 From: Nick Hackman Date: Tue, 20 Aug 2019 12:59:28 -0400 Subject: [PATCH 4/4] Propogate write! errors to ser::Error std::fmt::Errors are now converted to ser::Errors via ser::Error::custom --- src/ser.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/ser.rs b/src/ser.rs index 6a537e2..039c1f3 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -25,8 +25,6 @@ //! # fn main() {} //! ``` -#![allow(unused_must_use)] - use std::cell::Cell; use std::error; use std::fmt::{self, Write}; @@ -424,7 +422,7 @@ impl<'a> Serializer<'a> { fn display(&mut self, t: T, type_: &'static str) -> Result<(), Error> { self.emit_key(type_)?; - write!(self.dst, "{}", t); + write!(self.dst, "{}", t).map_err(ser::Error::custom)?; if let State::Table { .. } = self.state { self.dst.push_str("\n"); } @@ -517,7 +515,7 @@ impl<'a> Serializer<'a> { _ => false, }); if ok { - write!(self.dst, "{}", key); + write!(self.dst, "{}", key).map_err(ser::Error::custom)?; } else { self.emit_str(key, true)?; } @@ -650,7 +648,7 @@ impl<'a> Serializer<'a> { '\u{22}' => self.dst.push_str("\\\""), '\u{5c}' => self.dst.push_str("\\\\"), c if c < '\u{1f}' => { - write!(self.dst, "\\u{:04X}", ch as u32); + write!(self.dst, "\\u{:04X}", ch as u32).map_err(ser::Error::custom)?; } ch => self.dst.push(ch), } @@ -754,15 +752,15 @@ macro_rules! serialize_float { ($this:expr, $v:expr) => {{ $this.emit_key("float")?; if ($v.is_nan() || $v == 0.0) && $v.is_sign_negative() { - write!($this.dst, "-"); + write!($this.dst, "-").map_err(ser::Error::custom)?; } if $v.is_nan() { - write!($this.dst, "nan"); + write!($this.dst, "nan").map_err(ser::Error::custom)?; } else { - write!($this.dst, "{}", $v); + write!($this.dst, "{}", $v).map_err(ser::Error::custom)?; } if $v % 1.0 == 0.0 { - write!($this.dst, ".0"); + write!($this.dst, ".0").map_err(ser::Error::custom)?; } if let State::Table { .. } = $this.state { $this.dst.push_str("\n");