1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
extern crate proc_macro;
use proc_macro::TokenStream;
use syn::{self, parse_macro_input};
mod attrib;
mod json;
mod shape;
/// Derive macro to allow structs and enums be queried from the database
///
/// This derive can be used on structures with named fields (which correspond
/// to "shapes" in EdgeDB).
///
/// ```rust
/// #[derive(edgedb_client::Queryable)]
/// struct User {
/// first_name: String,
/// age: i32,
/// }
/// ```
///
/// # Field attributes
///
/// ## JSON
///
/// The `#[edgedb(json)]` decodes a field using `serde_json` instead of EdgeDB
/// binary protocol. Useful if some data is stored in the database as JSON, but
/// you need to process it. The underlying type must implement
/// `serde::Deserialize`.
///
/// ```rust
/// # use std::collections::HashMap;
///
/// #[derive(edgedb_client::Queryable)]
/// struct User {
/// #[edgedb(json)]
/// user_notes: HashMap<String, String>,
/// }
/// ```
///
/// # Container attributes
///
/// ## JSON
///
/// The `#[edgedb(json)]` can be used to unpack the structure from the JSON.
/// The underlying type must implement `serde::Deserialize`
///
/// ```rust
/// #[derive(edgedb_client::Queryable, serde::Deserialize)]
/// #[edgedb(json)]
/// struct JsonData {
/// field1: String,
/// field2: u32,
/// }
/// ```
#[proc_macro_derive(Queryable, attributes(edgedb))]
pub fn edgedb_queryable(input: TokenStream) -> TokenStream {
let s = parse_macro_input!(input as syn::Item);
match derive(&s) {
Ok(stream) => stream.into(),
Err(e) => e.to_compile_error().into(),
}
}
fn derive(item: &syn::Item) -> syn::Result<proc_macro2::TokenStream> {
let attrs = match item {
syn::Item::Struct(s) => &s.attrs,
syn::Item::Enum(e) => &e.attrs,
_ => {
return Err(syn::Error::new_spanned(item,
"can only derive Queryable for structs and enums"
));
}
};
let attrs = attrib::ContainerAttrs::from_syn(&attrs)?;
if attrs.json {
json::derive(item)
} else {
match item {
syn::Item::Struct(s) => shape::derive_struct(s),
_ => {
return Err(syn::Error::new_spanned(item,
"can only derive Queryable for a struct in non-JSON mode"
));
}
}
}
}