]>
git.proxmox.com Git - cargo.git/blob - vendor/toml/src/spanned.rs
2 //! use serde_derive::Deserialize;
5 //! #[derive(Deserialize)]
7 //! s: Spanned<String>,
11 //! let t = "s = \"value\"\n";
13 //! let u: Value = toml::from_str(t).unwrap();
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"));
26 pub const NAME
: &'
static str = "$__toml_private_Spanned";
28 pub const START
: &'
static str = "$__toml_private_start";
30 pub const END
: &'
static str = "$__toml_private_end";
32 pub const VALUE
: &'
static str = "$__toml_private_value";
34 /// A spanned value, indicating the range at which it is defined in the source.
36 pub struct Spanned
<T
> {
39 /// The end range (exclusive).
41 /// The spanned value.
46 /// Access the start of the span of the contained value.
47 pub fn start(&self) -> usize {
51 /// Access the end of the span of the contained value.
52 pub fn end(&self) -> usize {
56 /// Get the span of the contained value.
57 pub fn span(&self) -> (usize, usize) {
58 (self.start
, self.end
)
61 /// Consumes the spanned value and returns the contained value.
62 pub fn into_inner(self) -> T
{
66 /// Returns a reference to the contained value.
67 pub fn get_ref(&self) -> &T
{
71 /// Returns a mutable reference to the contained value.
72 pub fn get_mut(&self) -> &T
{
77 impl<'de
, T
> de
::Deserialize
<'de
> for Spanned
<T
>
79 T
: de
::Deserialize
<'de
>,
81 fn deserialize
<D
>(deserializer
: D
) -> Result
<Spanned
<T
>, D
::Error
>
83 D
: de
::Deserializer
<'de
>,
85 struct SpannedVisitor
<T
>(::std
::marker
::PhantomData
<T
>);
87 impl<'de
, T
> de
::Visitor
<'de
> for SpannedVisitor
<T
>
89 T
: de
::Deserialize
<'de
>,
91 type Value
= Spanned
<T
>;
93 fn expecting(&self, formatter
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
94 formatter
.write_str("a TOML spanned")
97 fn visit_map
<V
>(self, mut visitor
: V
) -> Result
<Spanned
<T
>, V
::Error
>
99 V
: de
::MapAccess
<'de
>,
101 if visitor
.next_key()?
!= Some(START
) {
102 return Err(de
::Error
::custom("spanned start key not found"));
105 let start
: usize = visitor
.next_value()?
;
107 if visitor
.next_key()?
!= Some(END
) {
108 return Err(de
::Error
::custom("spanned end key not found"));
111 let end
: usize = visitor
.next_value()?
;
113 if visitor
.next_key()?
!= Some(VALUE
) {
114 return Err(de
::Error
::custom("spanned value key not found"));
117 let value
: T
= visitor
.next_value()?
;
127 let visitor
= SpannedVisitor(::std
::marker
::PhantomData
);
129 static FIELDS
: [&'
static str; 3] = [START
, END
, VALUE
];
130 deserializer
.deserialize_struct(NAME
, &FIELDS
, visitor
)
134 impl<T
: ser
::Serialize
> ser
::Serialize
for Spanned
<T
> {
135 fn serialize
<S
>(&self, serializer
: S
) -> Result
<S
::Ok
, S
::Error
>
139 self.value
.serialize(serializer
)