near-contract-tools-macros 0.7.2

Macros for `near-contract-tools`
Documentation
use darling::FromDeriveInput;
use proc_macro2::TokenStream;
use quote::quote;
use syn::Expr;

#[derive(Debug, FromDeriveInput)]
#[darling(attributes(owner), supports(struct_named))]
pub struct OwnerMeta {
    pub storage_key: Option<Expr>,

    pub generics: syn::Generics,
    pub ident: syn::Ident,

    // crates
    #[darling(rename = "crate", default = "crate::default_crate_name")]
    pub me: syn::Path,
    #[darling(default = "crate::default_near_sdk")]
    pub near_sdk: syn::Path,
}

pub fn expand(meta: OwnerMeta) -> Result<TokenStream, darling::Error> {
    let OwnerMeta {
        storage_key,
        ident,
        generics,

        me,
        near_sdk,
    } = meta;

    let (imp, ty, wher) = generics.split_for_impl();

    let root = storage_key.map(|storage_key| {
        quote! {
            fn root() -> #me::slot::Slot<()> {
                #me::slot::Slot::root(#storage_key)
            }
        }
    });

    Ok(quote! {
        impl #imp #me::owner::Owner for #ident #ty #wher {
            #root
        }

        #[#near_sdk::near_bindgen]
        impl #imp #me::owner::OwnerExternal for #ident #ty #wher {
            fn own_get_owner(&self) -> Option<#near_sdk::AccountId> {
                <Self as #me::owner::Owner>::slot_owner().read()
            }

            fn own_get_proposed_owner(&self) -> Option<#near_sdk::AccountId> {
                <Self as #me::owner::Owner>::slot_proposed_owner().read()
            }

            #[payable]
            fn own_renounce_owner(&mut self) {
                #near_sdk::assert_one_yocto();
                self.renounce_owner()
            }

            #[payable]
            fn own_propose_owner(&mut self, account_id: Option<#near_sdk::AccountId>) {
                #near_sdk::assert_one_yocto();
                self.propose_owner(account_id);
            }

            #[payable]
            fn own_accept_owner(&mut self) {
                #near_sdk::assert_one_yocto();
                self.accept_owner();
            }
        }
    })
}