Skip to content

Commit

Permalink
impl From<Error> for syn::Error
Browse files Browse the repository at this point in the history
The addition of syn::Error::combine makes this possible.
  • Loading branch information
TedDriggs committed Apr 28, 2022
1 parent d125c0a commit e660fe2
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions core/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,18 +350,7 @@ impl Error {

#[cfg(not(feature = "diagnostics"))]
{
self.flatten()
.into_iter()
.map(|e| e.single_to_syn_error().to_compile_error())
.collect()
}
}

#[cfg(not(feature = "diagnostics"))]
fn single_to_syn_error(self) -> ::syn::Error {
match self.span {
Some(span) => ::syn::Error::new(span, self.kind),
None => ::syn::Error::new(Span::call_site(), self),
syn::Error::from(self).into_compile_error()
}
}

Expand Down Expand Up @@ -455,6 +444,23 @@ impl From<syn::Error> for Error {
}
}

impl From<Error> for syn::Error {
fn from(e: Error) -> Self {
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")
}
}
}

// Don't want to publicly commit to Error supporting equality yet, but
// not having it makes testing very difficult. Note that spans are not
// considered for equality since that would break testing in most cases.
Expand Down

0 comments on commit e660fe2

Please sign in to comment.