2 use std
::borrow
::Borrow
;
3 use std
::cmp
::Ordering
;
5 use std
::hash
::{Hash, Hasher}
;
7 pub(crate) const NAME
: &str = "$__toml_private_Spanned";
8 pub(crate) const START
: &str = "$__toml_private_start";
9 pub(crate) const END
: &str = "$__toml_private_end";
10 pub(crate) const VALUE
: &str = "$__toml_private_value";
12 /// A spanned value, indicating the range at which it is defined in the source.
15 /// use serde_derive::Deserialize;
16 /// use toml::Spanned;
18 /// #[derive(Deserialize)]
20 /// s: Spanned<String>,
24 /// let t = "s = \"value\"\n";
26 /// let u: Value = toml::from_str(t).unwrap();
28 /// assert_eq!(u.s.start(), 4);
29 /// assert_eq!(u.s.end(), 11);
30 /// assert_eq!(u.s.get_ref(), "value");
31 /// assert_eq!(u.s.into_inner(), String::from("value"));
34 #[derive(Clone, Debug)]
35 pub struct Spanned
<T
> {
38 /// The end range (exclusive).
40 /// The spanned value.
45 /// Access the start of the span of the contained value.
46 pub fn start(&self) -> usize {
50 /// Access the end of the span of the contained value.
51 pub fn end(&self) -> usize {
55 /// Get the span of the contained value.
56 pub fn span(&self) -> (usize, usize) {
57 (self.start
, self.end
)
60 /// Consumes the spanned value and returns the contained value.
61 pub fn into_inner(self) -> T
{
65 /// Returns a reference to the contained value.
66 pub fn get_ref(&self) -> &T
{
70 /// Returns a mutable reference to the contained value.
71 pub fn get_mut(&mut self) -> &mut T
{
76 impl Borrow
<str> for Spanned
<String
> {
77 fn borrow(&self) -> &str {
82 impl<T
: PartialEq
> PartialEq
for Spanned
<T
> {
83 fn eq(&self, other
: &Self) -> bool
{
84 self.value
.eq(&other
.value
)
88 impl<T
: Eq
> Eq
for Spanned
<T
> {}
90 impl<T
: Hash
> Hash
for Spanned
<T
> {
91 fn hash
<H
: Hasher
>(&self, state
: &mut H
) {
92 self.value
.hash(state
);
96 impl<T
: PartialOrd
> PartialOrd
for Spanned
<T
> {
97 fn partial_cmp(&self, other
: &Self) -> Option
<Ordering
> {
98 self.value
.partial_cmp(&other
.value
)
102 impl<T
: Ord
> Ord
for Spanned
<T
> {
103 fn cmp(&self, other
: &Self) -> Ordering
{
104 self.value
.cmp(&other
.value
)
108 impl<'de
, T
> de
::Deserialize
<'de
> for Spanned
<T
>
110 T
: de
::Deserialize
<'de
>,
112 fn deserialize
<D
>(deserializer
: D
) -> Result
<Spanned
<T
>, D
::Error
>
114 D
: de
::Deserializer
<'de
>,
116 struct SpannedVisitor
<T
>(::std
::marker
::PhantomData
<T
>);
118 impl<'de
, T
> de
::Visitor
<'de
> for SpannedVisitor
<T
>
120 T
: de
::Deserialize
<'de
>,
122 type Value
= Spanned
<T
>;
124 fn expecting(&self, formatter
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
125 formatter
.write_str("a TOML spanned")
128 fn visit_map
<V
>(self, mut visitor
: V
) -> Result
<Spanned
<T
>, V
::Error
>
130 V
: de
::MapAccess
<'de
>,
132 if visitor
.next_key()?
!= Some(START
) {
133 return Err(de
::Error
::custom("spanned start key not found"));
136 let start
: usize = visitor
.next_value()?
;
138 if visitor
.next_key()?
!= Some(END
) {
139 return Err(de
::Error
::custom("spanned end key not found"));
142 let end
: usize = visitor
.next_value()?
;
144 if visitor
.next_key()?
!= Some(VALUE
) {
145 return Err(de
::Error
::custom("spanned value key not found"));
148 let value
: T
= visitor
.next_value()?
;
150 Ok(Spanned { start, end, value }
)
154 let visitor
= SpannedVisitor(::std
::marker
::PhantomData
);
156 static FIELDS
: [&str; 3] = [START
, END
, VALUE
];
157 deserializer
.deserialize_struct(NAME
, &FIELDS
, visitor
)
161 impl<T
: ser
::Serialize
> ser
::Serialize
for Spanned
<T
> {
162 fn serialize
<S
>(&self, serializer
: S
) -> Result
<S
::Ok
, S
::Error
>
166 self.value
.serialize(serializer
)