Adds a way to skip
This commit is contained in:
parent
854020118b
commit
3b9bd39a5a
|
|
@ -12,72 +12,97 @@
|
|||
// use proc_macro::TokenStream;
|
||||
// use quote::quote;
|
||||
// use syn::{Data, DeriveInput, Fields, parse_macro_input};
|
||||
#![feature(coverage_attribute)]
|
||||
extern crate proc_macro;
|
||||
|
||||
use proc_macro::{Span, TokenStream};
|
||||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::DeriveInput;
|
||||
use syn::Ident;
|
||||
use syn::parse_macro_input;
|
||||
use syn::{Data, Fields};
|
||||
use syn::{Data, DeriveInput, Fields, parse_macro_input};
|
||||
|
||||
#[coverage(off)]
|
||||
#[proc_macro_derive(FieldsAsSlice, attributes(INTEGER, NULL, REAL, TEXT, BLOB))]
|
||||
/// The macro can now be used on structs that have a mix of the
|
||||
/// following field‑level attributes:
|
||||
/// * INTEGER, NULL, REAL, TEXT, BLOB → SQLite column type
|
||||
/// * SKIP → omit this field from the slice
|
||||
///
|
||||
/// Example usage
|
||||
///
|
||||
/// ```ignore
|
||||
/// #[derive(FieldsAsSlice)]
|
||||
/// struct Person {
|
||||
/// #[INTEGER] id: i32,
|
||||
/// #[TEXT] name: String,
|
||||
/// #[SKIP] secret_code: String, // will not appear in `attributes_slice()`
|
||||
/// }
|
||||
/// ```
|
||||
#[proc_macro_derive(FieldsAsSlice, attributes(INTEGER, NULL, REAL, TEXT, BLOB, SKIP))]
|
||||
pub fn derive_iterable(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
|
||||
let struct_name = input.ident;
|
||||
|
||||
// collect named fields only
|
||||
let fields = match input.data {
|
||||
Data::Struct(data_struct) => {
|
||||
// let list_attrs = input.attrs.iter().map(|v| v.path());
|
||||
match data_struct.fields {
|
||||
Fields::Named(fields_named) => fields_named.named,
|
||||
_ => panic!("Only structs with named fields are supported"),
|
||||
}
|
||||
}
|
||||
Data::Struct(data_struct) => match data_struct.fields {
|
||||
Fields::Named(f_named) => f_named.named,
|
||||
_ => panic!("Only structs with named fields are supported"),
|
||||
},
|
||||
_ => panic!("Only structs are supported"),
|
||||
};
|
||||
|
||||
let fields_iter = fields.iter().map(|field| {
|
||||
let field_ident = &field.ident;
|
||||
// For each field: * skip if `#[SKIP]` is present
|
||||
// * otherwise find the first of the type attributes, or default to TEXT
|
||||
let fields_iter = fields.iter().filter_map(|field| {
|
||||
// 1. Skip any field that has a SKIP attribute.
|
||||
if field.attrs.iter().any(|a| a.path().is_ident("SKIP")) {
|
||||
return None;
|
||||
}
|
||||
|
||||
// 2. Determine the SQLite type for this field
|
||||
let val = field
|
||||
.attrs
|
||||
.iter()
|
||||
.map(|v| v.path().get_ident())
|
||||
.next()
|
||||
.flatten()
|
||||
.filter_map(|attr| attr.path().get_ident())
|
||||
.find(|ident| {
|
||||
matches!(
|
||||
ident.to_string().as_str(),
|
||||
"INTEGER" | "NULL" | "REAL" | "TEXT" | "BLOB"
|
||||
)
|
||||
})
|
||||
.cloned()
|
||||
.unwrap_or_else(|| syn::Ident::new("TEXT", Span::call_site().into()))
|
||||
.unwrap_or_else(|| syn::Ident::new("TEXT", proc_macro::Span::call_site().into()))
|
||||
.to_string();
|
||||
let field_name = field_ident.as_ref().unwrap().to_string();
|
||||
quote! {
|
||||
(#field_name, #val)
|
||||
}
|
||||
|
||||
// 3. Get the field name (it is guaranteed to be Some for named fields)
|
||||
let field_name = field
|
||||
.ident
|
||||
.as_ref()
|
||||
.expect("Expected a named field")
|
||||
.to_string();
|
||||
|
||||
// Produce the tokens that will end up in the generated slice.
|
||||
Some(quote! { (#field_name, #val) })
|
||||
});
|
||||
|
||||
let name_of_static_str = struct_name.to_string().to_ascii_uppercase() + "_ATTR_SLICE";
|
||||
let name_of_static = Ident::new(&name_of_static_str, Span::call_site().into());
|
||||
// Build a unique static name for the slice.
|
||||
let const_name_str = format!(
|
||||
"{}_ATTR_SLICE",
|
||||
struct_name.to_string().to_ascii_uppercase()
|
||||
);
|
||||
let const_name = syn::Ident::new(&const_name_str, proc_macro::Span::call_site().into());
|
||||
|
||||
// Finally emit the implementation
|
||||
let expanded = quote! {
|
||||
/// Derived implementation of a (attribute, type) slice
|
||||
/// allowing a constant time serialisation & deserialisation into rusqlite
|
||||
const #name_of_static: &[(&str, &str)] = &[
|
||||
/// Generated slice containing `(field_name, sqlite_type)` pairs.
|
||||
///
|
||||
/// The slice is `static` so it can be used in zero‑alloc scenarios.
|
||||
const #const_name: &[(&str, &str)] = &[
|
||||
#(#fields_iter),*
|
||||
];
|
||||
|
||||
impl FieldsAsSlice for #struct_name {
|
||||
/// Gives a precompiled slice of the &[(fields: &str, sqlite_type: &str)]
|
||||
fn attributes_slice<'a>() -> &'static [(&'static str, &'static str)] {
|
||||
#name_of_static
|
||||
&#const_name
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
TokenStream::from(expanded)
|
||||
}
|
||||
|
||||
// As an hashmap (incorrect as of no ordering)
|
||||
// let mut h = std::collections::HashMap::new();
|
||||
// #(h.insert(#fields_iter);)*
|
||||
// h
|
||||
|
|
|
|||
Loading…
Reference in New Issue