Skip to content
Closed
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
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,25 @@ export type User = { user_id: number, first_name: string, last_name: string, };
- generic types
- support for ESM imports

### cargo features
| **Feature** | **Description** |
|:-------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| serde-compat | **Enabled by default** <br/>See the *"serde compatibility"* section below for more information. |
| format | Enables formatting of the generated TypeScript bindings. <br/>Currently, this unfortunately adds quite a few dependencies. |
| no-serde-warnings | By default, warnings are printed during build if unsupported serde attributes are encountered. <br/>Enabling this feature silences these warnings. |
| serde-json-impl | Implement `TS` for types from *serde_json* |
| chrono-impl | Implement `TS` for types from *chrono* |
| bigdecimal-impl | Implement `TS` for types from *bigdecimal* |
| url-impl | Implement `TS` for types from *url* |
| uuid-impl | Implement `TS` for types from *uuid* |
| bson-uuid-impl | Implement `TS` for *bson::oid::ObjectId* and *bson::uuid* |
| bytes-impl | Implement `TS` for types from *bytes* |
| indexmap-impl | Implement `TS` for types from *indexmap* |
| 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* |
| tokio-impl | Implement `TS` for types from *tokio* |
| **Feature** | **Description** |
| :----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
| serde-compat | **Enabled by default** <br/>See the _"serde compatibility"_ section below for more information. |
| format | Enables formatting of the generated TypeScript bindings. <br/>Currently, this unfortunately adds quite a few dependencies. |
| no-serde-warnings | By default, warnings are printed during build if unsupported serde attributes are encountered. <br/>Enabling this feature silences these warnings. |
| serde-json-impl | Implement `TS` for types from _serde_json_ |
| chrono-impl | Implement `TS` for types from _chrono_ |
| bigdecimal-impl | Implement `TS` for types from _bigdecimal_ |
| url-impl | Implement `TS` for types from _url_ |
| uuid-impl | Implement `TS` for types from _uuid_ |
| bson-uuid-impl | Implement `TS` for _bson::oid::ObjectId_ and _bson::uuid_ |
| bytes-impl | Implement `TS` for types from _bytes_ |
| indexmap-impl | Implement `TS` for types from _indexmap_ |
| 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_ |
| tokio-impl | Implement `TS` for types from _tokio_ |
| no-big-int | Always bind primitive integers to raw javascript numbers even when this could lead to precision loss |

<br/>

Expand Down
5 changes: 4 additions & 1 deletion ts-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ serde-json-impl = ["serde_json"]
no-serde-warnings = ["ts-rs-macros/no-serde-warnings"]
import-esm = []
tokio-impl = ["tokio"]
no-big-int = []

[dev-dependencies]
serde = { version = "1.0", features = ["derive"] }
Expand All @@ -50,7 +51,9 @@ thiserror = "2"
heapless = { version = ">= 0.7, < 0.9", optional = true }
dprint-plugin-typescript = { version = "0.90", optional = true }
chrono = { version = "0.4", optional = true }
bigdecimal = { version = ">= 0.0.13, < 0.5", features = ["serde"], optional = true }
bigdecimal = { version = ">= 0.0.13, < 0.5", features = [
"serde",
], optional = true }
uuid = { version = "1", optional = true }
bson = { version = "2", optional = true }
bytes = { version = "1", optional = true }
Expand Down
53 changes: 34 additions & 19 deletions ts-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,25 @@
//! - generic types
//! - support for ESM imports
//!
//! ## cargo features
//! | **Feature** | **Description** |
//! |:-------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
//! | serde-compat | **Enabled by default** <br/>See the *"serde compatibility"* section below for more information. |
//! | format | Enables formatting of the generated TypeScript bindings. <br/>Currently, this unfortunately adds quite a few dependencies. |
//! | no-serde-warnings | By default, warnings are printed during build if unsupported serde attributes are encountered. <br/>Enabling this feature silences these warnings. |
//! | serde-json-impl | Implement `TS` for types from *serde_json* |
//! | chrono-impl | Implement `TS` for types from *chrono* |
//! | bigdecimal-impl | Implement `TS` for types from *bigdecimal* |
//! | url-impl | Implement `TS` for types from *url* |
//! | uuid-impl | Implement `TS` for types from *uuid* |
//! | bson-uuid-impl | Implement `TS` for *bson::oid::ObjectId* and *bson::uuid* |
//! | bytes-impl | Implement `TS` for types from *bytes* |
//! | indexmap-impl | Implement `TS` for types from *indexmap* |
//! | 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* |
//! | tokio-impl | Implement `TS` for types from *tokio* |
//! | **Feature** | **Description** |
//! | :----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
//! | serde-compat | **Enabled by default** <br/>See the _"serde compatibility"_ section below for more information. |
//! | format | Enables formatting of the generated TypeScript bindings. <br/>Currently, this unfortunately adds quite a few dependencies. |
//! | no-serde-warnings | By default, warnings are printed during build if unsupported serde attributes are encountered. <br/>Enabling this feature silences these warnings. |
//! | serde-json-impl | Implement `TS` for types from _serde_json_ |
//! | chrono-impl | Implement `TS` for types from _chrono_ |
//! | bigdecimal-impl | Implement `TS` for types from _bigdecimal_ |
//! | url-impl | Implement `TS` for types from _url_ |
//! | uuid-impl | Implement `TS` for types from _uuid_ |
//! | bson-uuid-impl | Implement `TS` for _bson::oid::ObjectId_ and _bson::uuid_ |
//! | bytes-impl | Implement `TS` for types from _bytes_ |
//! | indexmap-impl | Implement `TS` for types from _indexmap_ |
//! | 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_ |
//! | tokio-impl | Implement `TS` for types from _tokio_ |
//! | no-big-int | Always bind primitive integers to raw javascript numbers even when this could lead to precision loss |
//!
//! <br/>
//!
Expand Down Expand Up @@ -1124,6 +1124,7 @@ mod bytes {
impl_shadow!(as Vec<u8>: impl TS for bytes::BytesMut);
}

