Struct darling_core::error::Accumulator

source ·
pub struct Accumulator(/* private fields */);
Expand description

Accumulator for errors, for helping call Error::multiple.

See the docs for darling::Error for more discussion of error handling with darling.

§Panics

Accumulator panics on drop unless finish, finish_with, or into_inner has been called, even if it contains no errors. If you want to discard an Accumulator that you know to be empty, use accumulator.finish().unwrap().

§Example

fn validate_things(inputs: Vec<Thing>) -> darling::Result<Vec<Output>> {
    let mut errors = darling::Error::accumulator();

    let outputs = inputs
        .into_iter()
        .filter_map(|thing| errors.handle_in(|| thing.validate()))
        .collect::<Vec<_>>();

    errors.finish()?;
    Ok(outputs)
}

Implementations§

source§

impl Accumulator

source

pub fn handle_in<T, F: FnOnce() -> Result<T>>(&mut self, f: F) -> Option<T>

Runs a closure, returning the successful value as Some, or collecting the error

The closure’s return type is darling::Result, so inside it one can use ?.

source

pub fn handle<T>(&mut self, result: Result<T>) -> Option<T>

Handles a possible error.

Returns a successful value as Some, or collects the error and returns None.

source

pub fn finish(self) -> Result<()>

Stop accumulating errors, producing Ok if there are no errors or producing an error with all those encountered by the accumulator.

source

pub fn finish_with<T>(self, success: T) -> Result<T>

Bundles the collected errors if there were any, or returns the success value

Call this at the end of your input processing.

If there were no errors recorded, returns Ok(success). Otherwise calls Error::multiple and returns the result as an Err.

source

pub fn into_inner(self) -> Vec<Error>

Returns the accumulated errors as a Vec.

This function defuses the drop bomb.

source

pub fn push(&mut self, error: Error)

Add one error to the collection.

source

pub fn checkpoint(self) -> Result<Accumulator>

Finish the current accumulation, and if there are no errors create a new Self so processing may continue.

This is shorthand for:

errors.finish()?;
errors = Error::accumulator();
§Drop Behavior

This function returns a new Accumulator in the success case. This new accumulator is “armed” and will detonate if dropped without being finished.

§Example
fn validate(lorem_inputs: &[Thing], ipsum_inputs: &[Thing])
            -> darling::Result<(Vec<Output>, Vec<Output>)> {
    let mut errors = darling::Error::accumulator();

    let lorems = lorem_inputs.iter().filter_map(|l| {
        errors.handle(l.validate())
    }).collect();

    errors = errors.checkpoint()?;

    let ipsums = ipsum_inputs.iter().filter_map(|l| {
        errors.handle(l.validate())
    }).collect();

    errors.finish_with((lorems, ipsums))
}

Trait Implementations§

source§

impl Debug for Accumulator

source§

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

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

impl Default for Accumulator

source§

fn default() -> Self

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

impl Drop for Accumulator

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Extend<Error> for Accumulator

source§

fn extend<I>(&mut self, iter: I)
where I: IntoIterator<Item = Error>,

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.