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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
use inflector::Inflector;
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{format_ident, quote};
use std::convert::*;
use super::*;
pub fn derive_holder(ident: &syn::Ident, st: &syn::DataStruct, attr: &HolderAttr) -> TokenStream2 {
let name = ident.to_string().to_screaming_snake_case();
let holder_ident = as_holder_ident(ident);
let def_holder_tt = def_holder(ident, st);
let impl_holder_tt = impl_holder(ident, attr, st);
let impl_entity_table_tt = impl_entity_table(ident, attr);
if attr.generate_deserialize {
let def_visitor_tt = def_visitor(&holder_ident, &name, st);
let impl_deserialize_tt = impl_deserialize(&holder_ident, &name, st);
let impl_with_visitor_tt = impl_with_visitor(ident);
quote! {
#def_holder_tt
#impl_holder_tt
#impl_entity_table_tt
#def_visitor_tt
#impl_deserialize_tt
#impl_with_visitor_tt
}
} else {
quote! {
#def_holder_tt
#impl_holder_tt
#impl_entity_table_tt
}
}
}
pub fn def_holder(ident: &syn::Ident, st: &syn::DataStruct) -> TokenStream2 {
let holder_ident = as_holder_ident(ident);
let FieldEntries { holder_types, .. } = FieldEntries::parse(st);
quote! {
#[derive(Debug, Clone, PartialEq)]
pub struct #holder_ident(#(pub #holder_types),*);
}
}
pub fn impl_holder(ident: &syn::Ident, table: &HolderAttr, st: &syn::DataStruct) -> TokenStream2 {
let name = ident.to_string().to_screaming_snake_case();
let holder_ident = as_holder_ident(ident);
let FieldEntries {
holder_types,
into_owned,
} = FieldEntries::parse(st);
let HolderAttr { table, .. } = table;
let tuple_len = holder_types.len();
let table_arg = table_arg();
let ruststep = ruststep_crate();
quote! {
#[automatically_derived]
impl #ruststep::tables::IntoOwned for #holder_ident {
type Table = #table;
type Owned = #ident;
fn into_owned(self, #table_arg: &Self::Table) -> #ruststep::error::Result<Self::Owned> {
Ok(#ident ( #(#into_owned),* ))
}
}
#[automatically_derived]
impl #ruststep::tables::Holder for #holder_ident {
fn name() -> &'static str {
#name
}
fn attr_len() -> usize {
#tuple_len
}
}
}
}
pub fn impl_entity_table(ident: &syn::Ident, table: &HolderAttr) -> TokenStream2 {
let HolderAttr { table, field, .. } = table;
let holder_ident = as_holder_ident(ident);
let ruststep = ruststep_crate();
quote! {
#[automatically_derived]
impl #ruststep::tables::EntityTable<#holder_ident> for #table {
fn get_owned(&self, entity_id: u64) -> #ruststep::error::Result<#ident> {
#ruststep::tables::get_owned(self, &self.#field, entity_id)
}
fn owned_iter<'table>(&'table self) -> Box<dyn Iterator<Item = #ruststep::error::Result<#ident>> + 'table> {
#ruststep::tables::owned_iter(self, &self.#field)
}
}
}
}
fn def_visitor(ident: &syn::Ident, name: &str, st: &syn::DataStruct) -> TokenStream2 {
let visitor_ident = as_visitor_ident(ident);
let FieldEntries { holder_types, .. } = FieldEntries::parse(st);
let attr_len = holder_types.len();
let attributes = (0..attr_len)
.map(|i| format_ident!("a_{}", i))
.collect::<Vec<_>>();
let serde = serde_crate();
quote! {
#[doc(hidden)]
pub struct #visitor_ident;
#[automatically_derived]
impl<'de> #serde::de::Visitor<'de> for #visitor_ident {
type Value = #ident;
fn expecting(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(formatter, #name)
}
fn visit_seq<A>(self, mut seq: A) -> ::std::result::Result<Self::Value, A::Error>
where
A: #serde::de::SeqAccess<'de>,
{
if let Some(size) = seq.size_hint() {
if size != #attr_len {
use #serde::de::Error;
return Err(A::Error::invalid_length(size, &self));
}
}
#( let #attributes = seq.next_element()?.unwrap(); )*
Ok(#ident ( #(#attributes),* ))
}
fn visit_map<A>(self, mut map: A) -> ::std::result::Result<Self::Value, A::Error>
where
A: #serde::de::MapAccess<'de>,
{
let key: String = map
.next_key()?
.expect("Empty map cannot be accepted as ruststep Holder");
if key != #name {
use #serde::de::{Error, Unexpected};
return Err(A::Error::invalid_value(Unexpected::Other(&key), &self));
}
let value = map.next_value()?;
Ok(value)
}
}
}
}
fn impl_deserialize(ident: &syn::Ident, name: &str, st: &syn::DataStruct) -> TokenStream2 {
let visitor_ident = as_visitor_ident(ident);
let FieldEntries { holder_types, .. } = FieldEntries::parse(st);
let attr_len = holder_types.len();
let serde = serde_crate();
quote! {
#[automatically_derived]
impl<'de> #serde::de::Deserialize<'de> for #ident {
fn deserialize<D>(deserializer: D) -> ::std::result::Result<Self, D::Error>
where
D: #serde::de::Deserializer<'de>,
{
deserializer.deserialize_tuple_struct(#name, #attr_len, #visitor_ident {})
}
}
}
}
fn impl_with_visitor(ident: &syn::Ident) -> TokenStream2 {
let ruststep = ruststep_crate();
let visitor_ident = as_holder_visitor(ident);
let holder_ident = as_holder_ident(ident);
quote! {
#[automatically_derived]
impl #ruststep::tables::WithVisitor for #holder_ident {
type Visitor = #visitor_ident;
fn visitor_new() -> Self::Visitor {
#visitor_ident {}
}
}
}
}
struct FieldEntries {
holder_types: Vec<syn::Type>,
into_owned: Vec<TokenStream2>,
}
impl FieldEntries {
fn parse(st: &syn::DataStruct) -> Self {
let table_arg = table_arg();
let mut holder_types = Vec::new();
let mut into_owned = Vec::new();
for (i, field) in st.fields.iter().enumerate() {
let ft: FieldType = field.ty.clone().try_into().unwrap();
let index = syn::Index::from(i);
let HolderAttr { place_holder, .. } = HolderAttr::parse(&field.attrs);
if place_holder {
match &ft {
FieldType::Path(_) => {
into_owned.push(quote! { self.#index.into_owned(#table_arg)? });
}
FieldType::Optional(_) => {
into_owned.push(quote! { self.#index.map(|holder| holder.into_owned(#table_arg)).transpose()? },);
}
FieldType::List(_) => into_owned.push(quote! {
self.#index
.into_iter()
.map(|v| v.into_owned(#table_arg))
.collect::<::std::result::Result<Vec<_>, _>>()?
}),
FieldType::Boxed(_) => abort_call_site!("Unexpected Box<T>"),
}
holder_types.push(ft.as_holder().as_place_holder().into());
} else {
into_owned.push(quote! { self.#index });
holder_types.push(ft.into());
}
}
FieldEntries {
holder_types,
into_owned,
}
}
}
fn table_arg() -> syn::Ident {
syn::Ident::new("table", Span::call_site())
}