ab_contracts_macros_impl/contract/
common.rs

1use proc_macro2::{Ident, Literal, TokenStream};
2use quote::quote;
3use syn::{Error, Type};
4
5pub(super) fn extract_ident_from_type(ty: &Type) -> Option<&Ident> {
6    let Type::Path(type_path) = ty else {
7        return None;
8    };
9    type_path.path.get_ident()
10}
11
12pub(super) fn derive_ident_metadata(ident: &Ident) -> Result<TokenStream, Error> {
13    let ident_string = ident.to_string();
14    let ident_bytes = ident_string.as_bytes();
15    let ident_bytes_len = u8::try_from(ident_bytes.len()).map_err(|_error| {
16        Error::new(
17            ident.span(),
18            format!(
19                "Name of the field must not be more than {} bytes in length",
20                u8::MAX
21            ),
22        )
23    })?;
24    let ident_bytes = Literal::byte_string(ident_bytes);
25    let ident_bytes_len = Literal::u8_unsuffixed(ident_bytes_len);
26
27    Ok(quote! {
28        &[#ident_bytes_len], #ident_bytes
29    })
30}