-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfragment.rs
201 lines (187 loc) · 5.89 KB
/
fragment.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
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
use darling::{FromDeriveInput, FromField, FromMeta, FromVariant, ast::Data};
use proc_macro2::{Ident, TokenStream, TokenTree};
use quote::{ToTokens, format_ident, quote};
use syn::{
Attribute, DeriveInput, Generics, Meta, MetaList, Path, Type, Visibility, WherePredicate,
parse_quote,
};
#[derive(FromMeta)]
struct PathOverrides {
#[darling(default = "PathOverrides::default_fragment")]
fragment: Path,
#[darling(default = "PathOverrides::default_result")]
result: Path,
}
impl std::default::Default for PathOverrides {
fn default() -> Self {
Self {
fragment: Self::default_fragment(),
result: Self::default_result(),
}
}
}
impl PathOverrides {
fn default_fragment() -> Path {
parse_quote!(::stackable_operator::config::fragment)
}
fn default_result() -> Path {
parse_quote!(::core::result)
}
}
#[derive(FromDeriveInput)]
#[darling(attributes(fragment), forward_attrs(fragment_attrs, doc))]
pub struct FragmentInput {
ident: Ident,
generics: Generics,
data: Data<FragmentVariant, FragmentField>,
attrs: Vec<Attribute>,
#[darling(default)]
path_overrides: PathOverrides,
#[darling(default)]
bound: Option<Vec<WherePredicate>>,
}
fn split_by_comma(tokens: TokenStream) -> Vec<TokenStream> {
let mut iter = tokens.into_iter().fuse().peekable();
let mut groups = Vec::new();
while iter.peek().is_some() {
groups.push(
iter.by_ref()
.take_while(
|token| !matches!(token, TokenTree::Punct(punct) if punct.as_char() == ','),
)
.collect(),
);
}
groups
}
enum ExtractAttrsError {
InvalidAttrForm,
}
impl ExtractAttrsError {
fn into_compile_error(self) -> TokenStream {
match self {
Self::InvalidAttrForm => quote! {
compile_error!("`#[fragment_attrs]` only takes list-form parameters");
},
}
}
}
fn extract_forwarded_attrs(attrs: &[Attribute]) -> Result<TokenStream, ExtractAttrsError> {
attrs
.iter()
.flat_map(|attr| {
if attr.path().is_ident("fragment_attrs") {
match &attr.meta {
Meta::List(MetaList { tokens, .. }) => {
split_by_comma(tokens.clone()).into_iter().map(Ok).collect()
}
_ => vec![Err(ExtractAttrsError::InvalidAttrForm)],
}
} else if attr.path().is_ident("doc") {
vec![Ok(attr.meta.to_token_stream())]
} else {
Vec::new()
}
})
.map(|attr| attr.map(|attr| quote! { #[#attr] }))
.collect::<Result<TokenStream, ExtractAttrsError>>()
}
#[derive(Debug, FromVariant)]
struct FragmentVariant {}
#[derive(Debug, FromField)]
#[darling(attributes(fragment), forward_attrs(fragment_attrs, doc))]
struct FragmentField {
vis: Visibility,
ident: Option<Ident>,
ty: Type,
attrs: Vec<Attribute>,
}
pub fn derive(input: DeriveInput) -> TokenStream {
let FragmentInput {
ident,
data,
attrs,
mut generics,
bound,
path_overrides:
PathOverrides {
fragment: fragment_mod,
result: result_mod,
},
} = match FragmentInput::from_derive_input(&input) {
Ok(input) => input,
Err(err) => return err.write_errors(),
};
let fields = match data {
Data::Enum(_) => {
return quote! {
compile_error!("`#[derive(Fragment)]` does not currently support enums");
};
}
Data::Struct(fields) => fields.fields,
};
let fragment_ident = format_ident!("{ident}Fragment");
let fragment_fields = fields
.iter()
.map(
|FragmentField {
vis,
ident,
ty,
attrs,
}| {
let attrs = match extract_forwarded_attrs(attrs) {
Ok(x) => x,
Err(err) => return err.into_compile_error(),
};
quote! { #attrs #vis #ident: <#ty as #fragment_mod::FromFragment>::Fragment, }
},
)
.collect::<TokenStream>();
let from_fragment_fields = fields
.iter()
.map(
|FragmentField {
vis: _,
ident,
ty: _,
attrs: _,
}| {
let ident_name = ident.as_ref().map(ToString::to_string);
quote! {
#ident: {
let validator = validator.field(&#ident_name);
#fragment_mod::FromFragment::from_fragment(fragment.#ident, validator)?
},
}
},
)
.collect::<TokenStream>();
let attrs = match extract_forwarded_attrs(&attrs) {
Ok(x) => x,
Err(err) => return err.into_compile_error(),
};
if let Some(bound) = bound {
let where_clause = generics.make_where_clause();
where_clause.predicates.extend(bound);
}
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
quote! {
#attrs
pub struct #fragment_ident #generics #where_clause {
#fragment_fields
}
impl #impl_generics #fragment_mod::FromFragment for #ident #ty_generics #where_clause {
type Fragment = #fragment_ident #ty_generics;
type RequiredFragment = #fragment_ident #ty_generics;
fn from_fragment(
fragment: Self::Fragment,
validator: #fragment_mod::Validator,
) -> #result_mod::Result<Self, #fragment_mod::ValidationError> {
#result_mod::Result::Ok(Self {
#from_fragment_fields
})
}
}
}
}