serde_cursor 0.4.0

fetch the desired parts of a serde-compatible data format efficiently using a jq-like language
Documentation
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use core::fmt;
use core::marker::PhantomData;

use serde_core::Deserialize;
use serde_core::Deserializer;
use serde_core::de::DeserializeSeed;
use serde_core::de::IgnoredAny;
use serde_core::de::MapAccess;
use serde_core::de::SeqAccess;
use serde_core::de::Visitor;

use crate::ConstPathSegment;
use crate::Cursor;
use crate::Path;
use crate::PathEnd;
use crate::PathSegment;

impl<'de, T, P> Deserialize<'de> for Cursor<T, P>
where
    T: Deserialize<'de>,
    P: DeserializePath<'de, T>,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = <P as DeserializePath<'de, T>>::deserialize(deserializer)?;
        Ok(Self(value, PhantomData))
    }
}

/// Deserializes the path to the type returned by [`Cursor!`]
///
/// For more information, see the [crate-level](crate) documentation.
#[diagnostic::on_unimplemented(
    message = "`{T}` doesn't implement `serde_cursor::Sequence`",
    note = "try: `Vec<{T}>`"
)]
pub trait DeserializePath<'de, T> {
    fn deserialize<D>(deserializer: D) -> Result<T, D::Error>
    where
        D: Deserializer<'de>;
}

// base case: we are at the target property
//
// Cursor!(package[0].name: String)
//                    ^^^^ we are here
//
// So we call: <String as Deserialize>::deserialize
impl<'de, T: Deserialize<'de>> DeserializePath<'de, T> for PathEnd {
    fn deserialize<D>(deserializer: D) -> Result<T, D::Error>
    where
        D: Deserializer<'de>,
    {
        // The recursion ends here.
        <T as Deserialize<'de>>::deserialize(deserializer)
    }
}

// step case: we are still digging into the object
//
// Cursor!(package.name: String)
//         ^^^^^^^ we may be here
//
// Path<
//     Field<"package">, <-- we may be here
//     Path<
//         Field<"name">,
//         PathEnd
//     >
// >
//
// Now calling either of:
//
// - deserializer.deserialize_map
// - deserializer.deserialize_seq
//
// Deserializes the entire rest of the data:
//
// Cursor!(package.name.whatever: String)
//                 ^^^^^^^^^^^^^^^^^^^ all of this will be deserialized in this step
//
// Path<
//     Field<"package">,
//
//     Path<              |
//         Field<"name">, |
//         PathEnd        |
//     >                  |
//     ^^^^^^^^^^^^^^^^^^^^ all of this will be deserialized (a single recursive step)
// >
//
impl<'de, S, P, T> DeserializePath<'de, T> for Path<S, P>
where
    S: ConstPathSegment, // const S: PathSegment
    P: DeserializePath<'de, T>,
    T: Deserialize<'de>,
{
    fn deserialize<D>(deserializer: D) -> Result<T, D::Error>
    where
        D: Deserializer<'de>,
    {
        let segment = <S as ConstPathSegment>::VALUE;

        // The path `P` corresponds to everything after "package"
        // while `String` corresponds to the value of the field

        let result = match segment {
            // The current segment is a named field.
            //
            // Cursor!(package.serde.name: String)
            //         ^^^^^^^ we are here
            PathSegment::Field(name) => {
                // Cursor!(package.serde.name: String)
                //                 ^^^^^^^^^^ deserialize all of this
                deserializer.deserialize_map(FieldVisitor::<P, T> {
                    target: name,
                    _marker: PhantomData,
                })
            }

            // The current segment is an index into a sequence.
            //
            // Cursor!(packages[4].name: String)
            //         ^^^^^^^^ we are here
            PathSegment::Index(index) => {
                // Cursor!(packages[4].name: String)
                //                 ^^^^^^^^ deserialize all of this
                deserializer.deserialize_seq(SequenceVisitor::<P, T> {
                    target_index: index,
                    _marker: PhantomData,
                })
            }
        };

        #[cfg(not(feature = "alloc"))]
        return result;

        // wrap the error with the current path segment
        #[cfg(feature = "alloc")]
        return result.map_err(|e| {
            use alloc::format;
            use alloc::string::ToString as _;

            use serde_core::de::Error;

            let err_str = e.to_string();
            let path_str = match segment {
                PathSegment::Field(name) => format!(".{}", name),
                PathSegment::Index(i) => format!("[{}]", i),
            };

            // if the error is a nested path (starts with . or [), just join them
            if err_str.starts_with('.') || err_str.starts_with('[') {
                D::Error::custom(format_args!("{}{}", path_str, err_str))
            }
            // if this is the "top" level of the path, ensure it starts with a dot
            else {
                let prefix = if path_str.starts_with('[') {
                    format!(".{}", path_str)
                } else {
                    path_str
                };
                D::Error::custom(format_args!("{}: {}", prefix, err_str))
            }
        });
    }
}

