diff options
author | John-John Tedro <udoprog@tedro.se> | 2018-05-07 04:56:25 +0200 |
---|---|---|
committer | John-John Tedro <udoprog@tedro.se> | 2018-05-07 04:57:16 +0200 |
commit | 579cf491e6cca04b5e4cfe63f3132eed1f2a4e93 (patch) | |
tree | ce0ac3d1dd8e9024d592382657dc6417e768516a | |
parent | 6c067202e061910d5018c4f96e9a7595c4291a08 (diff) | |
download | milf-rs-579cf491e6cca04b5e4cfe63f3132eed1f2a4e93.tar.gz milf-rs-579cf491e6cca04b5e4cfe63f3132eed1f2a4e93.zip |
Use BorrowedStrDeserializer instead of generated key deserializers
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/de.rs | 7 | ||||
-rw-r--r-- | src/lib.rs | 2 | ||||
-rw-r--r-- | src/spanned.rs | 53 |
4 files changed, 7 insertions, 56 deletions
@@ -20,7 +20,6 @@ travis-ci = { repository = "alexcrichton/toml-rs" } [dependencies] serde = "1.0" -serde_derive = "1.0" [dev-dependencies] serde_json = "1.0" @@ -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) } @@ -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<D>(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<E>(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<T> { @@ -100,25 +63,19 @@ 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> { - let start = visitor.next_key::<StartKey>()?; - - 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::<EndKey>()?; - - 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::<ValueKey>()?; - - 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<T> } } - 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]; |