Skip to main content

uom_derive/
lib.rs

1use proc_macro::{self, TokenStream};
2use quote::quote;
3use syn::{parse_macro_input, DeriveInput};
4use darling::FromDeriveInput;
5
6#[derive(FromDeriveInput, Default)]
7#[darling(default, attributes(uom), forward_attrs(allow, doc, cfg))]
8struct Opts {
9    label: Option<syn::Path>,
10}
11
12#[proc_macro_derive(Uom, attributes(uom))]
13pub fn derive(input: TokenStream) -> TokenStream {
14    let input = parse_macro_input!(input);
15    let opts = Opts::from_derive_input(&input).expect("Wrong options");
16    let DeriveInput { ident, .. } = input;
17    let uom = match opts.label {
18        Some(path) => quote! {
19            fn uom() -> String {
20                format!(stringify!(#path))
21            }
22        },
23        None => quote! {
24            fn uom() -> String {
25                format!(stringify!(#ident))
26            }
27        },
28    };
29    let output = quote! {
30        impl Uom for #ident {
31            #uom
32        }
33    };
34    output.into()
35}