Skip to main content

CollectedErrors

Struct CollectedErrors 

Source
pub struct CollectedErrors { /* private fields */ }
Expand description

The errors a map or sequence collected from its items.

This is a helper for sinks that collect the errors of their items (see State::set_collect_errors). In Sink::recover the error is passed to collect, which keeps it if errors are collected. Once the container is complete, finish fails with all errors that were collected:

use deser::de::{CollectedErrors, Deserialize, Sink, SinkHandle};
use deser::{Error, State};

struct Numbers(Vec<u32>);

struct NumbersSink<'a> {
    out: &'a mut Option<Numbers>,
    numbers: Vec<u32>,
    current: Option<u32>,
    errors: CollectedErrors,
}

impl<'a> NumbersSink<'a> {
    fn flush(&mut self) {
        self.numbers.extend(self.current.take());
    }
}

impl<'de> Sink<'de> for NumbersSink<'_> {
    fn seq(&mut self, _state: &mut State) -> Result<(), Error> {
        Ok(())
    }

    fn next_value(
        &mut self,
        state: &mut State,
    ) -> Result<SinkHandle<'_, 'de>, Error> {
        self.flush();
        Ok(u32::deserialize_into(&mut self.current, state))
    }

    fn recover(
        &mut self,
        err: Error,
        state: &mut State,
    ) -> Result<(), Error> {
        self.current = None;
        self.errors.collect(err, state)
    }

    fn finish(&mut self, _state: &mut State) -> Result<(), Error> {
        self.errors.finish()?;
        self.flush();
        *self.out = Some(Numbers(std::mem::take(&mut self.numbers)));
        Ok(())
    }
}

Implementations§

Source§

impl CollectedErrors

Source

pub const fn new() -> CollectedErrors

Creates an empty list of errors.

Source

pub fn is_empty(&self) -> bool

Returns true if no errors were collected.

Source

pub fn collect(&mut self, err: Error, state: &mut State) -> Result<(), Error>

Collects the error of an item if errors are collected.

If errors are not collected (or the limit of errors is reached, see State::set_max_errors), the error is returned together with the errors collected so far. Errors which do not have the context of an event attached yet (see Error) get the context of the current event.

Source

pub fn push(&mut self, err: Error, state: &State)

Adds an error that is not the error of an item.

This is for errors that are found once the container is complete, for instance missing fields. They are always added and do not count towards the limit of errors. Errors which do not have the context of an event attached yet get the context of the current event.

Source

pub fn take(&mut self) -> Option<Error>

Takes the collected errors as a single error.

Returns None if there are none.

Source

pub fn finish(&mut self) -> Result<(), Error>

Fails with the collected errors if there are any.

Trait Implementations§

Source§

impl Debug for CollectedErrors

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CollectedErrors

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.