diff --git a/CHANGELOG.md b/CHANGELOG.md index c017798..0eb4c02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ ## Unreleased - Fix a bug where using a trait that accepts `#[darling(attributes(...))]` without specifying any attributes would emit code that did not compile. [#183](https://github.com/TedDriggs/darling/issues/183) +- Impl `Clone` for `darling::Error` [#184](https://github.com/TedDriggs/darling/pull/184) +- Impl `From for syn::Error` [#184](https://github.com/TedDriggs/darling/pull/184) +- Add `Error::span` and `Error::explicit_span` methods [#184](https://github.com/TedDriggs/darling/pull/184) ## v0.14.0 (April 13, 2022) diff --git a/core/src/error/kind.rs b/core/src/error/kind.rs index 4e3ecb3..0030e0d 100644 --- a/core/src/error/kind.rs +++ b/core/src/error/kind.rs @@ -6,10 +6,10 @@ type DeriveInputShape = String; type FieldName = String; type MetaFormat = String; -#[derive(Debug)] +#[derive(Debug, Clone)] // Don't want to publicly commit to ErrorKind supporting equality yet, but // not having it makes testing very difficult. -#[cfg_attr(test, derive(Clone, PartialEq, Eq))] +#[cfg_attr(test, derive(PartialEq, Eq))] pub(in crate::error) enum ErrorKind { /// An arbitrary error message. Custom(String), @@ -104,10 +104,10 @@ impl From for ErrorKind { /// An error for an unknown field, with a possible "did-you-mean" suggestion to get /// the user back on the right track. -#[derive(Debug)] +#[derive(Clone, Debug)] // Don't want to publicly commit to ErrorKind supporting equality yet, but // not having it makes testing very difficult. -#[cfg_attr(test, derive(Clone, PartialEq, Eq))] +#[cfg_attr(test, derive(PartialEq, Eq))] pub(in crate::error) struct ErrorUnknownField { name: String, did_you_mean: Option, diff --git a/core/src/error/mod.rs b/core/src/error/mod.rs index ceae921..ca68956 100644 --- a/core/src/error/mod.rs +++ b/core/src/error/mod.rs @@ -56,8 +56,7 @@ pub type Result = ::std::result::Result; /// to ensure those errors appear in the right place. Use `darling::util::SpannedValue` to keep /// span information around on parsed fields so that custom diagnostics can point to the correct /// parts of the input AST. -#[derive(Debug)] -#[cfg_attr(test, derive(Clone))] +#[derive(Debug, Clone)] pub struct Error { kind: ErrorKind, locations: Vec, @@ -449,14 +448,16 @@ impl From for syn::Error { if e.len() == 1 { syn::Error::new(e.span(), e) } else { - e.flatten() - .into_iter() - .map(syn::Error::from) - .reduce(|mut accum, next| { - accum.combine(next); - accum - }) - .expect("darling::Error can never be empty") + let mut syn_errors = e.flatten().into_iter().map(syn::Error::from); + let mut error = syn_errors + .next() + .expect("darling::Error can never be empty"); + + for next_error in syn_errors { + error.combine(next_error); + } + + error } } }