Skip to content

Commit

Permalink
Fix empty struct tagged enum variant (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavo-shigueo authored Nov 25, 2024
1 parent 93aec35 commit 6438296
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion macros/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn type_def(attr: &StructAttr, ident: &str, fields: &Fields) -> Result<DerivedTS

match fields {
Fields::Named(named) => match named.named.len() {
0 => unit::empty_object(attr, &name),
0 if attr.tag.is_none() => unit::empty_object(attr, &name),
_ => named::named(attr, &name, named),
},
Fields::Unnamed(unnamed) => match unnamed.unnamed.len() {
Expand Down
4 changes: 2 additions & 2 deletions ts-rs/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ fn export_and_merge(
Ok(())
}

const HEADER_ERROR_MESSAGE: &'static str = "The generated strings must have their NOTE and imports separated from their type declarations by a new line";
const HEADER_ERROR_MESSAGE: &str = "The generated strings must have their NOTE and imports separated from their type declarations by a new line";

const DECLARATION_START: &'static str = "export type ";
const DECLARATION_START: &str = "export type ";

/// Inserts the imports and declaration from the newly generated type
/// into the contents of the file, removimg duplicate imports and organazing
Expand Down
22 changes: 22 additions & 0 deletions ts-rs/tests/integration/enum_variant_annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,25 @@ fn test_variant_quoted() {
}
assert_eq!(E::inline(), r#"{ "variant-name": { f: string, } }"#)
}

#[derive(TS)]
#[ts(export, export_to = "enum_variant_anotation/")]
enum D {
Foo {},
}

#[derive(TS)]
#[ts(export, export_to = "enum_variant_anotation/", tag = "type")]
enum E {
Foo {},
Bar {},
Biz { x: i32 },
}

#[test]
fn test_empty_struct_variant_with_tag() {
assert_eq!(
E::inline(),
r#"{ "type": "Foo", } | { "type": "Bar", } | { "type": "Biz", x: number, }"#
)
}
13 changes: 12 additions & 1 deletion ts-rs/tests/integration/struct_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,21 @@ struct TaggedType {
b: i32,
}

#[derive(TS)]
#[cfg_attr(feature = "serde-compat", derive(Serialize))]
#[cfg_attr(feature = "serde-compat", serde(tag = "type"))]
#[cfg_attr(not(feature = "serde-compat"), ts(tag = "type"))]
struct EmptyTaggedType {}

#[test]
fn test() {
assert_eq!(
TaggedType::inline(),
"{ \"type\": \"TaggedType\", a: number, b: number, }"
)
);

assert_eq!(
EmptyTaggedType::inline(),
r#"{ "type": "EmptyTaggedType", }"#
);
}

0 comments on commit 6438296

Please sign in to comment.