Use BorrowedStrDeserializer instead of generated key deserializers

This commit is contained in:
John-John Tedro 2018-05-07 04:56:25 +02:00
parent 09c8957ae7
commit 8a9ae07de5
4 changed files with 7 additions and 56 deletions

View file

@ -20,7 +20,6 @@ travis-ci = { repository = "alexcrichton/toml-rs" }
[dependencies]
serde = "1.0"
serde_derive = "1.0"
[dev-dependencies]
serde_json = "1.0"

View file

@ -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)
}

View file

@ -153,8 +153,6 @@
#[macro_use]
extern crate serde;
#[macro_use]
extern crate serde_derive;
pub mod value;
#[doc(no_inline)]

View file

@ -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];