Skip to content

Commit

Permalink
Merge pull request #190 from Aleph-Alpha/ignore-serde-attributes-when…
Browse files Browse the repository at this point in the history
…-skipped

Ignore serde attributes when #[ts(skip)] is present
  • Loading branch information
NyxCode authored Jan 11, 2024
2 parents 618a164 + 4df0ffb commit 7e54da2
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ We recommend doing this in your tests.
### get started
```toml
[dependencies]
ts-rs = "7.0"
ts-rs = "7.1"
```

```rust
Expand Down
2 changes: 1 addition & 1 deletion macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ts-rs-macros"
version = "7.0.0"
version = "7.1.0"
authors = ["Moritz Bischof <[email protected]>"]
edition = "2021"
description = "derive macro for ts-rs"
Expand Down
5 changes: 4 additions & 1 deletion macros/src/attr/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<SerdeFieldAttr>(attrs).for_each(|a| result.merge(a.0));
if !result.skip {
crate::utils::parse_serde_attrs::<SerdeFieldAttr>(attrs)
.for_each(|a| result.merge(a.0));
}
Ok(result)
}

Expand Down
5 changes: 4 additions & 1 deletion macros/src/attr/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<SerdeVariantAttr>(attrs).for_each(|a| result.merge(a.0));
if !result.skip {
crate::utils::parse_serde_attrs::<SerdeVariantAttr>(attrs)
.for_each(|a| result.merge(a.0));
}
Ok(result)
}

Expand Down
4 changes: 2 additions & 2 deletions ts-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ts-rs"
version = "7.0.0"
version = "7.1.0"
authors = ["Moritz Bischof <[email protected]>"]
edition = "2021"
license = "MIT"
Expand Down Expand Up @@ -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 = [
Expand Down
24 changes: 14 additions & 10 deletions ts-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
//! ## get started
//! ```toml
//! [dependencies]
//! ts-rs = "7.0"
//! ts-rs = "7.1"
//! ```
//!
//! ```rust
Expand Down Expand Up @@ -104,7 +104,7 @@
//! Implement `TS` for `OrderedFloat` from ordered_float
//!
//! - `heapless-impl`
//!
//!
//! Implement `TS` for `Vec` from heapless
//!
//! - `no-serde-warnings`
Expand Down Expand Up @@ -464,20 +464,24 @@ impl<T: TS> TS for Option<T> {
}
}

impl<T : TS, E : TS> TS for Result<T, E>{
fn name() -> String{
impl<T: TS, E: TS> TS for Result<T, E> {
fn name() -> String {
unreachable!();
}
fn transparent() -> bool {
return true;
fn inline() -> String {
format!("{{ Ok : {} }} | {{ Err : {} }}", T::inline(), E::inline())
}
fn dependencies() -> Vec<Dependency>
where
Self: 'static {
[Dependency::from_ty::<T>(), Dependency::from_ty::<E>()].into_iter().flatten().collect()
Self: 'static,
{
[Dependency::from_ty::<T>(), Dependency::from_ty::<E>()]
.into_iter()
.flatten()
.collect()
}
fn inline() -> String {
format!("{{ Ok : {} }} | {{ Err : {} }}", T::inline(), E::inline())
fn transparent() -> bool {
true
}
}

Expand Down
6 changes: 3 additions & 3 deletions ts-rs/tests/issue-80.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ts_rs::TS;
use serde::Serialize;
use ts_rs::TS;

#[derive(TS, Serialize)]
#[ts(export)]
Expand All @@ -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""#);
}
}
27 changes: 27 additions & 0 deletions ts-rs/tests/serde-skip-with-default.rs
Original file line number Diff line number Diff line change
@@ -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, }"
);
}

0 comments on commit 7e54da2

Please sign in to comment.