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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#![cfg_attr(feature = "unstable", feature(proc_macro_span))]
extern crate proc_macro;
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::{quote, ToTokens};
mod annotations;
mod derive;
mod errors;
mod impl_anno_builder;
mod rename_linkme;
mod trait_impls;
mod utils;
use errors::Errors;
#[proc_macro_derive(DbEnum)]
pub fn derive_db_enum(input: TokenStream) -> TokenStream {
derive::db_enum(input.into()).into()
}
#[proc_macro_derive(Model, attributes(rorm))]
pub fn derive_model(input: TokenStream) -> TokenStream {
derive::model(input.into()).into()
}
#[proc_macro_derive(Patch, attributes(rorm))]
pub fn derive_patch(input: TokenStream) -> TokenStream {
derive::patch(input.into()).into()
}
#[doc(hidden)]
#[proc_macro_attribute]
pub fn rename_linkme(_args: TokenStream, item: TokenStream) -> TokenStream {
let mut item = syn::parse_macro_input!(item as syn::ItemStatic);
rename_linkme::rename_expr(&mut item.expr);
item.into_token_stream().into()
}
#[proc_macro_attribute]
pub fn rorm_main(args: TokenStream, item: TokenStream) -> TokenStream {
let errors = Errors::new();
let main = syn::parse_macro_input!(item as syn::ItemFn);
let feature =
syn::parse::<syn::LitStr>(args).unwrap_or(syn::LitStr::new("rorm-main", Span::call_site()));
if main.sig.ident != "main" {
errors.push_new(Span::call_site(), "only allowed on main function");
}
(if errors.is_empty() {
quote! {
#[cfg(feature = #feature)]
fn main() -> Result<(), String> {
let mut file = ::std::fs::File::create(".models.json").map_err(|err| err.to_string())?;
::rorm::write_models(&mut file)?;
return Ok(());
}
#[cfg(not(feature = #feature))]
#main
}
} else {
quote! {
#errors
#main
}
}).into()
}
#[doc(hidden)]
#[proc_macro]
pub fn impl_annotations_builder(args: TokenStream) -> TokenStream {
impl_anno_builder::impl_anno_builder(args.into()).into()
}