-
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.
This allows overriding how FromMeta-deriving types handle shorthand declarations or being absent altogether. Previously, this was done using `with` on each field where the type was consumed, but that could cause attribute sprawl or inconsistent behavior when consuming the type. This commit includes validations to avoid conflicts for unit structs, newtype structs, and enums that use the `word` variant-level attribute. Fixes #320
- Loading branch information
Showing
8 changed files
with
353 additions
and
25 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
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,30 @@ | ||
use darling::FromMeta; | ||
|
||
// This usage of `from_word` is invalid because unit structs already generate a from_word | ||
// method, and we don't allow using the from_word override when it conflicts with the macro's | ||
// "normal" operation. | ||
#[derive(FromMeta)] | ||
#[darling(from_word = || Ok(Unit))] | ||
struct Unit; | ||
|
||
fn newtype_from_word() -> darling::Result<Newtype> { | ||
Ok(Newtype(true)) | ||
} | ||
|
||
// This usage of `from_word` is invalid because newtype structs call the inner type's `from_meta` | ||
// directly from their `from_meta`, so the custom `from_word` will never be called in normal usage. | ||
#[derive(FromMeta)] | ||
#[darling(from_word = newtype_from_word)] | ||
struct Newtype(bool); | ||
|
||
#[derive(FromMeta)] | ||
#[darling(from_word = || Ok(Wordy::Options { thing: "Hello".to_string() }))] | ||
enum Wordy { | ||
#[darling(word)] | ||
Normal, | ||
Options { | ||
thing: String, | ||
}, | ||
} | ||
|
||
fn main() {} |
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,17 @@ | ||
error: `from_word` cannot be used on unit structs because it conflicts with the generated impl | ||
--> tests/compile-fail/from_word.rs:7:23 | ||
| | ||
7 | #[darling(from_word = || Ok(Unit))] | ||
| ^ | ||
|
||
error: `from_word` cannot be used on newtype structs because the implementation is entirely delegated to the inner type | ||
--> tests/compile-fail/from_word.rs:17:23 | ||
| | ||
17 | #[darling(from_word = newtype_from_word)] | ||
| ^^^^^^^^^^^^^^^^^ | ||
|
||
error: `from_word` cannot be used with an enum that also uses `word` | ||
--> tests/compile-fail/from_word.rs:21:23 | ||
| | ||
21 | #[darling(from_word = || Ok(Wordy::Options { thing: "Hello".to_string() }))] | ||
| ^ |
Oops, something went wrong.