fromsoftware_shared_macros/
lib.rs

1use proc_macro::TokenStream;
2use quote::quote;
3use syn::{ItemStruct, LitStr};
4
5/// Annotates a struct as a Dantelion2 singleton to be looked up using a single
6/// string argument.
7///
8/// This is only guaranteed to make the struct work with the
9/// `fromsoftware_shared::singleton::get_instance` function. Any other added
10/// functionality is considered an implementation detail and shouldn't be relied
11/// upon.
12#[proc_macro_attribute]
13pub fn singleton(args: TokenStream, input: TokenStream) -> TokenStream {
14    let input_struct: ItemStruct = syn::parse_macro_input!(input as ItemStruct);
15    let input_struct_ident = input_struct.ident.clone();
16    let dlrf_name = syn::parse_macro_input!(args as LitStr).value();
17
18    TokenStream::from(quote! {
19        #input_struct
20
21        impl ::from_singleton::FromSingleton for #input_struct_ident {
22            fn name() -> ::std::borrow::Cow<'static, str> {
23                ::std::borrow::Cow::Borrowed(#dlrf_name)
24            }
25        }
26    })
27}