#[macro_use]
extern crate darling;
extern crate syn;
use darling::{FromDeriveInput, FromMeta};
use syn::parse_str;
#[derive(Debug, FromDeriveInput)]
#[darling(attributes(my_trait), map = "MyInputReceiver::autocorrect")]
pub struct MyInputReceiver {
name: String,
frequency: darling::Result<i64>,
amplitude: Result<u64, syn::Meta>,
}
impl MyInputReceiver {
fn autocorrect(self) -> Self {
let Self {
name,
frequency,
amplitude,
} = self;
let amplitude = match amplitude {
Ok(amp) => amp,
Err(mi) => {
let val: i64 = if let Ok(v) = FromMeta::from_meta(&mi) {
v
} else {
panic!(format!("amplitude should have been an integer"))
};
val.abs() as u64
}
};
Self {
name: name,
frequency: frequency,
amplitude: Ok(amplitude),
}
}
}
fn main() {
let input = r#"#[derive(MyTrait)]
#[my_trait(name="Jon", amplitude = "-1", frequency = "1")]
pub struct Foo;"#;
let parsed = parse_str(input).unwrap();
let receiver = MyInputReceiver::from_derive_input(&parsed).unwrap();
println!(
r#"
INPUT:
{}
PARSED AS:
{:?}
"#,
input, receiver
);
}