2018-05-05 16:41:57 -05:00
|
|
|
//! ```
|
|
|
|
//! #[macro_use]
|
|
|
|
//! extern crate serde_derive;
|
|
|
|
//!
|
|
|
|
//! extern crate toml;
|
2018-05-09 14:26:59 -05:00
|
|
|
//! use toml::Spanned;
|
2018-05-05 16:41:57 -05:00
|
|
|
//!
|
|
|
|
//! #[derive(Deserialize)]
|
2018-05-06 20:47:09 -05:00
|
|
|
//! struct Value {
|
2018-05-05 16:41:57 -05:00
|
|
|
//! s: Spanned<String>,
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! fn main() {
|
2018-05-06 20:47:09 -05:00
|
|
|
//! let t = "s = \"value\"\n";
|
2018-05-05 16:41:57 -05:00
|
|
|
//!
|
2018-05-06 20:47:09 -05:00
|
|
|
//! let u: Value = toml::from_str(t).unwrap();
|
2018-05-05 16:41:57 -05:00
|
|
|
//!
|
2018-05-09 14:26:59 -05:00
|
|
|
//! assert_eq!(u.s.start(), 4);
|
|
|
|
//! assert_eq!(u.s.end(), 11);
|
|
|
|
//! assert_eq!(u.s.get_ref(), "value");
|
|
|
|
//! assert_eq!(u.s.into_inner(), String::from("value"));
|
2018-05-05 16:41:57 -05:00
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
|
2018-05-06 20:47:09 -05:00
|
|
|
use serde::{de, ser};
|
|
|
|
use std::fmt;
|
2018-05-05 16:41:57 -05:00
|
|
|
|
|
|
|
#[doc(hidden)]
|
2018-05-06 20:47:09 -05:00
|
|
|
pub const NAME: &'static str = "$__toml_private_Spanned";
|
2018-05-05 16:41:57 -05:00
|
|
|
#[doc(hidden)]
|
2018-05-06 20:47:09 -05:00
|
|
|
pub const START: &'static str = "$__toml_private_start";
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub const END: &'static str = "$__toml_private_end";
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub const VALUE: &'static str = "$__toml_private_value";
|
|
|
|
|
|
|
|
/// A spanned value, indicating the range at which it is defined in the source.
|
|
|
|
#[derive(Debug)]
|
2018-05-05 16:41:57 -05:00
|
|
|
pub struct Spanned<T> {
|
2018-05-06 20:47:09 -05:00
|
|
|
/// The start range.
|
2018-05-09 14:26:59 -05:00
|
|
|
start: usize,
|
2018-05-06 20:47:09 -05:00
|
|
|
/// The end range (exclusive).
|
2018-05-09 14:26:59 -05:00
|
|
|
end: usize,
|
2018-05-06 20:47:09 -05:00
|
|
|
/// The spanned value.
|
2018-05-09 14:26:59 -05:00
|
|
|
value: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Spanned<T> {
|
|
|
|
/// Access the start of the span of the contained value.
|
|
|
|
pub fn start(&self) -> usize {
|
|
|
|
self.start
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Access the end of the span of the contained value.
|
|
|
|
pub fn end(&self) -> usize {
|
|
|
|
self.end
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the span of the contained value.
|
|
|
|
pub fn span(&self) -> (usize, usize) {
|
|
|
|
(self.start, self.end)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Consumes the spanned value and returns the contained value.
|
|
|
|
pub fn into_inner(self) -> T {
|
|
|
|
self.value
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a reference to the contained value.
|
|
|
|
pub fn get_ref(&self) -> &T {
|
|
|
|
&self.value
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a mutable reference to the contained value.
|
|
|
|
pub fn get_mut(&self) -> &T {
|
|
|
|
&self.value
|
|
|
|
}
|
2018-05-06 20:47:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de, T> de::Deserialize<'de> for Spanned<T>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
T: de::Deserialize<'de>,
|
2018-05-06 20:47:09 -05:00
|
|
|
{
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Spanned<T>, D::Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
D: de::Deserializer<'de>,
|
2018-05-06 20:47:09 -05:00
|
|
|
{
|
|
|
|
struct SpannedVisitor<T>(::std::marker::PhantomData<T>);
|
|
|
|
|
|
|
|
impl<'de, T> de::Visitor<'de> for SpannedVisitor<T>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
T: de::Deserialize<'de>,
|
2018-05-06 20:47:09 -05:00
|
|
|
{
|
|
|
|
type Value = Spanned<T>;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str("a TOML spanned")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_map<V>(self, mut visitor: V) -> Result<Spanned<T>, V::Error>
|
2018-12-17 19:45:35 -06:00
|
|
|
where
|
|
|
|
V: de::MapAccess<'de>,
|
2018-05-06 20:47:09 -05:00
|
|
|
{
|
2018-05-06 21:56:25 -05:00
|
|
|
if visitor.next_key()? != Some(START) {
|
2018-12-17 19:45:35 -06:00
|
|
|
return Err(de::Error::custom("spanned start key not found"));
|
2018-05-06 20:47:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let start: usize = visitor.next_value()?;
|
|
|
|
|
2018-05-06 21:56:25 -05:00
|
|
|
if visitor.next_key()? != Some(END) {
|
2018-12-17 19:45:35 -06:00
|
|
|
return Err(de::Error::custom("spanned end key not found"));
|
2018-05-06 20:47:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let end: usize = visitor.next_value()?;
|
|
|
|
|
2018-05-06 21:56:25 -05:00
|
|
|
if visitor.next_key()? != Some(VALUE) {
|
2018-12-17 19:45:35 -06:00
|
|
|
return Err(de::Error::custom("spanned value key not found"));
|
2018-05-06 20:47:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let value: T = visitor.next_value()?;
|
|
|
|
|
|
|
|
Ok(Spanned {
|
|
|
|
start: start,
|
|
|
|
end: end,
|
2018-12-17 19:45:35 -06:00
|
|
|
value: value,
|
2018-05-06 20:47:09 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let visitor = SpannedVisitor(::std::marker::PhantomData);
|
|
|
|
|
|
|
|
static FIELDS: [&'static str; 3] = [START, END, VALUE];
|
|
|
|
deserializer.deserialize_struct(NAME, &FIELDS, visitor)
|
|
|
|
}
|
2018-05-05 16:41:57 -05:00
|
|
|
}
|
|
|
|
|
2018-05-06 20:47:09 -05:00
|
|
|
impl<T: ser::Serialize> ser::Serialize for Spanned<T> {
|
2018-05-05 16:41:57 -05:00
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
2018-05-06 20:47:09 -05:00
|
|
|
S: ser::Serializer,
|
2018-05-05 16:41:57 -05:00
|
|
|
{
|
|
|
|
self.value.serialize(serializer)
|
|
|
|
}
|
|
|
|
}
|