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