Skip to content

Commit

Permalink
impl Clone for Error
Browse files Browse the repository at this point in the history
  • Loading branch information
TedDriggs committed Apr 28, 2022
1 parent e660fe2 commit bafada6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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<darling::Error> 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)

Expand Down
8 changes: 4 additions & 4 deletions core/src/error/kind.rs
Expand Up @@ -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),
Expand Down Expand Up @@ -104,10 +104,10 @@ impl From<ErrorUnknownField> 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<String>,
Expand Down
21 changes: 11 additions & 10 deletions core/src/error/mod.rs
Expand Up @@ -56,8 +56,7 @@ pub type Result<T> = ::std::result::Result<T, Error>;
/// 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<String>,
Expand Down Expand Up @@ -449,14 +448,16 @@ impl From<Error> 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
}
}
}
Expand Down

0 comments on commit bafada6

Please sign in to comment.