#[cfg(not(feature = "no-big-int"))]
impl_primitives! {
u8, i8, NonZeroU8, NonZeroI8,
u16, i16, NonZeroU16, NonZeroI16,
Expand All @@ -1137,6 +1138,20 @@ impl_primitives! {
() => "null"
}

#[cfg(feature = "no-big-int")]
impl_primitives! {
u8, i8, NonZeroU8, NonZeroI8,
u16, i16, NonZeroU16, NonZeroI16,
u32, i32, NonZeroU32, NonZeroI32,
usize, isize, NonZeroUsize, NonZeroIsize, f32, f64,
u64, i64, NonZeroU64, NonZeroI64,
u128, i128, NonZeroU128, NonZeroI128 => "number",
bool => "boolean",
char, Path, PathBuf, String, str,
Ipv4Addr, Ipv6Addr, IpAddr, SocketAddrV4, SocketAddrV6, SocketAddr => "string",
() => "null"
}

#[rustfmt::skip]
pub(crate) use impl_primitives;
#[rustfmt::skip]
Expand Down
20 changes: 20 additions & 0 deletions ts-rs/tests/integration/enum_variant_annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,23 @@ enum A {
}

#[test]
#[cfg(not(feature = "no-big-int"))]
fn test_enum_variant_rename_all() {
assert_eq!(
A::inline(),
r#"{ "MESSAGE_ONE": { sender_id: string, number_of_snakes: bigint, } } | { "MESSAGE_TWO": { senderId: string, numberOfCamels: bigint, } }"#,
);
}

#[test]
#[cfg(feature = "no-big-int")]
fn test_enum_variant_rename_all() {
assert_eq!(
A::inline(),
r#"{ "MESSAGE_ONE": { sender_id: string, number_of_snakes: number, } } | { "MESSAGE_TWO": { senderId: string, numberOfCamels: number, } }"#,
);
}

#[derive(TS)]
#[ts(export, export_to = "enum_variant_anotation/")]
#[cfg_attr(feature = "serde-compat", derive(Serialize))]
Expand All @@ -49,13 +59,23 @@ enum B {
}

#[test]
#[cfg(not(feature = "no-big-int"))]
fn test_enum_variant_rename() {
assert_eq!(
B::inline(),
r#"{ "SnakeMessage": { sender_id: string, number_of_snakes: bigint, } } | { "CamelMessage": { sender_id: string, number_of_camels: bigint, } }"#,
);
}

#[test]
#[cfg(feature = "no-big-int")]
fn test_enum_variant_rename() {
assert_eq!(
B::inline(),
r#"{ "SnakeMessage": { sender_id: string, number_of_snakes: number, } } | { "CamelMessage": { sender_id: string, number_of_camels: number, } }"#,
);
}

#[derive(TS)]
#[ts(export, export_to = "enum_variant_anotation/")]
#[cfg_attr(feature = "serde-compat", derive(Serialize))]
Expand Down