Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions macros/src/attr/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,8 @@ impl Attr for StructAttr {
}
}

if !matches!(item, Fields::Named(_)) {
if self.tag.is_some() {
syn_err!("`tag` cannot be used with unit or tuple structs");
}
if !matches!(item, Fields::Named(_)) && self.tag.is_some() {
syn_err!("`tag` cannot be used with unit or tuple structs");
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl DerivedTS {
let variants = #inline.replace(|x: char| !x.is_numeric() && x != ',', "");
let mut variants = variants
.split(',')
.map(|x| isize::from_str_radix(x, 10).ok())
.map(|x| x.parse().ok())
.peekable();

if variants.peek().is_none() {
Expand Down
18 changes: 12 additions & 6 deletions macros/src/optional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ use crate::attr::FieldAttr;
#[derive(Default, Clone, Copy)]
pub enum Optional {
/// Explicitly marked as optional with `#[ts(optional)]`
#[allow(clippy::enum_variant_names)]
Optional { nullable: bool },

/// Explicitly marked as not optional with `#[ts(optional = false)]`
#[allow(clippy::enum_variant_names)]
NotOptional,

#[default]
Expand Down Expand Up @@ -82,9 +84,11 @@ pub fn apply(
check_that_field_is_option(x);
true
}},
nullable
.then(|| field_ty.clone())
.unwrap_or_else(|| unwrap_option(crate_rename, field_ty)),
if nullable {
field_ty.clone()
} else {
unwrap_option(crate_rename, field_ty)
},
),
// Inherited `#[ts(optional)]` from the struct.
// Acts like `#[ts(optional)]` on a field, but does not error on non-`Option` fields.
Expand All @@ -93,9 +97,11 @@ pub fn apply(
parse_quote! {
<#field_ty as #crate_rename::TS>::IS_OPTION
},
nullable
.then(|| field_ty.clone())
.unwrap_or_else(|| unwrap_option(crate_rename, field_ty)),
if nullable {
field_ty.clone()
} else {
unwrap_option(crate_rename, field_ty)
},
),
// no applicable `#[ts(optional)]` attributes
_ => {
Expand Down
54 changes: 6 additions & 48 deletions ts-rs/tests/integration/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,7 @@ fn export_b() {
fn export_c() {
C::export().unwrap();

let expected_content = if cfg!(feature = "format") {
concat!(
let expected_content = concat!(
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n",
"/**\n",
" * Doc comment.\n",
Expand All @@ -221,20 +220,7 @@ fn export_c() {
" * Testing\n",
" */\n",
"export type C = Record<string, never>;\n",
)
} else {
concat!(
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n",
"/**\n",
" * Doc comment.\n",
" * Supports new lines.\n",
" *\n",
" * Testing\n",
" */\n",
"export type C = Record<string, never>;",
"\n",
)
};
);

let actual_content = fs::read_to_string(C::default_output_path().unwrap()).unwrap();

Expand All @@ -245,8 +231,7 @@ fn export_c() {
fn export_d() {
D::export().unwrap();

let expected_content = if cfg!(feature = "format") {
concat!(
let expected_content = concat!(
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n",
"/**\n",
" * Doc comment.\n",
Expand All @@ -255,20 +240,7 @@ fn export_d() {
" * Testing\n",
" */\n",
"export type D = null;\n",
)
} else {
concat!(
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n",
"/**\n",
" * Doc comment.\n",
" * Supports new lines.\n",
" *\n",
" * Testing\n",
" */\n",
"export type D = null;",
"\n",
)
};
);
let actual_content = fs::read_to_string(D::default_output_path().unwrap()).unwrap();

assert_eq!(actual_content, expected_content);
Expand All @@ -278,8 +250,7 @@ fn export_d() {
fn export_e() {
E::export().unwrap();

let expected_content = if cfg!(feature = "format") {
concat!(
let expected_content = concat!(
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n",
"/**\n",
" * Doc comment.\n",
Expand All @@ -288,20 +259,7 @@ fn export_e() {
" * Testing\n",
" */\n",
"export type E = never;\n",
)
} else {
concat!(
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n",
"/**\n",
" * Doc comment.\n",
" * Supports new lines.\n",
" *\n",
" * Testing\n",
" */\n",
"export type E = never;",
"\n",
)
};
);

let actual_content = fs::read_to_string(E::default_output_path().unwrap()).unwrap();

Expand Down
2 changes: 2 additions & 0 deletions ts-rs/tests/integration/export_to.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::useless_format)]

use std::path::Path;

use ts_rs::TS;
Expand Down
1 change: 0 additions & 1 deletion ts-rs/tests/integration/generics.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![allow(clippy::box_collection, clippy::enum_variant_names, dead_code)]
#![allow(dead_code)]

use std::{
collections::{BTreeMap, HashSet},
Expand Down
2 changes: 1 addition & 1 deletion ts-rs/tests/integration/self_referential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ enum ExternallyTagged {

G(
Vec<ExternallyTagged>,
[&'static ExternallyTagged; 1024],
Box<[&'static ExternallyTagged; 1024]>,
HashMap<String, ExternallyTagged>,
),
}
Expand Down
Loading