1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use Arc;
use fmt;
use crateState;
/// The source the input ranges refer to.
///
/// Formats can publish the byte range in the input of every event (see
/// [`State::input_range`](crate::State::input_range)). This is cheap, but
/// resolving the ranges into lines and columns (see
/// [`Position::of`](crate::Position::of)) requires the source. As
/// this requires a copy of the input, formats only provide it when asked to
/// with [`TrackLocations`]. They store it in the [`State`] as an extension
/// value with [`set`](Self::set) before they emit the first event:
///
/// ```
/// use deser::de::DeserializeDriver;
/// use deser::Source;
///
/// let mut out = None::<bool>;
/// let mut driver = DeserializeDriver::new(&mut out);
/// Source("true".into()).set(driver.state_mut());
/// assert_eq!(&*driver.state().get::<Source>().unwrap().0, "true");
/// ```
;
/// Asks the formats to provide the [`Source`] (a value of the
/// [`Context`](crate::Context)).
///
/// Formats publish the byte range of every event, resolving them into
/// lines and columns (for instance with the `Spanned` type of
/// [`deser-location`](https://docs.rs/deser-location)) also requires the
/// source, which is a copy of the input. Formats only provide it if this
/// is set to `true` in the context (or the state). The errors a
/// deserialization fails with have their line and column either way, but
/// for instance the keys collected with
/// [`UnknownFields::Collect`](crate::de::UnknownFields::Collect) only
/// have them with the source:
///
/// ```
/// use deser::de::{Deserializer, IgnoredFields, UnknownFields};
/// use deser::{Context, Deserialize, TrackLocations};
///
/// #[derive(Deserialize)]
/// struct Config {
/// name: String,
/// }
///
/// let config = deser_json::DeserializerConfig::builder()
/// .context(Context::with(TrackLocations(true)))
/// .build();
/// let input = "{\n \"name\": \"demo\",\n \"nmae\": \"x\"\n}";
/// let ignored = IgnoredFields::new();
/// deser_json::Deserializer::from_str_with_config(input, config)
/// .deserialize_with::<Config, _>(|driver| {
/// UnknownFields::Collect(ignored.clone()).set(driver.state_mut())
/// })
/// .unwrap();
/// let ignored = ignored.take();
/// assert_eq!((ignored[0].line(), ignored[0].column()), (Some(3), Some(3)));
/// ```
///
/// Formats check this with [`of`](Self::of) and set the [`Source`] before
/// they emit the first event:
///
/// ```
/// use deser::de::{DeserializeDriver, Deserializer};
/// use deser::{Error, Source, TrackLocations};
///
/// /// A format which provides the source if asked to.
/// struct Text<'a>(&'a str);
///
/// impl<'de> Deserializer<'de> for Text<'de> {
/// fn drive(&mut self, driver: &mut DeserializeDriver<'_, 'de>) -> Result<(), Error> {
/// if TrackLocations::of(driver.state()) {
/// Source(self.0.into()).set(driver.state_mut());
/// }
/// driver.state_mut().set_input_range(0, self.0.len());
/// driver.emit(self.0)
/// }
/// }
/// ```
;