From 89235eedc9aa18f8ccf73e027369068e93c2eeba Mon Sep 17 00:00:00 2001 From: Moritz Bischof Date: Thu, 11 Jan 2024 04:53:11 +0100 Subject: [PATCH 1/3] bump version to 7.1 --- README.md | 2 +- macros/Cargo.toml | 2 +- ts-rs/Cargo.toml | 4 ++-- ts-rs/src/lib.rs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 09e29b826..5d1386e3c 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ We recommend doing this in your tests. ### get started ```toml [dependencies] -ts-rs = "7.0" +ts-rs = "7.1" ``` ```rust diff --git a/macros/Cargo.toml b/macros/Cargo.toml index ce884bb74..78a9550c2 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ts-rs-macros" -version = "7.0.0" +version = "7.1.0" authors = ["Moritz Bischof "] edition = "2021" description = "derive macro for ts-rs" diff --git a/ts-rs/Cargo.toml b/ts-rs/Cargo.toml index 0651a83bf..f40c45e30 100644 --- a/ts-rs/Cargo.toml +++ b/ts-rs/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ts-rs" -version = "7.0.0" +version = "7.1.0" authors = ["Moritz Bischof "] edition = "2021" license = "MIT" @@ -37,7 +37,7 @@ chrono = { version = "0.4", features = ["serde"] } [dependencies] heapless = { version = "0.7", optional = true } -ts-rs-macros = { version = "7.0.0", path = "../macros" } +ts-rs-macros = { version = "7.1.0", path = "../macros" } dprint-plugin-typescript = { version = "0.85.1", optional = true } chrono = { version = "0.4", optional = true } bigdecimal = { version = ">=0.0.13, < 0.4.0", features = [ diff --git a/ts-rs/src/lib.rs b/ts-rs/src/lib.rs index 14477d26f..ff4e8f442 100644 --- a/ts-rs/src/lib.rs +++ b/ts-rs/src/lib.rs @@ -40,7 +40,7 @@ //! ## get started //! ```toml //! [dependencies] -//! ts-rs = "7.0" +//! ts-rs = "7.1" //! ``` //! //! ```rust From e545a3a11ecefaaf0d0d29e230c461b9da4aafeb Mon Sep 17 00:00:00 2001 From: Moritz Bischof Date: Thu, 11 Jan 2024 05:15:27 +0100 Subject: [PATCH 2/3] do not parse serde attributes when #[ts(skip)] is present --- macros/src/attr/field.rs | 5 ++++- macros/src/attr/variant.rs | 5 ++++- ts-rs/tests/serde-skip-with-default.rs | 27 ++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 ts-rs/tests/serde-skip-with-default.rs diff --git a/macros/src/attr/field.rs b/macros/src/attr/field.rs index 13daef55d..0c91b1c99 100644 --- a/macros/src/attr/field.rs +++ b/macros/src/attr/field.rs @@ -22,7 +22,10 @@ impl FieldAttr { let mut result = Self::default(); parse_attrs(attrs)?.for_each(|a| result.merge(a)); #[cfg(feature = "serde-compat")] - crate::utils::parse_serde_attrs::(attrs).for_each(|a| result.merge(a.0)); + if !result.skip { + crate::utils::parse_serde_attrs::(attrs) + .for_each(|a| result.merge(a.0)); + } Ok(result) } diff --git a/macros/src/attr/variant.rs b/macros/src/attr/variant.rs index 0f67f1ee9..1f490db45 100644 --- a/macros/src/attr/variant.rs +++ b/macros/src/attr/variant.rs @@ -22,7 +22,10 @@ impl VariantAttr { let mut result = Self::default(); parse_attrs(attrs)?.for_each(|a| result.merge(a)); #[cfg(feature = "serde-compat")] - crate::utils::parse_serde_attrs::(attrs).for_each(|a| result.merge(a.0)); + if !result.skip { + crate::utils::parse_serde_attrs::(attrs) + .for_each(|a| result.merge(a.0)); + } Ok(result) } diff --git a/ts-rs/tests/serde-skip-with-default.rs b/ts-rs/tests/serde-skip-with-default.rs new file mode 100644 index 000000000..6287f36eb --- /dev/null +++ b/ts-rs/tests/serde-skip-with-default.rs @@ -0,0 +1,27 @@ +#![allow(dead_code)] + +// from issue #107. This does now no longer generate a warning. + +use serde::{Deserialize, Serialize}; +use ts_rs::TS; + +fn default_http_version() -> String { + "2".to_owned() +} + +#[derive(Debug, Clone, Deserialize, Serialize, TS)] +#[ts(export)] +pub struct Foobar { + #[ts(skip)] + #[serde(skip, default = "default_http_version")] + pub http_version: String, + pub something_else: i32, +} + +#[test] +fn serde_skip_with_default() { + assert_eq!( + Foobar::decl(), + "interface Foobar { something_else: number, }" + ); +} From 4df0ffbd1b1ef542526ee9f8972260fabfc09ee5 Mon Sep 17 00:00:00 2001 From: Moritz Bischof Date: Thu, 11 Jan 2024 05:17:34 +0100 Subject: [PATCH 3/3] format, fix linter warnings --- ts-rs/src/lib.rs | 22 +++++++++++++--------- ts-rs/tests/issue-80.rs | 6 +++--- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/ts-rs/src/lib.rs b/ts-rs/src/lib.rs index ff4e8f442..49391ab47 100644 --- a/ts-rs/src/lib.rs +++ b/ts-rs/src/lib.rs @@ -104,7 +104,7 @@ //! Implement `TS` for `OrderedFloat` from ordered_float //! //! - `heapless-impl` -//! +//! //! Implement `TS` for `Vec` from heapless //! //! - `no-serde-warnings` @@ -464,20 +464,24 @@ impl TS for Option { } } -impl TS for Result{ - fn name() -> String{ +impl TS for Result { + fn name() -> String { unreachable!(); } - fn transparent() -> bool { - return true; + fn inline() -> String { + format!("{{ Ok : {} }} | {{ Err : {} }}", T::inline(), E::inline()) } fn dependencies() -> Vec where - Self: 'static { - [Dependency::from_ty::(), Dependency::from_ty::()].into_iter().flatten().collect() + Self: 'static, + { + [Dependency::from_ty::(), Dependency::from_ty::()] + .into_iter() + .flatten() + .collect() } - fn inline() -> String { - format!("{{ Ok : {} }} | {{ Err : {} }}", T::inline(), E::inline()) + fn transparent() -> bool { + true } } diff --git a/ts-rs/tests/issue-80.rs b/ts-rs/tests/issue-80.rs index 8b0ce4b91..20c4b07cb 100644 --- a/ts-rs/tests/issue-80.rs +++ b/ts-rs/tests/issue-80.rs @@ -1,5 +1,5 @@ -use ts_rs::TS; use serde::Serialize; +use ts_rs::TS; #[derive(TS, Serialize)] #[ts(export)] @@ -9,11 +9,11 @@ pub enum SomeTypeList { #[ts(skip)] skip_this: String, }, - Value2 + Value2, } #[test] fn issue_80() { let ty = SomeTypeList::inline(); assert_eq!(ty, r#"{ "Value1": { } } | "Value2""#); -} \ No newline at end of file +}