-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathmodelv2.rs
74 lines (60 loc) · 1.97 KB
/
modelv2.rs
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
mod args;
mod codegen;
mod config;
mod identifier;
mod parsing;
mod utils;
use darling::FromDeriveInput as _;
use darling::Result;
use proc_macro2::TokenStream;
use quote::quote;
use syn::DeriveInput;
use args::ModelArgs;
use config::*;
use identifier::Identifier;
pub fn model(input: &DeriveInput) -> Result<TokenStream> {
let model_name = &input.ident;
let model_vis = &input.vis;
let options = ModelArgs::from_derive_input(input)?;
let config = ModelConfig::from_macro_args(options, model_name.clone(), model_vis.clone())?;
let model_impl = config.model_impl();
let row_decl = config.row_decl();
let changeset_decl = config.changeset_decl();
let identifiable_impls = config.identifiable_impls();
let preferred_id_impl = config.preferred_id_impl();
let model_from_row_impl = config.model_from_row_impl();
let changeset_from_model_impl = config.changeset_from_model_impl();
let changeset_builder = config.changeset_builder_impl_block();
let patch_builder = config.patch_builder_impl_block();
let model_impls = config.make_model_traits_impl();
Ok(quote! {
#model_impl
#row_decl
#changeset_decl
#(#identifiable_impls)*
#preferred_id_impl
#model_from_row_impl
#changeset_from_model_impl
#changeset_builder
#patch_builder
#model_impls
})
}
#[cfg(test)]
#[test]
fn test_construction() {
let input = syn::parse_quote! {
#[derive(Clone, Model)]
#[model(table = crate::tables::osrd_infra_document)]
#[model(row(type_name = "DocumentRow", derive(Debug)))]
#[model(changeset(type_name = "DocumentChangeset", public, derive(Debug)))] // fields are public
struct Document {
#[model(column = "id", preferred, primary)]
id_: i64,
#[model(identifier, json)]
content_type: String,
data: Vec<u8>,
}
};
let _ = model(&input).expect("should generate");
}