use darling::{FromDeriveInput, FromMeta};
use syn::parse_quote;
#[derive(Debug, Default, PartialEq, Eq)]
struct NotFromMeta(String);
fn parser(meta: &syn::Meta) -> darling::Result<NotFromMeta> {
String::from_meta(meta).map(NotFromMeta)
}
#[derive(Debug, FromDeriveInput)]
#[darling(attributes(example))]
struct Example {
#[darling(default, with = parser)]
with: NotFromMeta,
}
#[derive(FromMeta)]
struct Opts {
#[darling(default, with = parser)]
with: NotFromMeta,
}
#[test]
fn parses_via_the_with_callable() {
let input: Example = Example::from_derive_input(&parse_quote! {
#[example(with = "hello")]
struct Example;
})
.unwrap();
assert_eq!(input.with, NotFromMeta("hello".to_string()));
}
#[test]
fn missing_input_uses_default() {
let input: Example = Example::from_derive_input(&parse_quote! {
#[example]
struct Example;
})
.unwrap();
assert_eq!(input.with, NotFromMeta::default());
}
#[test]
fn parses_via_the_with_callable_on_from_meta() {
let opts = Opts::from_meta(&parse_quote!(example(with = "hello"))).unwrap();
assert_eq!(opts.with, NotFromMeta("hello".to_string()));
}
#[test]
fn invalid_expr_is_an_error() {
let err = Example::from_derive_input(&parse_quote! {
#[example(with = pub(crate))]
struct Example;
})
.unwrap_err();
assert_eq!(err.to_string(), "expected an expression at with");
}