Skip to content

Commit

Permalink
Merge pull request #42 from johanholmerin/main
Browse files Browse the repository at this point in the history
Add skip support for skipping serialization inside a newtype enum variant
  • Loading branch information
siefkenj committed Aug 13, 2024
2 parents 29abef8 + e1d4742 commit 27d6982
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
69 changes: 68 additions & 1 deletion tests/skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ fn test_skip() {
C,
/// Comment for D
D,
/// Comment for Struct
Struct {
#[serde(skip)]
field_a: bool,
field_b: u8,
field_c: String,
},
/// Comment for Tuple
Tuple(#[serde(skip)] bool, u8, String),
/// Comment for NewType
NewType(#[serde(skip)] bool),
}

let expected = indoc! {r#"
Expand All @@ -77,13 +88,69 @@ fn test_skip() {
* Comment for D
*/
export type D = "D";
/**
* Comment for Struct
*/
export type Struct = { Struct: { field_b: number; field_c: string } };
/**
* Comment for Tuple
*/
export type Tuple = { Tuple: [number, string] };
/**
* Comment for NewType
*/
export type NewType = "NewType";
}
/**
* Comment for Enum
*/
export type Enum = "D";"#
export type Enum = "D" | { Struct: { field_b: number; field_c: string } } | { Tuple: [number, string] } | "NewType";"#
};

assert_eq!(Enum::DECL, expected);

/// Comment for InternalTagEnum
#[derive(Tsify)]
#[serde(tag = "type")]
#[tsify(namespace)]
enum InternalTagEnum {
/// Comment for Unit
Unit,
/// Comment for Struct
Struct {
#[serde(skip)]
field_a: bool,
field_b: u8,
},
/// Comment for NewType
NewType(#[serde(skip)] bool),
}

let expected = indoc! {r#"
/**
* Comment for InternalTagEnum
*/
declare namespace InternalTagEnum {
/**
* Comment for Unit
*/
export type Unit = { type: "Unit" };
/**
* Comment for Struct
*/
export type Struct = { type: "Struct"; field_b: number };
/**
* Comment for NewType
*/
export type NewType = { type: "NewType" };
}
/**
* Comment for InternalTagEnum
*/
export type InternalTagEnum = { type: "Unit" } | { type: "Struct"; field_b: number } | { type: "NewType" };"#
};

assert_eq!(InternalTagEnum::DECL, expected);
}
11 changes: 10 additions & 1 deletion tsify-next-macros/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,16 @@ impl<'a> Parser<'a> {
fn parse_variant(&self, variant: &Variant) -> TsType {
let tag_type = self.container.serde_attrs().tag();
let name = variant.attrs.name().serialize_name().to_owned();
let style = variant.style;
// Checks for Newtype with a skip attribute and treats it as a Unit
let style = if matches!(variant.style, Style::Newtype)
&& (variant.fields[0].attrs.skip_serializing()
|| variant.fields[0].attrs.skip_deserializing()
|| is_phantom(variant.fields[0].ty))
{
Style::Unit
} else {
variant.style
};
let type_ann: TsType = self.parse_fields(style, &variant.fields).into();
type_ann.with_tag_type(&self.container.attrs.ty_config, name, style, tag_type)
}
Expand Down

0 comments on commit 27d6982

Please sign in to comment.