deser-core 0.9.0

Core traits and types of deser, use the deser crate instead
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
//! Error interface.
use alloc::borrow::Cow;
use alloc::boxed::Box;
use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;
use core::any::{Any, TypeId};
use core::fmt;

use crate::{Position, State};

/// Describes the kind of error.
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum ErrorKind {
    UnsupportedType,
    Unexpected,
    MissingField,
    OutOfRange,
    WrongLength,
    EndOfFile,
    /// Reading or writing failed (see `deser::io`).  The IO error is
    /// the [`source`](std::error::Error::source) of the error.
    Io,
}

/// Additional information attached to an [`Error`].
///
/// Besides the location in the input, which is built into errors, layers
/// and other code can attach typed values to errors with
/// [`Error::with_attachment`] and retrieve them with
/// [`Error::attachment`].  An error holds at most one attachment per type.
/// For instance the `deser-path` crate attaches the path of the value an
/// error refers to.
///
/// Attachments can contribute to the [`Display`](fmt::Display) output of
/// the error with [`fmt_context`](Self::fmt_context).
///
/// ```
/// use std::fmt;
/// use deser::{Error, ErrorAttachment, ErrorKind};
///
/// #[derive(Debug)]
/// struct FileName(String);
///
/// impl ErrorAttachment for FileName {
///     fn fmt_context(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
///         write!(f, " in {}", self.0)
///     }
/// }
///
/// let err = Error::new(ErrorKind::Unexpected, "unexpected string")
///     .with_position(12, 2, 5)
///     .with_attachment(FileName("config.json".into()));
/// assert_eq!(err.attachment::<FileName>().unwrap().0, "config.json");
/// assert_eq!(
///     err.to_string(),
///     "Unexpected: unexpected string at line 2 column 5 in config.json"
/// );
/// ```
pub trait ErrorAttachment: Any + fmt::Debug + Send + Sync {
    /// Writes the attachment as part of the error message.
    ///
    /// The output is appended to the message and the location of the
    /// error, so it typically starts with a space.  By default attachments
    /// are not shown.
    fn fmt_context(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let _ = f;
        Ok(())
    }
}

/// Adds context to errors, see [`State::add_error_context`](crate::State::add_error_context).
///
/// This is typically implemented by the extension type which holds the
/// information that is attached to errors.
pub trait ErrorContext: 'static {
    /// Adds context to an error.
    ///
    /// This is invoked with the state as it was when the error happened.
    /// Context that is already attached to the error should not be
    /// replaced.
    fn add_context(err: Error, state: &State) -> Error;
}

/// An error for deser.
///
/// Besides a kind and a message an error can carry context: the location
/// in the input it refers to (see [`offset`](Self::offset),
/// [`line`](Self::line) and [`column`](Self::column)) and typed
/// attachments (see [`ErrorAttachment`]).  The context is part of the
/// [`Display`](fmt::Display) output:
///
/// ```
/// use deser::{Error, ErrorKind};
///
/// let err = Error::new(ErrorKind::Unexpected, "unexpected string")
///     .with_position(12, 2, 5);
/// assert_eq!(
///     err.to_string(),
///     "Unexpected: unexpected string at line 2 column 5"
/// );
/// ```
///
/// Errors raised while deserializing a value (for instance by a
/// [`Sink`](crate::de::Sink)) get the context attached by the
/// [`DeserializeDriver`](crate::de::DeserializeDriver): the start of the
/// input range of the event (see [`State::input_range`](crate::State::input_range))
/// and the context of the types registered with
/// [`State::add_error_context`](crate::State::add_error_context).  Formats
/// resolve the offsets into lines and columns.
///
/// # Multiple Errors
///
/// An error can hold multiple errors, for instance if deserialization
/// continued after an error to report all problems of the input at once
/// (see [`State::set_collect_errors`](crate::State::set_collect_errors)).
/// The accessors ([`kind`](Self::kind), [`message`](Self::message), the
/// location and the attachments) refer to the first of them, all of them
/// are iterated with [`errors`](Self::errors).  The
/// [`Display`](fmt::Display) output mentions how many more errors there
/// are, with the alternate flag (`{:#}`) it lists all of them, one per
/// line:
///
/// ```
/// use deser::{Error, ErrorKind};
///
/// let err = Error::from_errors([
///     Error::new(ErrorKind::MissingField, "missing field `a`")
///         .with_offset(0),
///     Error::new(ErrorKind::Unexpected, "unexpected string")
///         .with_offset(9),
/// ])
/// .unwrap()
/// .resolve_position(b"{\n  \"b\": \"x\"}");
/// assert_eq!(err.error_count(), 2);
/// assert_eq!(err.kind(), ErrorKind::MissingField);
/// assert_eq!(
///     err.to_string(),
///     "MissingField: missing field `a` at line 1 column 1 \
///      (and 1 more error)"
/// );
/// assert_eq!(
///     format!("{:#}", err),
///     "MissingField: missing field `a` at line 1 column 1\n\
///      Unexpected: unexpected string at line 2 column 8"
/// );
/// ```
pub struct Error {
    // boxed so that results stay small.  Errors are rare but results are
    // passed around for every single value.
    inner: Box<ErrorInner>,
}

