-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Report errors on unsupported attributes
`darling` pre-dates Rust support for non-meta attributes. These increase flexibility for Rust code, but mean that not `darling` does not parse all valid attributes. `darling` previously ignored these outright, creating misleading errors. This commit makes those explicit errors. Fixes #96
- Loading branch information
Showing
5 changed files
with
145 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#[macro_use] | ||
extern crate darling; | ||
#[macro_use] | ||
extern crate syn; | ||
|
||
use darling::FromDeriveInput; | ||
use syn::{Ident, LitStr, Path}; | ||
|
||
#[derive(Debug, FromDeriveInput)] | ||
#[darling(supports(struct_unit), attributes(bar))] | ||
pub struct Bar { | ||
pub ident: Ident, | ||
pub st: Path, | ||
pub file: LitStr, | ||
} | ||
|
||
/// Per [#96](https://github.com/TedDriggs/darling/issues/96), make sure that an | ||
/// attribute which isn't a valid meta gets an error. | ||
#[test] | ||
fn non_meta_attribute_gets_own_error() { | ||
let di = parse_quote! { | ||
#[derive(Bar)] | ||
#[bar(file = "motors/example_6.csv", st = RocketEngine)] | ||
pub struct EstesC6; | ||
}; | ||
|
||
let errors: darling::Error = Bar::from_derive_input(&di).unwrap_err().flatten(); | ||
// The number of errors here is 1 for the bad attribute + 2 for the missing fields | ||
assert_eq!(3, errors.len()); | ||
// Make sure one of the errors propagates the syn error | ||
assert!(errors | ||
.into_iter() | ||
.any(|e| e.to_string().contains("expected lit"))); | ||
} | ||
|
||
/// Properties can be split across multiple attributes; this test ensures that one | ||
/// non-meta attribute does not interfere with the parsing of other, well-formed attributes. | ||
#[test] | ||
fn non_meta_attribute_does_not_block_others() { | ||
let di = parse_quote! { | ||
#[derive(Bar)] | ||
#[bar(st = RocketEngine)] | ||
#[bar(file = "motors/example_6.csv")] | ||
pub struct EstesC6; | ||
}; | ||
|
||
let errors: darling::Error = Bar::from_derive_input(&di).unwrap_err().flatten(); | ||
// The number of errors here is 1 for the bad attribute + 1 for the missing "st" field | ||
assert_eq!(2, errors.len()); | ||
// Make sure one of the errors propagates the syn error | ||
assert!(errors | ||
.into_iter() | ||
.any(|e| e.to_string().contains("expected lit"))); | ||
} | ||
|
||
#[test] | ||
fn name_value_attribute_gets_own_error() { | ||
let di = parse_quote! { | ||
#[derive(Bar)] | ||
#[bar = "motors/example_6.csv"] | ||
pub struct EstesC6; | ||
}; | ||
|
||
let errors: darling::Error = Bar::from_derive_input(&di).unwrap_err().flatten(); | ||
// The number of errors here is 1 for the bad attribute + 2 for the missing fields | ||
assert_eq!(3, errors.len()); | ||
let expected_message = darling::Error::name_value_attribute().to_string(); | ||
// Make sure one of the errors propagates the syn error | ||
assert!(errors | ||
.into_iter() | ||
.any(|e| e.to_string() == expected_message)); | ||
} |