Use BorrowedStrDeserializer instead of generated key deserializers
This commit is contained in:
parent
09c8957ae7
commit
8a9ae07de5
|
@ -20,7 +20,6 @@ travis-ci = { repository = "alexcrichton/toml-rs" }
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serde = "1.0"
|
serde = "1.0"
|
||||||
serde_derive = "1.0"
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
|
|
@ -12,6 +12,7 @@ use std::vec;
|
||||||
|
|
||||||
use serde::de;
|
use serde::de;
|
||||||
use serde::de::IntoDeserializer;
|
use serde::de::IntoDeserializer;
|
||||||
|
use serde::de::value::BorrowedStrDeserializer;
|
||||||
|
|
||||||
use tokens::{Tokenizer, Token, Error as TokenError, Span};
|
use tokens::{Tokenizer, Token, Error as TokenError, Span};
|
||||||
use datetime;
|
use datetime;
|
||||||
|
@ -620,11 +621,11 @@ impl<'de> de::MapAccess<'de> for SpannedDeserializer<'de> {
|
||||||
K: de::DeserializeSeed<'de>,
|
K: de::DeserializeSeed<'de>,
|
||||||
{
|
{
|
||||||
if self.start.is_some() {
|
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() {
|
} 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() {
|
} else if self.value.is_some() {
|
||||||
seed.deserialize(spanned::VALUE.into_deserializer()).map(Some)
|
seed.deserialize(BorrowedStrDeserializer::new(spanned::VALUE)).map(Some)
|
||||||
} else {
|
} else {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
|
|
@ -153,8 +153,6 @@
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
#[macro_use]
|
|
||||||
extern crate serde_derive;
|
|
||||||
|
|
||||||
pub mod value;
|
pub mod value;
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
|
|
|
@ -32,43 +32,6 @@ pub const END: &'static str = "$__toml_private_end";
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub const VALUE: &'static str = "$__toml_private_value";
|
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.
|
/// A spanned value, indicating the range at which it is defined in the source.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Spanned<T> {
|
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>
|
fn visit_map<V>(self, mut visitor: V) -> Result<Spanned<T>, V::Error>
|
||||||
where V: de::MapAccess<'de>
|
where V: de::MapAccess<'de>
|
||||||
{
|
{
|
||||||
let start = visitor.next_key::<StartKey>()?;
|
if visitor.next_key()? != Some(START) {
|
||||||
|
|
||||||
if start.is_none() {
|
|
||||||
return Err(de::Error::custom("spanned start key not found"))
|
return Err(de::Error::custom("spanned start key not found"))
|
||||||
}
|
}
|
||||||
|
|
||||||
let start: usize = visitor.next_value()?;
|
let start: usize = visitor.next_value()?;
|
||||||
|
|
||||||
let end = visitor.next_key::<EndKey>()?;
|
if visitor.next_key()? != Some(END) {
|
||||||
|
|
||||||
if end.is_none() {
|
|
||||||
return Err(de::Error::custom("spanned end key not found"))
|
return Err(de::Error::custom("spanned end key not found"))
|
||||||
}
|
}
|
||||||
|
|
||||||
let end: usize = visitor.next_value()?;
|
let end: usize = visitor.next_value()?;
|
||||||
|
|
||||||
let value = visitor.next_key::<ValueKey>()?;
|
if visitor.next_key()? != Some(VALUE) {
|
||||||
|
|
||||||
if value.is_none() {
|
|
||||||
return Err(de::Error::custom("spanned value key not found"))
|
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);
|
let visitor = SpannedVisitor(::std::marker::PhantomData);
|
||||||
|
|
||||||
static FIELDS: [&'static str; 3] = [START, END, VALUE];
|
static FIELDS: [&'static str; 3] = [START, END, VALUE];
|
||||||
|
|
Loading…
Reference in a new issue