enum ErrorInner {
    Single(ErrorData),
    // at least two errors, all of them are single errors.  The first one
    // is the error the accessors refer to.
    Multiple(Vec<Error>),
}

#[derive(Debug)]
struct ErrorData {
    kind: ErrorKind,
    msg: Cow<'static, str>,
    source: Option<Box<dyn core::error::Error + Send + Sync>>,
    offset: Option<usize>,
    // line and column (1-based)
    line_column: Option<(usize, usize)>,
    // in the order they were attached, at most one per type
    attachments: Vec<Attachment>,
    // `true` once the driver attached the context of the current event.
    has_context: bool,
    // `true` once the error was collected (see `CollectedErrors`)
    collected: bool,
}

#[derive(Debug)]
struct Attachment {
    // Invariant: the type of the value
    type_id: TypeId,
    value: Box<dyn ErrorAttachment>,
}

impl Error {
    /// Creates a new error.
    #[cold]
    pub fn new<M: Into<Cow<'static, str>>>(kind: ErrorKind, msg: M) -> Error {
        Error {
            inner: Box::new(ErrorInner::Single(ErrorData {
                kind,
                msg: msg.into(),
                source: None,
                offset: None,
                line_column: None,
                attachments: Vec::new(),
                has_context: false,
                collected: false,
            })),
        }
    }

    /// Combines errors into one.
    ///
    /// Errors that hold multiple errors are flattened (see
    /// [`push_error`](Self::push_error)).  Returns `None` if there are no
    /// errors.
    pub fn from_errors<I: IntoIterator<Item = Error>>(errors: I) -> Option<Error> {
        let mut errors = errors.into_iter();
        let mut rv = errors.next()?;
        for err in errors {
            rv.push_error(err);
        }
        Some(rv)
    }

    /// Creates the error for a value that is serialized while another one
    /// is only partially written.
    ///
    /// Stream serializers return this once they are
    /// [in progress](crate::ser::StreamSerializer::in_progress) and are asked
    /// to serialize another value.
    #[cold]
    pub fn in_progress() -> Error {
        Error::new(
            ErrorKind::Unexpected,
            "a value was only partially written, the stream cannot continue",
        )
    }

    /// Adds an error to this error.
    ///
    /// If the error that is added holds multiple errors, they are added
    /// individually: errors do not nest (see [`errors`](Self::errors)).
    pub fn push_error(&mut self, err: Error) {
        let errors = self.make_multiple();
        match *err.inner {
            ErrorInner::Single(data) => errors.push(Error {
                inner: Box::new(ErrorInner::Single(data)),
            }),
            ErrorInner::Multiple(others) => errors.extend(others),
        }
    }

    /// Turns the error into one that holds multiple errors.
    fn make_multiple(&mut self) -> &mut Vec<Error> {
        if let ErrorInner::Single(_) = *self.inner {
            let first = core::mem::replace(&mut *self.inner, ErrorInner::Multiple(Vec::new()));
            if let ErrorInner::Multiple(ref mut errors) = *self.inner {
                errors.push(Error {
                    inner: Box::new(first),
                });
            }
        }
        match *self.inner {
            ErrorInner::Multiple(ref mut errors) => errors,
            ErrorInner::Single(_) => unreachable!(),
        }
    }

    /// Iterates over the errors this error holds.
    ///
    /// For an error that holds a single error, this is the error itself.
    /// The errors that are returned hold a single error each.
    pub fn errors(&self) -> impl Iterator<Item = &Error> {
        match *self.inner {
            ErrorInner::Single(_) => core::slice::from_ref(self).iter(),
            ErrorInner::Multiple(ref errors) => errors.iter(),
        }
    }

    /// Returns the number of errors this error holds.
    pub fn error_count(&self) -> usize {
        match *self.inner {
            ErrorInner::Single(_) => 1,
            ErrorInner::Multiple(ref errors) => errors.len(),
        }
    }

    /// Returns the data of the (first) error.
    fn data(&self) -> &ErrorData {
        match *self.inner {
            ErrorInner::Single(ref data) => data,
            ErrorInner::Multiple(ref errors) => errors[0].data(),
        }
    }

    /// Returns the data of the (first) error mutably.
    fn data_mut(&mut self) -> &mut ErrorData {
        match *self.inner {
            ErrorInner::Single(ref mut data) => data,
            ErrorInner::Multiple(ref mut errors) => errors[0].data_mut(),
        }
    }

    /// Applies a function to every error this error holds.
    pub(crate) fn map_each(mut self, mut f: impl FnMut(Error) -> Error) -> Error {
        if let ErrorInner::Multiple(ref mut errors) = *self.inner {
            for err in errors.iter_mut() {
                let taken = core::mem::replace(err, Error::new(ErrorKind::Unexpected, ""));
                *err = f(taken);
            }
            self
        } else {
            f(self)
        }
    }

    /// Returns the number of errors this error holds that were not
    /// collected yet.
    pub(crate) fn uncollected_count(&self) -> usize {
        self.errors().filter(|err| !err.data().collected).count()
    }

    /// Marks all errors this error holds as collected.
    pub(crate) fn mark_collected(mut self) -> Error {
        self = self.map_each(|mut err| {
            err.data_mut().collected = true;
            err
        });
        self
    }

    /// Attaches another error as source to this error.
    pub fn with_source<E: core::error::Error + Send + Sync + 'static>(mut self, source: E) -> Self {
        self.data_mut().source = Some(Box::new(source));
        self
    }

    /// Returns the kind of the error.
    pub fn kind(&self) -> ErrorKind {
        self.data().kind
    }

    /// Returns the message of the error (without context).
    pub fn message(&self) -> &str {
        &self.data().msg
    }

    /// Sets the byte offset in the input the error refers to.
    ///
    /// A previously set line and column are discarded.
    pub fn with_offset(mut self, offset: usize) -> Self {
        let data = self.data_mut();
        data.offset = Some(offset);
        data.line_column = None;
        self
    }

    /// Sets the byte offset together with its line and column (1-based).
    pub fn with_position(mut self, offset: usize, line: usize, column: usize) -> Self {
        let data = self.data_mut();
        data.offset = Some(offset);
        data.line_column = Some((line, column));
        self
    }

    /// Resolves the offset into line and column.
    ///
    /// The source is the input the offset refers to.  Columns are counted
    /// in characters (bytes that are not UTF-8 continuation bytes).  If the
    /// error has no offset or already has a line and column, it's returned
    /// unchanged.  Text formats call this for the errors they return.
    ///
    /// ```
    /// use deser::{Error, ErrorKind};
    ///
    /// let err = Error::new(ErrorKind::Unexpected, "bad value")
    ///     .with_offset(7)
    ///     .resolve_position(b"[1,\n  x]");
    /// assert_eq!((err.line(), err.column()), (Some(2), Some(4)));
    /// ```
    ///
    /// The positions of further errors (see [`errors`](Self::errors)) are
    /// resolved as well.
    pub fn resolve_position(self, source: &[u8]) -> Self {
        self.map_each(|mut err| {
            let data = err.data_mut();
            if let (Some(offset), None) = (data.offset, data.line_column) {
                let pos = Position::of(source, offset);
                data.line_column = Some((pos.line, pos.column));
            }
            err
        })
    }

    /// Moves the position of the error by the position of the input it
    /// refers to.
    ///
    /// This is used for errors of inputs which are part of a larger input,
    /// the base is the position of the start of the part.
    pub(crate) fn shift_position(self, base: Position) -> Self {
        self.map_each(|mut err| {
            let data = err.data_mut();
            if let Some(ref mut error_offset) = data.offset {
                *error_offset += base.offset;
            }
            if let Some((ref mut error_line, ref mut error_column)) = data.line_column {
                if *error_line == 1 {
                    *error_column += base.column - 1;
                }
                *error_line += base.line - 1;
            }
            err
        })
    }

    /// Returns the byte offset in the input the error refers to.
    pub fn offset(&self) -> Option<usize> {
        self.data().offset
    }

    /// Returns the line (1-based) the error refers to.
    pub fn line(&self) -> Option<usize> {
        self.data().line_column.map(|x| x.0)
    }

    /// Returns the column (1-based, in characters) the error refers to.
    pub fn column(&self) -> Option<usize> {
        self.data().line_column.map(|x| x.1)
    }

    /// Attaches a value to the error.
    ///
    /// An attachment of the same type is replaced but keeps its position
    /// in the [`Display`](fmt::Display) output.  See [`ErrorAttachment`].
    pub fn with_attachment<T: ErrorAttachment>(mut self, value: T) -> Self {
        let type_id = TypeId::of::<T>();
        let value = Box::new(value);
        let attachments = &mut self.data_mut().attachments;
        match attachments.iter_mut().find(|x| x.type_id == type_id) {
            Some(attachment) => attachment.value = value,
            None => attachments.push(Attachment { type_id, value }),
        }
        self
    }

    /// Returns the attachment of the given type.
    pub fn attachment<T: ErrorAttachment>(&self) -> Option<&T> {
        let type_id = TypeId::of::<T>();
        let attachment = self
            .data()
            .attachments
            .iter()
            .find(|x| x.type_id == type_id)?;
        (&*attachment.value as &dyn Any).downcast_ref()
    }

    /// Returns the attachment of the given type mutably.
    pub fn attachment_mut<T: ErrorAttachment>(&mut self) -> Option<&mut T> {
        let type_id = TypeId::of::<T>();
        let attachment = self
            .data_mut()
            .attachments
            .iter_mut()
            .find(|x| x.type_id == type_id)?;
        (&mut *attachment.value as &mut dyn Any).downcast_mut()
    }

    /// Iterates over the attachments in the order they were attached.
    pub fn attachments(&self) -> impl Iterator<Item = &dyn ErrorAttachment> {
        self.data().attachments.iter().map(|x| &*x.value)
    }

    /// Returns `true` if the context of an event was attached.
    pub(crate) fn has_context(&self) -> bool {
        self.data().has_context
    }

    /// Marks the context of an event as attached.
    pub(crate) fn set_has_context(&mut self) {
        self.data_mut().has_context = true;
    }
}

