Skip to content

Commit

Permalink
feature: support smol_str (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
corvusrabus authored Sep 7, 2024
1 parent 78591a2 commit 7577aca
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- The `bson-uuid-impl` feature now supports `bson::oid::ObjectId` as well ([#340](https://github.com/Aleph-Alpha/ts-rs/pull/340))
- Allow multile types to have the same `#[ts(export_to = "...")]` attribute and be exported to the same file ([#316](https://github.com/Aleph-Alpha/ts-rs/pull/316))
- Add support for types from `smol_str` behind cargo feature `smol_str-impl`

### Fixes

Expand Down
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ When running `cargo test`, the TypeScript bindings will be exported to the file
| ordered-float-impl | Implement `TS` for types from *ordered_float* |
| heapless-impl | Implement `TS` for types from *heapless* |
| semver-impl | Implement `TS` for types from *semver* |
| smol_str-impl | Implement `TS` for types from *smol_str* |

<br/>

Expand Down
2 changes: 2 additions & 0 deletions ts-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ indexmap-impl = ["indexmap"]
ordered-float-impl = ["ordered-float"]
heapless-impl = ["heapless"]
semver-impl = ["semver"]
smolstr-impl = ["smol_str"]
serde-json-impl = ["serde_json"]
no-serde-warnings = ["ts-rs-macros/no-serde-warnings"]
import-esm = []
Expand All @@ -53,6 +54,7 @@ bson = { version = "2", optional = true }
bytes = { version = "1", optional = true }
url = { version = "2", optional = true }
semver = { version = "1", optional = true }
smol_str = { version = "0.3", optional = true }
thiserror = "1"
indexmap = { version = "2", optional = true }
ordered-float = { version = ">= 3, < 5", optional = true }
Expand Down
4 changes: 4 additions & 0 deletions ts-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
//! | ordered-float-impl | Implement `TS` for types from *ordered_float* |
//! | heapless-impl | Implement `TS` for types from *heapless* |
//! | semver-impl | Implement `TS` for types from *semver* |
//! | smol_str-impl | Implement `TS` for types from *smol_str* |
//!
//! <br/>
//!
Expand Down Expand Up @@ -994,6 +995,9 @@ impl_tuples!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10);
#[cfg(feature = "bigdecimal-impl")]
impl_primitives! { bigdecimal::BigDecimal => "string" }

#[cfg(feature = "smolstr-impl")]
impl_primitives! { smol_str::SmolStr => "string" }

#[cfg(feature = "uuid-impl")]
impl_primitives! { uuid::Uuid => "string" }

Expand Down
106 changes: 106 additions & 0 deletions ts-rs/tests/integration/impl_primitive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#[cfg(feature = "bigdecimal-impl")]
#[test]
fn impl_primitive_bigdecimal() {
assert_eq!(
<bigdecimal::BigDecimal as ts_rs::TS>::name(),
<String as ts_rs::TS>::name()
);
assert_eq!(
<bigdecimal::BigDecimal as ts_rs::TS>::inline(),
<String as ts_rs::TS>::inline()
)
}

#[cfg(feature = "smolstr-impl")]
#[test]
fn impl_primitive_smolstr() {
assert_eq!(
<smol_str::SmolStr as ts_rs::TS>::name(),
<String as ts_rs::TS>::name()
);
assert_eq!(
<smol_str::SmolStr as ts_rs::TS>::inline(),
<String as ts_rs::TS>::inline()
)
}

#[cfg(feature = "uuid-impl")]
#[test]
fn impl_primitive_uuid() {
assert_eq!(
<uuid::Uuid as ts_rs::TS>::name(),
<String as ts_rs::TS>::name()
);
assert_eq!(
<uuid::Uuid as ts_rs::TS>::inline(),
<String as ts_rs::TS>::inline()
)
}

#[cfg(feature = "url-impl")]
#[test]
fn impl_primitive_url() {
assert_eq!(
<url::Url as ts_rs::TS>::name(),
<String as ts_rs::TS>::name()
);
assert_eq!(
<url::Url as ts_rs::TS>::inline(),
<String as ts_rs::TS>::inline()
)
}

#[cfg(feature = "ordered-float-impl")]
#[test]
fn impl_primitive_order_float() {
assert_eq!(
<ordered_float::OrderedFloat<f64> as ts_rs::TS>::name(),
<f64 as ts_rs::TS>::name()
);
assert_eq!(
<ordered_float::OrderedFloat<f64> as ts_rs::TS>::inline(),
<f64 as ts_rs::TS>::inline()
);
assert_eq!(
<ordered_float::OrderedFloat<f32> as ts_rs::TS>::name(),
<f32 as ts_rs::TS>::name()
);
assert_eq!(
<ordered_float::OrderedFloat<f32> as ts_rs::TS>::inline(),
<f32 as ts_rs::TS>::inline()
)
}

#[cfg(feature = "bson-uuid-impl")]
#[test]
fn impl_primitive_bson_uuid() {
assert_eq!(
<bson::oid::ObjectId as ts_rs::TS>::name(),
<String as ts_rs::TS>::name()
);
assert_eq!(
<bson::oid::ObjectId as ts_rs::TS>::inline(),
<String as ts_rs::TS>::inline()
);
assert_eq!(
<bson::Uuid as ts_rs::TS>::name(),
<String as ts_rs::TS>::name()
);
assert_eq!(
<bson::Uuid as ts_rs::TS>::inline(),
<String as ts_rs::TS>::inline()
)
}

#[cfg(feature = "semver-impl")]
#[test]
fn impl_primitive_semver() {
assert_eq!(
<semver::Version as ts_rs::TS>::name(),
<String as ts_rs::TS>::name()
);
assert_eq!(
<semver::Version as ts_rs::TS>::inline(),
<String as ts_rs::TS>::inline()
)
}
1 change: 1 addition & 0 deletions ts-rs/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod generics;
mod generics_flatten;
mod hashmap;
mod hashset;
mod impl_primitive;
mod imports;
mod indexmap;
mod infer_as;
Expand Down

0 comments on commit 7577aca

Please sign in to comment.