Skip to content

Commit cf9f42b

Browse files
authored
Support ts-rs (#23)
1 parent 8d259c2 commit cf9f42b

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

Cargo.lock

+23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ ryu = { version = "1", optional = true }
2323
rkyv = { version = "0.8", optional = true, default-features = false }
2424
sqlx = { version = "0.8", optional = true, default-features = false }
2525
sqlx-mysql = { version = "0.8", optional = true, default-features = false }
26+
ts-rs = { version = "10.1", optional = true, default-features = false }
2627

2728
[features]
2829
default = ["std"]
@@ -34,6 +35,7 @@ redis-unsafe = ["redis"]
3435
rkyv = ["rkyv/alloc"]
3536
sqlx-mysql = ["std", "dep:sqlx", "dep:sqlx-mysql"]
3637
sqlx-mysql-unsafe = ["sqlx-mysql"]
38+
ts-rs = ["std", "dep:ts-rs"]
3739

3840

3941
[dev-dependencies]

src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -806,3 +806,6 @@ mod rkyv;
806806

807807
#[cfg(feature = "sqlx-mysql")]
808808
mod sqlx_mysql;
809+
810+
#[cfg(feature = "ts-rs")]
811+
pub mod ts_rs;

src/ts_rs.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use crate::FastStr;
2+
3+
impl ts_rs::TS for FastStr {
4+
type WithoutGenerics = Self;
5+
fn name() -> String {
6+
"string".to_owned()
7+
}
8+
fn inline() -> String {
9+
<Self as ts_rs::TS>::name()
10+
}
11+
/// This function is expected to panic because primitive types cannot be flattened.
12+
fn inline_flattened() -> String {
13+
panic!("{} cannot be flattened", <Self as ts_rs::TS>::name())
14+
}
15+
/// This function is expected to panic because primitive types cannot be declared.
16+
fn decl() -> String {
17+
panic!("{} cannot be declared", <Self as ts_rs::TS>::name())
18+
}
19+
/// Same as `decl` if the type is not generic.
20+
fn decl_concrete() -> String {
21+
panic!("{} cannot be declared", <Self as ts_rs::TS>::name())
22+
}
23+
}
24+
25+
#[test]
26+
fn test_ts_rs() {
27+
#[derive(ts_rs::TS)]
28+
struct Nested {
29+
#[allow(unused)]
30+
#[ts(rename = "nested_id")]
31+
id: FastStr,
32+
}
33+
#[derive(ts_rs::TS)]
34+
struct Test {
35+
#[allow(unused)]
36+
#[ts(optional)]
37+
id: Option<FastStr>,
38+
#[allow(unused)]
39+
#[ts(flatten)]
40+
nested: Nested,
41+
}
42+
43+
assert_eq!(
44+
<Test as ts_rs::TS>::decl(),
45+
"type Test = { id?: string, nested_id: string, };"
46+
);
47+
}

0 commit comments

Comments
 (0)