Skip to main content

annotate_derive/
lib.rs

1use proc_macro2::TokenStream;
2use syn::spanned::Spanned;
3use syn::{Item, Result, parse};
4
5pub(crate) use attributes::*;
6
7use crate::function::AnnotatedFunction;
8use crate::module::AnnotatedModule;
9
10mod attributes;
11mod function;
12mod module;
13
14#[proc_macro_attribute]
15pub fn pragma(
16    attr: proc_macro::TokenStream,
17    item: proc_macro::TokenStream,
18) -> proc_macro::TokenStream {
19    let expanded = expand(attr, item).unwrap_or_else(|error| error.to_compile_error());
20    expanded.into()
21}
22
23fn expand(attr: proc_macro::TokenStream, item: proc_macro::TokenStream) -> Result<TokenStream> {
24    let attributes = Attributes::parse(attr)?;
25    let item = parse::<Item>(item)?;
26
27    let expanded = match item {
28        Item::Fn(item_fn) => AnnotatedFunction::new(item_fn, attributes).expand(),
29        Item::Mod(item_mod) => AnnotatedModule::new(item_mod, attributes).expand(),
30        _ => syn::Error::new(
31            item.span(),
32            "Pragmas are not supported for this type of construct",
33        )
34        .to_compile_error(),
35    };
36
37    Ok(expanded)
38}