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
use crate::Endpoint;
use darling::{
ast::{Data, Fields},
FromDeriveInput, FromField, FromMeta, FromVariant,
};
use proc_macro2::TokenStream;
use quote::quote;
use syn::{GenericArgument, Ident, PathArguments, Type};
#[derive(Debug, FromField, Clone)]
#[darling(attributes(api))]
pub struct ApiField {
pub ident: Option<Ident>,
pub ty: Type,
#[darling(default)]
pub table_skip: bool,
pub format: Option<FieldFormat>,
}
#[derive(Debug, FromMeta, Clone)]
pub enum FieldFormat {
Date {
format: String,
},
}
#[derive(Debug, FromVariant)]
#[darling(attributes(api))]
#[allow(dead_code)]
pub struct ApiVariant {
ident: Ident,
fields: darling::ast::Fields<Type>,
}
#[derive(Debug, FromDeriveInput)]
#[darling(attributes(api, suggest))]
pub struct Api {
pub ident: Ident,
pub data: Data<ApiVariant, ApiField>,
#[darling(default)]
#[darling(multiple)]
pub endpoint: Vec<Endpoint>,
#[darling(rename = "stream")]
#[darling(default)]
pub result_is_stream: bool,
}
#[rustfmt::skip::macros(quote)]
pub fn table_impl<T: Into<ApiField> + Clone>(
struct_ident: &Ident,
data: &Data<ApiVariant, T>,
) -> TokenStream {
let (headers, table_convertions) = match data {
Data::Enum(_) => {
todo!("Générer les entetes et les données pour chaque variant");
}
Data::Struct(Fields { fields, .. }) => {
let headers: Vec<proc_macro2::TokenStream> = fields
.iter()
.filter(|field| {
let field: ApiField = (*field).clone().into();
!field.table_skip
})
.map(|field| {
let field: ApiField = (*field).clone().into();
let f = field
.ident
.as_ref()
.expect("Field without ident")
.to_string();
quote! {#f.to_string()}
})
.collect();
let table_convertions: proc_macro2::TokenStream = fields
.iter()
.filter(|field| {
let field: ApiField = (*field).clone().into();
!field.table_skip
})
.map(|field| {
let field: ApiField = (*field).clone().into();
let fname = field.ident.as_ref().expect("Field without ident");
let unformated_value = if is_option_vec(&field.ty) {
quote!{self.#fname .as_ref().unwrap() .join(", ").replace('\n', "\\n")}
} else if is_option(&field.ty) {
quote! {self.#fname .clone().unwrap_or_default().to_string().replace('\n', "\\n")}
} else if is_vec(&field.ty) {
quote!{self.#fname .join(", ").replace('\n', "\\n")}
} else {
quote! {self.#fname .to_string().replace('\n', "\\n")}
};
let formated_value = if let Some(FieldFormat::Date { format }) = field.format {
quote!(#unformated_value.parse::<chrono::DateTime<chrono::Utc>>()
.into_diagnostic().wrap_err("Can't parse Date")?
.format(#format).to_string()
)
} else {
unformated_value
};
quote!(#formated_value ,)
})
.collect();
(headers, table_convertions)
}
};
quote! {impl crud_api::Api for #struct_ident {
fn to_table_header(&self) -> Vec<String> {
vec![#(#headers),*]
}
fn to_table(&self) -> miette::Result<Vec<String>> {
Ok(vec![#table_convertions])
}
}}
}
fn is_option(ty: &Type) -> bool {
if let Type::Path(s) = ty {
if let Some(x) = s.path.segments.first() {
return x.ident.eq("Option");
}
}
false
}
fn is_vec(ty: &Type) -> bool {
if let Type::Path(s) = ty {
if let Some(x) = s.path.segments.first() {
return x.ident.eq("Vec");
}
}
false
}
fn is_option_vec(ty: &Type) -> bool {
fn strip_type_no_rec(ty: &Type) -> &Type {
if is_option(ty) || is_vec(ty) {
if let Type::Path(s) = ty {
if let Some(segment) = s.path.segments.first() {
if let PathArguments::AngleBracketed(first_arg) = &segment.arguments {
if let GenericArgument::Type(result_type) = first_arg.args.first().unwrap() {
return result_type;
}
}
}
}
}
ty
}
if is_option(ty) {
is_vec(strip_type_no_rec(ty))
} else {
false
}
}