2018-05-06 20:47:09 -05:00
|
|
|
use serde::{de, ser};
|
2019-10-25 14:05:31 -05:00
|
|
|
use std::borrow::Borrow;
|
|
|
|
use std::cmp::Ordering;
|
2018-05-06 20:47:09 -05:00
|
|
|
use std::fmt;
|
2019-10-25 14:05:31 -05:00
|
|
|
use std::hash::{Hash, Hasher};
|
2018-05-05 16:41:57 -05:00
|
|
|
|
2019-09-09 13:04:47 -05:00
|
|
|
pub(crate) const NAME: &str = "$__toml_private_Spanned";
|
|
|
|
pub(crate) const START: &str = "$__toml_private_start";
|
|
|
|
pub(crate) const END: &str = "$__toml_private_end";
|
|
|
|
pub(crate) const VALUE: &str = "$__toml_private_value";
|
2018-05-06 20:47:09 -05:00
|
|
|
|
|
|
|
/// A spanned value, indicating the range at which it is defined in the source.
|
2019-08-09 13:47:52 -05:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use serde_derive::Deserialize;
|
|
|
|
/// use toml::Spanned;
|
|
|
|
///
|
|
|
|
/// #[derive(Deserialize)]
|
|
|
|
/// struct Value {
|
|
|
|
/// s: Spanned<String>,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let t = "s = \"value\"\n";
|
|
|
|
///
|
|
|
|
/// let u: Value = toml::from_str(t).unwrap();
|
|
|
|
///
|
|
|
|
/// 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"));
|
|
|
|
/// }
|
|
|
|
/// ```
|
2019-10-25 14:05:31 -05:00
|
|
|
#[derive(Clone, 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.
|
2019-10-03 10:29:50 -05:00
|
|
|
pub fn get_mut(&mut self) -> &mut T {
|
|
|
|
&mut self.value
|
2018-05-09 14:26:59 -05:00
|
|
|
}
|
2018-05-06 20:47:09 -05:00
|
|
|
}
|
|
|
|
|
2019-10-25 14:05:31 -05:00
|
|
|
impl Borrow<str> for Spanned<String> {
|
|
|
|
fn borrow(&self) -> &str {
|
|
|
|
&self.get_ref()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: PartialEq> PartialEq for Spanned<T> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.value.eq(&other.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Eq> Eq for Spanned<T> {}
|
|
|
|
|
|
|
|
impl<T: Hash> Hash for Spanned<T> {
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
self.value.hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: PartialOrd> PartialOrd for Spanned<T> {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
self.value.partial_cmp(&other.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Ord> Ord for Spanned<T> {
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
self.value.cmp(&other.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>;
|
|
|
|
|
2019-05-08 19:37:38 -05:00
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-05-06 20:47:09 -05:00
|
|
|
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()?;
|
|
|
|
|
2019-08-14 21:52:24 -05:00
|
|
|
Ok(Spanned { start, end, value })
|
2018-05-06 20:47:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let visitor = SpannedVisitor(::std::marker::PhantomData);
|
|
|
|
|
2019-08-14 21:52:24 -05:00
|
|
|
static FIELDS: [&str; 3] = [START, END, VALUE];
|
2018-05-06 20:47:09 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|