From 8a9ae07de5cb1c7e4b761fe7cbd5cd3b70b9c9f1 Mon Sep 17 00:00:00 2001 From: John-John Tedro Date: Mon, 7 May 2018 04:56:25 +0200 Subject: [PATCH] Use BorrowedStrDeserializer instead of generated key deserializers --- Cargo.toml | 1 - src/de.rs | 7 ++++--- src/lib.rs | 2 -- src/spanned.rs | 53 +++----------------------------------------------- 4 files changed, 7 insertions(+), 56 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 13b5beb..3518d6e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,6 @@ travis-ci = { repository = "alexcrichton/toml-rs" } [dependencies] serde = "1.0" -serde_derive = "1.0" [dev-dependencies] serde_json = "1.0" diff --git a/src/de.rs b/src/de.rs index 4e2b4b7..5521e00 100644 --- a/src/de.rs +++ b/src/de.rs @@ -12,6 +12,7 @@ use std::vec; use serde::de; use serde::de::IntoDeserializer; +use serde::de::value::BorrowedStrDeserializer; use tokens::{Tokenizer, Token, Error as TokenError, Span}; use datetime; @@ -620,11 +621,11 @@ impl<'de> de::MapAccess<'de> for SpannedDeserializer<'de> { K: de::DeserializeSeed<'de>, { if self.start.is_some() { - seed.deserialize(spanned::START.into_deserializer()).map(Some) + seed.deserialize(BorrowedStrDeserializer::new(spanned::START)).map(Some) } else if self.end.is_some() { - seed.deserialize(spanned::END.into_deserializer()).map(Some) + seed.deserialize(BorrowedStrDeserializer::new(spanned::END)).map(Some) } else if self.value.is_some() { - seed.deserialize(spanned::VALUE.into_deserializer()).map(Some) + seed.deserialize(BorrowedStrDeserializer::new(spanned::VALUE)).map(Some) } else { Ok(None) } diff --git a/src/lib.rs b/src/lib.rs index f42512f..b90612c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -153,8 +153,6 @@ #[macro_use] extern crate serde; -#[macro_use] -extern crate serde_derive; pub mod value; #[doc(no_inline)] diff --git a/src/spanned.rs b/src/spanned.rs index 1673d66..8fbc529 100644 --- a/src/spanned.rs +++ b/src/spanned.rs @@ -32,43 +32,6 @@ pub const END: &'static str = "$__toml_private_end"; #[doc(hidden)] pub const VALUE: &'static str = "$__toml_private_value"; -macro_rules! key_deserialize { - ($ident:ident, $field:expr, $name:expr) => { - struct $ident; - - impl<'de> de::Deserialize<'de> for $ident { - fn deserialize(deserializer: D) -> Result<$ident, D::Error> - where D: de::Deserializer<'de> - { - struct FieldVisitor; - - impl<'de> de::Visitor<'de> for FieldVisitor { - type Value = (); - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("a valid spanned field") - } - - fn visit_str(self, s: &str) -> Result<(), E> - where E: de::Error - { - if s == $field { - Ok(()) - } else { - Err(de::Error::custom( - concat!("expected spanned field `", $name, "`"))) - } - } - } - - deserializer.deserialize_identifier(FieldVisitor)?; - Ok($ident) - } - } - } -} - - /// A spanned value, indicating the range at which it is defined in the source. #[derive(Debug)] pub struct Spanned { @@ -100,25 +63,19 @@ impl<'de, T> de::Deserialize<'de> for Spanned fn visit_map(self, mut visitor: V) -> Result, V::Error> where V: de::MapAccess<'de> { - let start = visitor.next_key::()?; - - if start.is_none() { + if visitor.next_key()? != Some(START) { return Err(de::Error::custom("spanned start key not found")) } let start: usize = visitor.next_value()?; - let end = visitor.next_key::()?; - - if end.is_none() { + if visitor.next_key()? != Some(END) { return Err(de::Error::custom("spanned end key not found")) } let end: usize = visitor.next_value()?; - let value = visitor.next_key::()?; - - if value.is_none() { + if visitor.next_key()? != Some(VALUE) { return Err(de::Error::custom("spanned value key not found")) } @@ -132,10 +89,6 @@ impl<'de, T> de::Deserialize<'de> for Spanned } } - key_deserialize!(StartKey, START, "start"); - key_deserialize!(EndKey, END, "end"); - key_deserialize!(ValueKey, VALUE, "value"); - let visitor = SpannedVisitor(::std::marker::PhantomData); static FIELDS: [&'static str; 3] = [START, END, VALUE];