/// Deserializes field named `target` at the Path (`P` implementing [`DeserializePath`])
/// into the type `T` implementing [`Deserialize`].
struct FieldVisitor<P, T> {
    /// The field that we are searching for.
    target: &'static str,
    _marker: PhantomData<(P, T)>,
}

impl<'de, P, T> Visitor<'de> for FieldVisitor<P, T>
where
    P: DeserializePath<'de, T>,
    T: Deserialize<'de>,
{
    type Value = T;

    fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "map with field '{}'", self.target)
    }

    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
    where
        A: MapAccess<'de>,
    {
        // We search for the same key in the map as `self.target`.

        let mut result = None;

        // If `self.target` is "key", then we follow these steps:
        //
        // {
        //     "foo": ?, // value ignored.
        //     ^^^^^ Is this "key"? NO! value is not deserialized
        //
        //     "bar": ?, // value Ignored.
        //     ^^^^^ Is this "key"? NO! value is not deserialized
        //
        //     "key": ?
        //     ^^^^^ Is this "key"? YES! value is deserialized
        // }
        //
        //     "key": ?
        //            ^ deserializing the value is job of the `PathSeed`
        while let Some(key) = map.next_key::<CowStr<'de>>()? {
            if key.as_ref() == self.target && result.is_none() {
                // The value is deserialized using the `DeserializeSeed` implementation
                // for `PathSeed`, which delegates to `DeserializePath::deserialize`
                result = Some(map.next_value_seed(PathSeed::<P, T>(PhantomData))?);
            } else {
                // The value is ignored, not deserialized - we don't have to
                // read a large document into memory
                map.next_value::<IgnoredAny>()?;
            }
        }

        match result {
            // The map contains a key of the same name as `self.target`.
            Some(val) => Ok(val),

            // The map does not contain a key of the same name as `self.target`.
            None => {
                // This allows Option<T> to become None instead of failing
                // deserialization completely.
                //
                // Say we are searching for the "dependencies" key, but we can't find it.
                //
                // let value = from_str::<Cursor!(package.dependencies.serde.name: Option<String>)>(json)?.0;
                //                                        ^^^^^^^^^^^^ key not found
                //
                // Then `value` will be `None`, instead of an error occurring.
                //
                // We are basically inserting this key into the map:
                //
                // "package": {
                //     "dependencies": null
                //     ^^^^^^^^^^^^^^^^^^^^ this wasn't here before, but we added it
                //                          effectively treat missing value same as if the
                //                          value is specified to be "null"
                // }
                let result =
                    T::deserialize(serde_core::de::value::UnitDeserializer::<A::Error>::new());

                // If the Option<T> case failed, that means this is a regular type - like
                // a String, for example. So the field was required, and we report an error.
                result.map_err(|_| {
                    <A::Error as serde_core::de::Error>::custom(format_args!(
                        "missing field '{}'",
                        self.target
                    ))
                })
            }
        }
    }
}