impl fmt::Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let data = match *self.inner {
            ErrorInner::Single(ref data) => data,
            ErrorInner::Multiple(ref errors) => {
                return f.debug_tuple("Errors").field(errors).finish();
            }
        };
        let mut s = f.debug_struct("Error");
        s.field("kind", &data.kind).field("msg", &data.msg);
        if let Some(offset) = data.offset {
            s.field("offset", &offset);
        }
        if let Some((line, column)) = data.line_column {
            s.field("line", &line).field("column", &column);
        }
        if !data.attachments.is_empty() {
            s.field("attachments", &DebugAttachments(&data.attachments));
        }
        s.field("source", &data.source).finish()
    }
}

impl ErrorData {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}: {}", self.kind, self.msg)?;
        match (self.line_column, self.offset) {
            (Some((line, column)), _) => write!(f, " at line {} column {}", line, column)?,
            (None, Some(offset)) => write!(f, " at offset {}", offset)?,
            (None, None) => {}
        }
        for attachment in self.attachments.iter() {
            attachment.value.fmt_context(f)?;
        }
        Ok(())
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let errors = match *self.inner {
            ErrorInner::Single(ref data) => return data.fmt(f),
            ErrorInner::Multiple(ref errors) => errors,
        };
        errors[0].data().fmt(f)?;
        if f.alternate() {
            for err in &errors[1..] {
                writeln!(f)?;
                err.data().fmt(f)?;
            }
        } else if errors.len() == 2 {
            write!(f, " (and 1 more error)")?;
        } else {
            write!(f, " (and {} more errors)", errors.len() - 1)?;
        }
        Ok(())
    }
}

