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
impl Accumulator
sourcepub fn handle_in<T, F: FnOnce() -> Result<T>>(&mut self, f: F) -> Option<T>
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 ?
.
sourcepub fn handle<T>(&mut self, result: Result<T>) -> Option<T>
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
.
sourcepub fn finish(self) -> Result<()>
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.
sourcepub fn finish_with<T>(self, success: T) -> Result<T>
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
.
sourcepub fn into_inner(self) -> Vec<Error>
pub fn into_inner(self) -> Vec<Error>
Returns the accumulated errors as a Vec
.
This function defuses the drop bomb.
sourcepub fn checkpoint(self) -> Result<Accumulator>
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
impl Debug for Accumulator
source§impl Default for Accumulator
impl Default for Accumulator
source§impl Drop for Accumulator
impl Drop for Accumulator
source§impl Extend<Error> for Accumulator
impl Extend<Error> for Accumulator
source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = Error>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = Error>,
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)