Struct darling::error::Accumulator
source · [−]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
sourceimpl Accumulator
impl Accumulator
sourcepub fn handle_in<T, F>(&mut self, f: F) -> Option<T> where
F: FnOnce() -> Result<T, Error>,
pub fn handle_in<T, F>(&mut self, f: F) -> Option<T> where
F: FnOnce() -> Result<T, Error>,
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 ?
.
sourcepub fn handle<T>(&mut self, result: Result<T, Error>) -> Option<T>
pub fn handle<T>(&mut self, result: Result<T, Error>) -> Option<T>
Handles a possible error.
Returns a successful value as Some
, or collects the error and returns None
.
sourcepub fn finish(self) -> Result<(), Error>
pub fn finish(self) -> Result<(), Error>
Stop accumulating errors, producing Ok
if there are no errors or producing
an error with all those encountered by the accumulator.
sourcepub fn finish_with<T>(self, success: T) -> Result<T, Error>
pub fn finish_with<T>(self, success: T) -> Result<T, Error>
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
.
sourcepub fn into_inner(self) -> Vec<Error, Global>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
pub fn into_inner(self) -> Vec<Error, Global>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
A: Allocator,
Returns the accumulated errors as a Vec
.
This function defuses the drop bomb.
sourcepub fn checkpoint(self) -> Result<Accumulator, Error>
pub fn checkpoint(self) -> Result<Accumulator, Error>
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
sourceimpl Debug for Accumulator
impl Debug for Accumulator
sourceimpl Default for Accumulator
impl Default for Accumulator
sourcepub fn default() -> Accumulator
pub fn default() -> Accumulator
Returns the “default value” for a type. Read more
sourceimpl Drop for Accumulator
impl Drop for Accumulator
sourceimpl Extend<Error> for Accumulator
impl Extend<Error> for Accumulator
sourcepub fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = Error>,
pub fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = Error>,
Extends a collection with the contents of an iterator. Read more
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Extends a collection with exactly one element.
sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
Auto Trait Implementations
impl RefUnwindSafe for Accumulator
impl !Send for Accumulator
impl !Sync for Accumulator
impl Unpin for Accumulator
impl UnwindSafe for Accumulator
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more