struct DebugAttachments<'a>(&'a [Attachment]);

impl fmt::Debug for DebugAttachments<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list()
            .entries(self.0.iter().map(|x| &x.value))
            .finish()
    }
}

#[cfg(feature = "std")]
impl From<std::io::Error> for Error {
    fn from(err: std::io::Error) -> Error {
        Error::new(ErrorKind::Io, err.to_string()).with_source(err)
    }
}

impl core::error::Error for Error {
    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
        self.data().source.as_ref().map(|err| err.as_ref() as _)
    }
}

/// Creates an error that is thrown away.
///
/// While errors are discarded (see `State::discard_errors`) the common
/// errors are created with this instead of building a message nobody reads.
#[cold]
#[inline(never)]
pub(crate) fn discarded_error(kind: ErrorKind) -> Error {
    Error::new(kind, "discarded error")
}

/// Creates the error for a value that failed to convert or validate.
#[cold]
pub(crate) fn conversion_error<E: fmt::Display>(err: E) -> Error {
    Error::new(ErrorKind::Unexpected, format!("invalid value: {}", err))
}

/// Creates the error for an unknown variant.
///
/// `tag` is the name that was given (if it can be a name), `type_name` the
/// name of the enum and `names` are the names of the variants.
#[cold]
pub fn unknown_variant(tag: Option<&str>, type_name: &str, names: &[&str]) -> Error {
    let mut msg = String::from("unknown variant");
    if let Some(tag) = tag {
        msg.push_str(" `");
        msg.push_str(tag);
        msg.push('`');
    }
    msg.push_str(" of ");
    msg.push_str(type_name);
    push_expected(&mut msg, names, "variants");
    Error::new(ErrorKind::Unexpected, msg)
}

/// Appends the expected names to an error message.
///
/// `what` is what the names are, for the message if there are none.
pub(crate) fn push_expected(msg: &mut String, names: &[&str], what: &str) {
    match names {
        [] => {
            msg.push_str(", there are no ");
            msg.push_str(what);
        }
        [name] => {
            msg.push_str(", expected `");
            msg.push_str(name);
            msg.push('`');
        }
        [first, second] => {
            msg.push_str(", expected `");
            msg.push_str(first);
            msg.push_str("` or `");
            msg.push_str(second);
            msg.push('`');
        }
        names => {
            msg.push_str(", expected one of ");
            for (idx, name) in names.iter().enumerate() {
                if idx > 0 {
                    msg.push_str(", ");
                }
                msg.push('`');
                msg.push_str(name);
                msg.push('`');
            }
        }
    }
}