Skip to content

Commit

Permalink
impl From<syn::Error> for Error
Browse files Browse the repository at this point in the history
`syn` provides useful functions for low-level AST parsing that go beyond
what `darling` attempts to support natively. In these cases, a caller
may want to propagate a `syn::Error` up through a function that returns
a `darling::Result`.

This new impl guarantees lossless handling, emitting the same compiler
error is emitted as if the `syn::Error::to_compile_error` had been
called directly.
  • Loading branch information
TedDriggs committed Jan 5, 2021
1 parent 9efe2f4 commit 9ef676e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Any crates using `darling` that relied on those attributes being silently ignored could see new errors reported in their dependent crates. [#113](https://github.com/TedDriggs/darling/pull/113)
- Impl `syn::spanned::Spanned` for `darling::util::SpannedValue` [#113](https://github.com/TedDriggs/darling/pull/113)
- Add `darling::util::parse_attribute_to_meta_list` to provide useful errors during attribute parsing [#113](https://github.com/TedDriggs/darling/pull/113)
- Add `impl From<syn::Error> for Error` to losslessly propagate `syn` errors [#115](https://github.com/TedDriggs/darling/pull/115)

## v0.11.0 (December 14, 2020)
- Bump minor version due to unexpected breaking change [#107](https://github.com/TedDriggs/darling/issues/107)
Expand Down
13 changes: 13 additions & 0 deletions core/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,19 @@ impl fmt::Display for Error {
}
}

impl From<syn::Error> for Error {
fn from(e: syn::Error) -> Self {
// This impl assumes there is nothing but the message and span that needs to be preserved
// from the passed-in error. If this changes at some point, a new ErrorKind should be made
// to hold the syn::Error, and this impl should preserve it unmodified while setting its own
// span to be a copy of the passed-in error.
Self {
span: Some(e.span()),
..Self::custom(e)
}
}
}

// 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 9ef676e

Please sign in to comment.