pub struct Accumulator(_);
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

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 ?.

Handles a possible error.

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

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

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.

Returns the accumulated errors as a Vec.

This function defuses the drop bomb.

Add one error to the collection.

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

Formats the value using the given formatter. Read more

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

Executes the destructor for this type. Read more

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

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 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

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.