Skip to content

Commit

Permalink
fixes #170
Browse files Browse the repository at this point in the history
  • Loading branch information
oscartbeaumont committed Nov 8, 2023
1 parent 5704d85 commit 821fd62
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
60 changes: 47 additions & 13 deletions macros/src/type/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,56 @@ pub fn parse_struct(
data.fields.span(),
"specta: unit structs cannot be transparent",
));
} else if data.fields.len() != 1 {
return Err(syn::Error::new(
data.fields.span(),
"specta: transparent structs must have exactly one field",
));
}

let field = data
.fields
.iter()
.next()
.expect("unreachable: we just checked this!");
let field_attrs = decode_field_attrs(field)?;
let (field_ty, field_attrs) = match data.fields {
Fields::Named(_) => {
let fields = data
.fields
.iter()
.map(|field| decode_field_attrs(field).map(|v| (field.ty.clone(), v)))
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.filter(|(_, attrs)| !attrs.skip)
.collect::<Vec<_>>();

let field_ty = field_attrs.r#type.as_ref().unwrap_or(&field.ty);
if fields.len() != 1 {
return Err(syn::Error::new(
data.fields.span(),
"specta: transparent structs must have exactly one field",
));
}

fields.into_iter().next().expect("fields.len() != 1")
}
Fields::Unnamed(_) => {
let fields = data
.fields
.iter()
.map(|field| decode_field_attrs(field).map(|v| (field.ty.clone(), v)))
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.filter(|(_, attrs)| !attrs.skip)
.collect::<Vec<_>>();

if fields.len() != 1 {
return Err(syn::Error::new(
data.fields.span(),
"specta: transparent structs must have exactly one field",
));
}

fields.into_iter().next().expect("fields.len() != 1")
}
Fields::Unit => {
return Err(syn::Error::new(
data.fields.span(),
"specta: transparent structs must have exactly one field",
));
}
};

let field_ty = field_attrs.r#type.as_ref().unwrap_or(&field_ty);

let ty = construct_datatype(
format_ident!("ty"),
Expand Down Expand Up @@ -152,7 +187,6 @@ pub fn parse_struct(
.then(|| quote!(true))
.unwrap_or(parent_inline.clone());


let ty = field_attrs.skip.then(|| Ok(quote!(None)))
.unwrap_or_else(|| {
construct_datatype(
Expand Down
12 changes: 12 additions & 0 deletions tests/serde/skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ pub enum SkipNamedFieldInVariant {
},
}

// https://github.com/oscartbeaumont/specta/issues/170
#[derive(Type)]
#[specta(transparent, export = false)]
pub struct TransparentWithSkip((), #[specta(skip)] String);

// https://github.com/oscartbeaumont/specta/issues/170
#[derive(Type)]
#[specta(transparent, export = false)]
pub struct TransparentWithSkip2(#[specta(skip)] (), String);

#[test]
fn skip() {
assert_ts!(SkipOnlyField, "Record<string, never>");
Expand All @@ -94,4 +104,6 @@ fn skip() {
SkipNamedFieldInVariant,
"{ A: Record<string, never> } | { B: { b: number } }"
);
assert_ts!(TransparentWithSkip, "null");
assert_ts!(TransparentWithSkip2, "string");
}

0 comments on commit 821fd62

Please sign in to comment.