I'm trying to parse an attribute with darling, and I want to support the following usages:
// att not specified
#[derive(MyTrait)]
struct Foo(u64);
// att specified without an argument
#[derive(MyTrait)]
#[myderive(att)]
struct Foo(u64);
// att specified with an argument
#[derive(MyTrait)]
#[myderive(att(value = "String"))]
struct Foo(u64);
These are my types:
#[derive(FromDeriveInput)]
#[darling(attributes(myderive))]
struct MyDeriveInput {
#[darling(default)]
att: Option<MyAttr>,
}
#[derive(FromMeta, Default)]
struct MyAttr {
#[darling(default)]
value: Option<Path>,
}
And a test:
#[test]
fn test() {
let derive_input = syn::parse_str(
r#"
#[derive(MyTrait)]
#[myderive(att)]
struct Foo(u64);
"#,
)
.unwrap();
let parsed: MyDeriveInput = FromDeriveInput::from_derive_input(&derive_input).unwrap();
assert!(parsed.att.is_some());
}
I get this error:
thread 'test' panicked at 'called `Result::unwrap()` on an `Err` value: Error { kind: UnexpectedFormat("word"), locations: ["att"], span: Some(Span) }'
I get the same error if I specify att
, regardless of whether value
is specified.
Is this possible? If so, what structure does darling expect to parse this into?