Make error collection easier
Today, collecting multiple custom errors requires writing:
let mut errors = vec![];
//... validation code
if !errors.is_empty() {
return Err(darling::Error::multiple(errors));
}
That's not bad, but it's enough boilerplate that it's tempting to write a macro for it.
An alternative would be:
let mut errors = vec![];
// ... validation
darling::Error::check_none(errors)?;
The check_none function would return darling::Result<()>; it would return Ok if the list is empty, and Err if there were errors. The try operator counts as using the Result, so no let _ = is required.
Open Questions
- Is this useful enough to merit expanding the API surface?
- Is this the right API? Alternatives would include one where the caller asks for an "error collector" rather than using a standard
Vec, or a free function indarling::errorrather than a function offdarling::Error. - Is there a preexisting name for this concept from other libraries that we should name it here?
To be honest, I'm not entirely sure about how effective that collection method is at all.
For example, let's say I have an extra check about whether the function the current macro is applied on is unsafe and would build a syn::Error if it does, which contains a span and a message. Currently, the only way to convert that syn::Error into a darling::Error is, as far as I can see, by taking out the span, converting the error to a string as message, finally feeding the message into darling::Error::new() and the span into .with_span.
This could be done easier if darling's error would provide a From<syn::Error> implementation or use syn's mechanism of stacking errors as in syn::Error::combine, but that's just guessing.
Nevermind, darling already does provide a From<syn::Error> implementation, rustdoc messed me up and I was blind. Sorry, then my above comment is only very partially relevant.
There is now darling::Error::accumulator which achieves this.