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
use inflector::Inflector;
use proc_macro2::TokenStream as TokenStream2;
use proc_macro_error::{abort_call_site, OptionExt};
use quote::quote;
use crate::common::ruststep_crate;
pub fn derive_table_init(ast: &syn::DeriveInput) -> TokenStream2 {
let ident = &ast.ident;
match &ast.data {
syn::Data::Struct(st) => match st.fields {
syn::Fields::Named(_) => entity_impl_table_init(ident, st),
syn::Fields::Unnamed(_) => tuple_impl_table_init(ident, st),
syn::Fields::Unit => panic!("Unit struct is not supported."),
},
_ => abort_call_site!("Only struct is supprted currently"),
}
}
fn entity_impl_table_init(ident: &syn::Ident, st: &syn::DataStruct) -> TokenStream2 {
let mut table_names = Vec::new();
let mut entity_names = Vec::new();
for field in &st.fields {
let ident = field.ident.as_ref().expect_or_abort("unreachable!");
let name = ident.to_string().to_screaming_snake_case();
table_names.push(ident);
entity_names.push(name);
}
assert_eq!(table_names.len(), entity_names.len());
let ruststep = ruststep_crate();
quote! {
#[automatically_derived]
impl #ruststep::tables::TableInit for #ident {
fn append_data_section(
&mut self,
data_sec: &#ruststep::ast::DataSection
) -> #ruststep::error::Result<()> {
use #ruststep::{error::Error, tables::insert_record, ast::EntityInstance};
for entity in &data_sec.entities {
match entity {
EntityInstance::Simple { id, record } => match record.name.as_str() {
#(
#entity_names => insert_record(&mut self.#table_names, *id, record)?,
)*
_ => {
return Err(Error::UnknownEntityName {
entity_name: record.name.clone(),
schema: "".to_string(),
});
}
},
EntityInstance::Complex { .. } => {
unimplemented!("Complex entity is not supported")
}
}
}
Ok(())
}
}
#[automatically_derived]
impl ::std::str::FromStr for #ident {
type Err = #ruststep::error::Error;
fn from_str(input: &str) -> #ruststep::error::Result<Self> {
use #ruststep::{tables::TableInit, ast::DataSection};
let data_sec = DataSection::from_str(input)?;
Ok(Self::from_data_section(&data_sec)?)
}
}
}
}
fn tuple_impl_table_init(ident: &syn::Ident, st: &syn::DataStruct) -> TokenStream2 {
let mut table_names = Vec::new();
let mut entity_names = Vec::new();
for field in &st.fields {
let ty = &field.ty;
let ident = quote! { #ty }.to_string().to_screaming_snake_case();
table_names.push(ident.clone());
entity_names.push(ident);
}
assert_eq!(table_names.len(), entity_names.len());
let ruststep = ruststep_crate();
quote! {
#[automatically_derived]
impl #ruststep::tables::TableInit for #ident {
fn append_data_section(
&mut self,
data_sec: &#ruststep::ast::DataSection
) -> #ruststep::error::Result<()> {
use #ruststep::{error::Error, tables::insert_record, ast::EntityInstance};
for entity in &data_sec.entities {
match entity {
EntityInstance::Simple { id, record } => match record.name.as_str() {
#(
#entity_names => insert_record(&mut self.#table_names, *id, record)?,
)*
_ => {
return Err(Error::UnknownEntityName {
entity_name: record.name.clone(),
schema: "".to_string(),
});
}
},
EntityInstance::Complex { .. } => {
unimplemented!("Complex entity is not supported")
}
}
}
Ok(())
}
}
#[automatically_derived]
impl ::std::str::FromStr for #ident {
type Err = #ruststep::error::Error;
fn from_str(input: &str) -> #ruststep::error::Result<Self> {
use #ruststep::{tables::TableInit, ast::DataSection};
let data_sec = DataSection::from_str(input)?;
Ok(Self::from_data_section(&data_sec)?)
}
}
}
}