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
116
117
118
119
120
121
122
123
#![warn(warnings)]
mod composite;
mod entity;
mod r#enum;
mod params;
mod symbol;
#[proc_macro_derive(Composite, attributes(elephantry))]
pub fn composite_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let ast = syn::parse(input).unwrap();
composite::impl_macro(&ast)
.unwrap_or_else(syn::Error::into_compile_error)
.into()
}
#[proc_macro_derive(Entity, attributes(elephantry))]
pub fn entity_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let ast = syn::parse(input).unwrap();
entity::impl_macro(&ast)
.unwrap_or_else(syn::Error::into_compile_error)
.into()
}
#[proc_macro_derive(Enum, attributes(elephantry))]
pub fn enum_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let ast = syn::parse(input).unwrap();
r#enum::impl_macro(&ast)
.unwrap_or_else(syn::Error::into_compile_error)
.into()
}
pub(crate) fn check_type(ty: &syn::Type) -> syn::Result<()> {
let features = vec![
#[cfg(feature = "bit")]
"bit",
#[cfg(feature = "date")]
"date",
#[cfg(feature = "geo")]
"geo",
#[cfg(feature = "json")]
"json",
#[cfg(feature = "multirange")]
"multirange",
#[cfg(feature = "net")]
"net",
#[cfg(feature = "numeric")]
"numeric",
#[cfg(feature = "time")]
"time",
#[cfg(feature = "uuid")]
"uuid",
#[cfg(feature = "xml")]
"xml",
];
let types = [
("bit", "bit_vec::BitVec"),
("bit", "u8"),
("date", "chrono::DateTime"),
("date", "chrono::NaiveDate"),
("date", "chrono::NaiveDateTime"),
("date", "elephantry::Interval"),
("geo", "elephantry::Box"),
("geo", "elephantry::Circle"),
("geo", "elephantry::Line"),
("geo", "elephantry::Path"),
("geo", "elephantry::Point"),
("geo", "elephantry::Polygon"),
("geo", "elephantry::Segment"),
("json", "serde_json::value::Value"),
("multirange", "elephantry::Multirange"),
("net", "ipnetwork::IpNetwork"),
("net", "macaddr::MacAddr6"),
("net", "macaddr::MacAddr8"),
("net", "std::net::IpAddr"),
("numeric", "bigdecimal::BigDecimal"),
("time", "elephantry::Time"),
("time", "elephantry::TimeTz"),
("uuid", "uuid::Uuid"),
("xml", "xmltree::Element"),
];
for (feature, feature_ty) in &types {
if !features.contains(feature) && ty == &syn::parse_str(feature_ty).unwrap() {
return error(
ty,
&format!(
"Enable '{}' feature to use the type `{}` in this entity",
feature, feature_ty
),
);
}
}
Ok(())
}
pub(crate) fn error<R>(ast: &dyn quote::ToTokens, message: &str) -> syn::Result<R> {
Err(syn::Error::new_spanned(ast, message))
}