toml/
spanned.rs

1use serde::{de, ser};
2use std::borrow::Borrow;
3use std::cmp::Ordering;
4use std::fmt;
5use std::hash::{Hash, Hasher};
6
7pub(crate) const NAME: &str = "$__toml_private_Spanned";
8pub(crate) const START: &str = "$__toml_private_start";
9pub(crate) const END: &str = "$__toml_private_end";
10pub(crate) const VALUE: &str = "$__toml_private_value";
11
12/// A spanned value, indicating the range at which it is defined in the source.
13///
14/// ```
15/// use serde_derive::Deserialize;
16/// use toml::Spanned;
17///
18/// #[derive(Deserialize)]
19/// struct Value {
20///     s: Spanned<String>,
21/// }
22///
23/// let t = "s = \"value\"\n";
24///
25/// let u: Value = toml::from_str(t).unwrap();
26///
27/// assert_eq!(u.s.start(), 4);
28/// assert_eq!(u.s.end(), 11);
29/// assert_eq!(u.s.get_ref(), "value");
30/// assert_eq!(u.s.into_inner(), String::from("value"));
31/// ```
32#[derive(Clone, Debug)]
33pub struct Spanned<T> {
34    /// The start range.
35    start: usize,
36    /// The end range (exclusive).
37    end: usize,
38    /// The spanned value.
39    value: T,
40}
41
42impl<T> Spanned<T> {
43    /// Access the start of the span of the contained value.
44    pub fn start(&self) -> usize {
45        self.start
46    }
47
48    /// Access the end of the span of the contained value.
49    pub fn end(&self) -> usize {
50        self.end
51    }
52
53    /// Get the span of the contained value.
54    pub fn span(&self) -> (usize, usize) {
55        (self.start, self.end)
56    }
57
58    /// Consumes the spanned value and returns the contained value.
59    pub fn into_inner(self) -> T {
60        self.value
61    }
62
63    /// Returns a reference to the contained value.
64    pub fn get_ref(&self) -> &T {
65        &self.value
66    }
67
68    /// Returns a mutable reference to the contained value.
69    pub fn get_mut(&mut self) -> &mut T {
70        &mut self.value
71    }
72}
73
74impl Borrow<str> for Spanned<String> {
75    fn borrow(&self) -> &str {
76        self.get_ref()
77    }
78}
79
80impl<T: PartialEq> PartialEq for Spanned<T> {
81    fn eq(&self, other: &Self) -> bool {
82        self.value.eq(&other.value)
83    }
84}
85
86impl<T: Eq> Eq for Spanned<T> {}
87
88impl<T: Hash> Hash for Spanned<T> {
89    fn hash<H: Hasher>(&self, state: &mut H) {
90        self.value.hash(state);
91    }
92}
93
94impl<T: PartialOrd> PartialOrd for Spanned<T> {
95    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
96        self.value.partial_cmp(&other.value)
97    }
98}
99
100impl<T: Ord> Ord for Spanned<T> {
101    fn cmp(&self, other: &Self) -> Ordering {
102        self.value.cmp(&other.value)
103    }
104}
105
106impl<'de, T> de::Deserialize<'de> for Spanned<T>
107where
108    T: de::Deserialize<'de>,
109{
110    fn deserialize<D>(deserializer: D) -> Result<Spanned<T>, D::Error>
111    where
112        D: de::Deserializer<'de>,
113    {
114        struct SpannedVisitor<T>(::std::marker::PhantomData<T>);
115
116        impl<'de, T> de::Visitor<'de> for SpannedVisitor<T>
117        where
118            T: de::Deserialize<'de>,
119        {
120            type Value = Spanned<T>;
121
122            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
123                formatter.write_str("a TOML spanned")
124            }
125
126            fn visit_map<V>(self, mut visitor: V) -> Result<Spanned<T>, V::Error>
127            where
128                V: de::MapAccess<'de>,
129            {
130                if visitor.next_key()? != Some(START) {
131                    return Err(de::Error::custom("spanned start key not found"));
132                }
133
134                let start: usize = visitor.next_value()?;
135
136                if visitor.next_key()? != Some(END) {
137                    return Err(de::Error::custom("spanned end key not found"));
138                }
139
140                let end: usize = visitor.next_value()?;
141
142                if visitor.next_key()? != Some(VALUE) {
143                    return Err(de::Error::custom("spanned value key not found"));
144                }
145
146                let value: T = visitor.next_value()?;
147
148                Ok(Spanned { start, end, value })
149            }
150        }
151
152        let visitor = SpannedVisitor(::std::marker::PhantomData);
153
154        static FIELDS: [&str; 3] = [START, END, VALUE];
155        deserializer.deserialize_struct(NAME, &FIELDS, visitor)
156    }
157}
158
159impl<T: ser::Serialize> ser::Serialize for Spanned<T> {
160    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
161    where
162        S: ser::Serializer,
163    {
164        self.value.serialize(serializer)
165    }
166}