squire_derive/lib.rs
1mod column;
2mod common;
3mod param;
4
5use column::ColumnsDerive;
6use darling::FromDeriveInput;
7use param::ParametersDerive;
8use proc_macro::TokenStream;
9
10/// Derive macro for implementing the `Parameters` trait.
11///
12/// # Attributes
13///
14/// - `#[squire(skip)]` - Skip this field when binding parameters
15/// - `#[squire(borrow)]` - Wrap the field in `Borrowed` for zero-copy binding
16/// - `#[squire(bind_with = custom_function)]` - Use a custom binding function
17/// - `#[squire(index = 1)]` - Use a specific parameter index
18/// - `#[squire(rename = other_name)]` - Use a different field name for binding
19#[proc_macro_derive(Parameters, attributes(squire))]
20pub fn derive_parameters(input: TokenStream) -> TokenStream {
21 let input = syn::parse_macro_input!(input as syn::DeriveInput);
22
23 match ParametersDerive::from_derive_input(&input) {
24 Ok(params) => match params.derive() {
25 Ok(tokens) => tokens.into(),
26 Err(err) => err.write_errors().into(),
27 },
28 Err(err) => err.write_errors().into(),
29 }
30}
31
32/// Derive macro for implementing the `Columns` trait.
33///
34/// # Attributes
35///
36/// - `#[squire(skip)]` - Skip this field when fetching columns
37/// - `#[squire(borrow)]` - Fetch the field as `Borrowed` for zero-copy access
38/// - `#[squire(fetch_with = custom_function)]` - Use a custom fetch function
39/// - `#[squire(index = 0)]` - Use a specific column index
40/// - `#[squire(rename = other_name)]` - Use a different field name for column lookup
41/// - `#[squire(result)]` - Unwrap a Result returned by the fetch expression
42#[proc_macro_derive(Columns, attributes(squire))]
43pub fn derive_columns(input: TokenStream) -> TokenStream {
44 let input = syn::parse_macro_input!(input as syn::DeriveInput);
45
46 match ColumnsDerive::from_derive_input(&input) {
47 Ok(columns) => match columns.derive() {
48 Ok(tokens) => tokens.into(),
49 Err(err) => err.write_errors().into(),
50 },
51 Err(err) => err.write_errors().into(),
52 }
53}