Skip to main content

tiberius_ng_macros/
lib.rs

1//! A utility proc-macro crate that generates trivial trait implementations used
2//! in the Rust-to-SQL data exchange for tiberius table-valued parameters.
3extern crate proc_macro;
4
5#[macro_use]
6extern crate quote;
7#[macro_use]
8extern crate syn;
9
10use proc_macro::TokenStream;
11
12macro_rules! sp_quote {
13    ($($t:tt)*) => (quote_spanned!(proc_macro2::Span::call_site() => $($t)*))
14}
15
16mod attr;
17mod table_value_param;
18
19/// Generates a trivial implementation of the `TableValueRow` trait.
20///
21/// # Applications
22/// Apply to structs that represent rows of a table-valued parameter.
23///
24/// # Example
25/// ```rust,ignore
26/// # use tiberius::*;
27/// #[derive(TableValueRow)]
28/// pub struct SomeGeoList {
29///   #[colname = "SomeID"]
30///   pub id: i32,
31///   #[colname = "LastSyncIPGeoLat"]
32///   pub lat: Numeric,
33///   #[colname = "LastSyncIPGeoLong"]
34///   pub lon: Numeric,
35/// }
36/// ```
37#[proc_macro_derive(TableValueRow, attributes(colname))]
38pub fn table_value_param(input: TokenStream) -> TokenStream {
39    let ast: syn::DeriveInput = syn::parse(input).expect("Couldn't parse item");
40    let result = match ast.data {
41        syn::Data::Enum(_) => panic!("n/a for enums, makes sense for structs only"),
42        syn::Data::Struct(ref s) => table_value_param::for_struct(&ast, &s.fields),
43        syn::Data::Union(_) => panic!("doesn't work with unions"),
44    };
45    result.into()
46}