Skip to content

Commit f34ab22

Browse files
committed
Deduplicate a little
1 parent abaed01 commit f34ab22

File tree

3 files changed

+48
-53
lines changed

3 files changed

+48
-53
lines changed

packages/cw-schema-derive/src/expand.rs

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TODO: CLEAN ALL THIS SHIT UP WHAT THE FUCK IS THIS
2+
13
use crate::bail;
24
use proc_macro2::TokenStream;
35
use quote::{format_ident, quote};
@@ -30,9 +32,13 @@ fn case_converter(case: &syn::LitStr) -> syn::Result<Converter> {
3032
Ok(converter)
3133
}
3234

35+
fn maybe_case_converter(case: Option<&syn::LitStr>) -> syn::Result<Converter> {
36+
case.map(case_converter)
37+
.unwrap_or_else(|| Ok(|txt: &str| txt.to_string()))
38+
}
39+
3340
fn ident_adapter(converter: Converter) -> impl Fn(&syn::Ident) -> syn::Ident {
34-
let adapter = move |ident: &syn::Ident| format_ident!("{}", converter(&ident.to_string()));
35-
Box::new(adapter)
41+
move |ident: &syn::Ident| format_ident!("{}", converter(&ident.to_string()))
3642
}
3743

3844
struct SerdeContainerOptions {
@@ -169,37 +175,45 @@ pub struct ContainerMeta {
169175
serde_options: SerdeContainerOptions,
170176
}
171177

178+
fn collect_struct_fields<'a, C>(
179+
converter: &'a C,
180+
crate_path: &'a syn::Path,
181+
fields: &'a syn::FieldsNamed,
182+
) -> impl Iterator<Item = syn::Result<TokenStream>> + 'a
183+
where
184+
C: Fn(&syn::Ident) -> syn::Ident,
185+
{
186+
fields.named.iter().map(move |field| {
187+
let name = converter(field.ident.as_ref().unwrap());
188+
let description = normalize_option(extract_documentation(&field.attrs)?);
189+
let field_ty = &field.ty;
190+
191+
let expanded = quote! {
192+
(
193+
stringify!(#name).into(),
194+
#crate_path::StructProperty {
195+
description: #description,
196+
value: <#field_ty as #crate_path::Schemaifier>::visit_schema(visitor),
197+
}
198+
)
199+
};
200+
201+
Ok(expanded)
202+
})
203+
}
204+
172205
fn expand_enum(mut meta: ContainerMeta, input: DataEnum) -> syn::Result<TokenStream> {
173206
let crate_path = &meta.options.crate_path;
174-
let converter = ident_adapter(
175-
meta.serde_options
176-
.rename_all
177-
.as_ref()
178-
.map(case_converter)
179-
.unwrap_or_else(|| Ok(|txt: &str| txt.to_string()))?,
180-
);
207+
let converter = ident_adapter(maybe_case_converter(
208+
meta.serde_options.rename_all.as_ref(),
209+
)?);
181210

182211
let mut cases = Vec::new();
183212
for variant in input.variants.iter() {
184213
let value = match variant.fields {
185214
syn::Fields::Named(ref fields) => {
186-
let items = fields.named.iter().map(|field| {
187-
let name = converter(field.ident.as_ref().unwrap());
188-
let description = normalize_option(extract_documentation(&field.attrs)?);
189-
let field_ty = &field.ty;
190-
191-
let expanded = quote! {
192-
(
193-
stringify!(#name).into(),
194-
#crate_path::StructProperty {
195-
description: #description,
196-
value: <#field_ty as #crate_path::Schemaifier>::visit_schema(visitor),
197-
}
198-
)
199-
};
200-
201-
Ok(expanded)
202-
}).collect::<syn::Result<Vec<_>>>()?;
215+
let items = collect_struct_fields(&converter, crate_path, fields)
216+
.collect::<syn::Result<Vec<_>>>()?;
203217

204218
quote! {
205219
#crate_path::EnumValue::Named {
@@ -269,13 +283,9 @@ fn expand_enum(mut meta: ContainerMeta, input: DataEnum) -> syn::Result<TokenStr
269283
}
270284

271285
fn expand_struct(mut meta: ContainerMeta, input: DataStruct) -> syn::Result<TokenStream> {
272-
let converter = ident_adapter(
273-
meta.serde_options
274-
.rename_all
275-
.as_ref()
276-
.map(case_converter)
277-
.unwrap_or_else(|| Ok(|txt: &str| txt.to_string()))?,
278-
);
286+
let converter = ident_adapter(maybe_case_converter(
287+
meta.serde_options.rename_all.as_ref(),
288+
)?);
279289

280290
let name = &meta.name;
281291
let description = normalize_option(meta.description.as_ref());
@@ -293,24 +303,9 @@ fn expand_struct(mut meta: ContainerMeta, input: DataStruct) -> syn::Result<Toke
293303
}
294304
} else {
295305
let node_ty = match input.fields {
296-
syn::Fields::Named(named) => {
297-
let items = named.named.iter().map(|field| {
298-
let name = converter(field.ident.as_ref().unwrap());
299-
let description = normalize_option(extract_documentation(&field.attrs)?);
300-
let field_ty = &field.ty;
301-
302-
let expanded = quote! {
303-
(
304-
stringify!(#name).into(),
305-
#crate_path::StructProperty {
306-
description: #description,
307-
value: <#field_ty as #crate_path::Schemaifier>::visit_schema(visitor),
308-
}
309-
)
310-
};
311-
312-
Ok(expanded)
313-
}).collect::<syn::Result<Vec<_>>>()?;
306+
syn::Fields::Named(ref named) => {
307+
let items = collect_struct_fields(&converter, crate_path, named)
308+
.collect::<syn::Result<Vec<_>>>()?;
314309

315310
quote! {
316311
#crate_path::StructType::Named {

packages/cw-schema/tests/basic.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,8 @@ fn roundtrip() {
116116

117117
#[test]
118118
fn can_decode_example() {
119-
/*
120119
let example = include_str!("example.json");
121120
let _: Schema = serde_json::from_str(example).unwrap();
122-
*/
123121
}
124122

125123
#[test]

packages/cw-schema/tests/derive.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(dead_code)] // We never construct these types. Introspection is done at compile time.
2+
13
use cw_schema::Schemaifier;
24

35
#[derive(Schemaifier)]

0 commit comments

Comments
 (0)