/// Deserializes the element at `target_index` at the Path (`P` implementing [`DeserializePath`]).
struct SequenceVisitor<P, T> {
    /// The index in the sequence we want to navigate into.
    target_index: usize,
    _marker: PhantomData<(P, T)>,
}

impl<'de, P, T> Visitor<'de> for SequenceVisitor<P, T>
where
    P: DeserializePath<'de, T>,
    T: Deserialize<'de>,
{
    type Value = T;

    fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "a sequence containing at least {} elements",
            self.target_index + 1
        )
    }

    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
    where
        A: SeqAccess<'de>,
    {
        // Cursor!(packages[4].name: String)
        //          ^^^^^^^^ we are here, looking for index 4

        // Skip elements before the target index to avoid memory bloat.
        // [
        //   {...}, // index 0: IgnoredAny
        //   {...}, // index 1: IgnoredAny
        //   {...}, // index 2: IgnoredAny
        //   {...}, // index 3: IgnoredAny
        //   {...}  // index 4: This is the one!
        // ]
        for i in 0..self.target_index {
            if seq.next_element::<IgnoredAny>()?.is_none() {
                return Err(serde_core::de::Error::custom(format_args!(
                    "index {} out of bounds (length {})",
                    self.target_index, i
                )));
            }
        }

        // We found the index. Now we use PathSeed to continue the recursion
        // for the rest of the path (e.g., ".name").
        let result = seq
            .next_element_seed(PathSeed::<P, T>(PhantomData))?
            .ok_or_else(|| {
                serde_core::de::Error::custom(format_args!(
                    "index {} out of bounds",
                    self.target_index
                ))
            })?;

        // Exhaust the sequence. Some formats (like binary formats or strict JSON
        // parsers) require the visitor to finish the entire sequence.
        while seq.next_element::<IgnoredAny>()?.is_some() {}

        Ok(result)
    }
}

/// A [`DeserializeSeed`] that allows us to pass our path-traversal state.
pub(crate) struct PathSeed<P, T>(pub(crate) PhantomData<(P, T)>);

impl<'de, P, T> DeserializeSeed<'de> for PathSeed<P, T>
where
    P: DeserializePath<'de, T>,
    T: Deserialize<'de>,
{
    type Value = T;

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        <P as DeserializePath<'de, T>>::deserialize(deserializer)
    }
}

#[cfg(feature = "serde_with")]
impl<'de, T, P> serde_with::DeserializeAs<'de, T> for Cursor<T, P>
where
    T: Deserialize<'de>,
    P: DeserializePath<'de, T>,
{
    fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>
    where
        D: Deserializer<'de>,
    {
        <P as DeserializePath<'de, T>>::deserialize(deserializer)
    }
}

#[derive(Debug, PartialEq, Eq)]
pub enum CowStr<'a> {
    Borrowed(&'a str),
    #[cfg(feature = "alloc")]
    Owned(alloc::string::String),
}

impl<'a> AsRef<str> for CowStr<'a> {
    fn as_ref(&self) -> &str {
        match self {
            CowStr::Borrowed(s) => s,
            #[cfg(feature = "alloc")]
            CowStr::Owned(s) => s.as_str(),
        }
    }
}

impl<'de> Deserialize<'de> for CowStr<'de> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct CowStrVisitor;

        impl<'de> serde_core::de::Visitor<'de> for CowStrVisitor {
            type Value = CowStr<'de>;

            fn expecting(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
                f.write_str("a string")
            }

            fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E> {
                Ok(CowStr::Borrowed(v))
            }

            #[cfg(feature = "alloc")]
            fn visit_str<E: serde_core::de::Error>(self, v: &str) -> Result<Self::Value, E> {
                Ok(CowStr::Owned(alloc::string::String::from(v)))
            }

            #[cfg(not(feature = "alloc"))]
            fn visit_str<E: serde_core::de::Error>(self, _v: &str) -> Result<Self::Value, E> {
                Err(E::custom(
                    "owned strings require the 'alloc' feature of `serde_cursor`",
                ))
            }
        }

        deserializer.deserialize_str(CowStrVisitor)
    }
}