I’m trying to implement a derive trait that would look like this:
#[derive(Cacheable)]
#[cacheable(directory = "path/in/cache")]
pub struct Something {
#[cacheable(id)]
pub id: String
}
Where the id
is just a field that is used to uniquely store the data. Only one of the fields should be marked with id
and it doesn’t have any data associated with it. We just need the name of the field. e.g. the trait has a fn id() -> String { return self.#id_field.to_string() }
I have it so that I can extract the directory
using the deluxe library but I can’t figure out how to get the id
tag.
fn cacheable_derive_impl(
item: proc_macro2::TokenStream,
) -> deluxe::Result<proc_macro2::TokenStream> {
// parse
let mut ast: syn::DeriveInput = syn::parse2(item)?;
// extract struct attributes
let CacheableAttributes { directory } = deluxe::extract_attributes(&mut ast)?;
// extract field attributes
// Need to get the single field marked with the id attribute
let id = if let syn::Data::Struct(s) = &mut ast.data //??
...
}
All of the examples I have found seem to be trying to extract a value for the attribute, or else have the attribute on all the fields.
So the question is, how can I get the field name and use it in the definition?