Skip to content

Commit 589f189

Browse files
authored
Merge pull request #60 from siefkenj/for-tsify
Merge tsify-next into tsify Great thanks to @siefkenj.
2 parents 66cddfe + c666d45 commit 589f189

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+4237
-1297
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
name: Build and Test
2+
3+
on:
4+
push:
5+
branches: [main, next]
6+
pull_request:
7+
branches: ["*"]
8+
9+
jobs:
10+
build:
11+
name: Build
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout sources
16+
uses: actions/checkout@v3
17+
18+
- name: Install toolchain
19+
uses: actions-rs/toolchain@v1
20+
with:
21+
profile: minimal
22+
toolchain: stable
23+
override: true
24+
25+
- name: Check
26+
uses: actions-rs/cargo@v1
27+
with:
28+
command: check
29+
30+
- name: Build
31+
uses: actions-rs/cargo@v1
32+
with:
33+
command: build
34+
test:
35+
name: Test
36+
runs-on: ubuntu-latest
37+
38+
steps:
39+
- name: Checkout sources
40+
uses: actions/checkout@v3
41+
42+
- name: Setup Node.js
43+
uses: actions/setup-node@v4
44+
with:
45+
node-version: '20'
46+
47+
- name: Install toolchain
48+
uses: actions-rs/toolchain@v1
49+
with:
50+
profile: minimal
51+
toolchain: stable
52+
override: true
53+
components: rustfmt
54+
55+
- name: Install wasm-pack
56+
uses: jetli/wasm-pack-action@v0.4.0
57+
with:
58+
# Optional version of wasm-pack to install(eg. 'v0.9.1', 'latest')
59+
version: "latest"
60+
61+
- name: Add cargo-expand
62+
run: cargo install cargo-expand
63+
64+
# Run the ./test.sh script
65+
- name: Test
66+
run: ./test.sh
67+
lint:
68+
name: Lint
69+
runs-on: ubuntu-latest
70+
71+
steps:
72+
- name: Checkout sources
73+
uses: actions/checkout@v3
74+
75+
- name: Install toolchain
76+
uses: actions-rs/toolchain@v1
77+
with:
78+
profile: minimal
79+
toolchain: stable
80+
override: true
81+
components: rustfmt, clippy
82+
83+
- name: Cargo fmt
84+
uses: actions-rs/cargo@v1
85+
with:
86+
command: fmt
87+
args: --all -- --check
88+
89+
- name: Cargo clippy
90+
run: cargo clippy -- -D warnings

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/target
2-
Cargo.lock
2+
Cargo.lock
3+
.vscode
4+
tests-e2e/*/pkg

CHANGELOG.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# tsify Changelog
2+
3+
## v0.5.5
4+
5+
- Don't assume a struct named `Range` is automatically a `Range` type
6+
- Put `#[automatically_derived]` on `impl` blocks
7+
- Better handling of `#[serde(skip)]`
8+
- Bump the `wasm_bindgen` dep version
9+
10+
## v0.5.4
11+
12+
- Allow serializing of `Vec<Struct>` provided that `Struct` is serializable.
13+
14+
## v0.5.3
15+
16+
- Propagate errors encountered during serialization.
17+
- More fixes for missing `From` trait implementations.
18+
19+
## v0.5.2
20+
21+
- Fix missing trait bounds for implemented `From` traits.
22+
23+
## v0.5.1
24+
25+
- @Pantamis contributed #22, implementing more `From` traits for more ergonomic use of Futures.
26+
- Fix: empty enums now produce a valid type of `void` rather than producing invalid Typescript.
27+
28+
## v0.5.0
29+
30+
- Forked from `tsify` merging most PRs that were queued on Github

Cargo.toml

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
[package]
22
name = "tsify"
3-
version = "0.4.5"
3+
version = "0.5.5"
44
edition = "2021"
5-
authors = ["Madono Haru <madonoharu@gmail.com>"]
5+
authors = [
6+
"Madono Haru <madonoharu@gmail.com>",
7+
"Jason Siefken <siefkenj@gmail.com>"
8+
]
69
license = "MIT OR Apache-2.0"
710
description = "Tsify is a library for generating TypeScript definitions from rust code."
811
repository = "https://github.com/madonoharu/tsify"
@@ -11,28 +14,29 @@ keywords = ["wasm", "wasm-bindgen", "typescript"]
1114
categories = ["wasm"]
1215

1316
[dependencies]
14-
tsify-macros = { path = "tsify-macros", version = "0.4.3" }
15-
wasm-bindgen = { version = "0.2.86", optional = true }
16-
serde = { version = "1.0", optional = true }
17+
tsify-macros = { path = "tsify-macros", version = "0.5.5" }
18+
wasm-bindgen = { version = "0.2.100", optional = true }
19+
serde = { version = "1.0", features = ["derive"], optional = true }
1720
serde_json = { version = "1.0", optional = true }
18-
serde-wasm-bindgen = { version = "0.5.0", optional = true }
19-
gloo-utils = { version = "0.1.6", optional = true }
21+
serde-wasm-bindgen = { version = "0.6", optional = true }
22+
gloo-utils = { version = "0.2", optional = true }
2023

2124
[dev-dependencies]
22-
indoc = "2.0.1"
23-
js-sys = "0.3.63"
25+
indoc = "2.0.5"
26+
js-sys = "0.3"
2427
macrotest = "1.0"
25-
pretty_assertions = "1.3.0"
26-
serde = { version = "1.0", features = ["derive"] }
27-
serde-wasm-bindgen = "0.5.0"
28-
serde_json = "1.0"
29-
wasm-bindgen = "0.2.86"
30-
wasm-bindgen-test = "0.3.36"
28+
pretty_assertions = "1.4.0"
29+
wasm-bindgen-test = "0.3"
3130

3231
[features]
3332
default = ["json"]
3433
wasm-bindgen = ["tsify-macros/wasm-bindgen", "dep:wasm-bindgen"]
35-
js = ["wasm-bindgen", "tsify-macros/js", "dep:serde", "dep:serde-wasm-bindgen"]
34+
js = [
35+
"wasm-bindgen",
36+
"tsify-macros/js",
37+
"dep:serde",
38+
"dep:serde-wasm-bindgen"
39+
]
3640
json = [
3741
"wasm-bindgen",
3842
"tsify-macros/json",
@@ -42,4 +46,5 @@ json = [
4246
]
4347

4448
[workspace]
45-
members = ["tsify-macros"]
49+
members = ["tsify-macros", "tests-e2e/*"]
50+
exclude = ["tests-e2e/reference_output"]

README.md

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Click to show Cargo.toml.
1515

1616
```toml
1717
[dependencies]
18-
tsify = "0.4.5"
18+
tsify = "0.5.5"
1919
serde = { version = "1.0", features = ["derive"] }
2020
wasm-bindgen = { version = "0.2" }
2121
```
@@ -57,45 +57,45 @@ export function into_js(): Point;
5757
*/
5858
export function from_js(point: Point): void;
5959
export interface Point {
60-
x: number;
61-
y: number;
60+
x: number;
61+
y: number;
6262
}
6363
```
6464

6565
This is the behavior due to [`typescript_custom_section`](https://rustwasm.github.io/docs/wasm-bindgen/reference/attributes/on-rust-exports/typescript_custom_section.html) and [`Rust Type conversions`](https://rustwasm.github.io/docs/wasm-bindgen/contributing/design/rust-type-conversions.html).
6666

6767
## Crate Features
6868

69-
- `json` (default) enables serialization through [`serde_json`](https://github.com/serde-rs/json).
70-
- `js` enables serialization through [`serde-wasm-bindgen`](https://github.com/cloudflare/serde-wasm-bindgen) and generates the appropriate types for it. This will be the default in future versions.
69+
- `json` (default) enables serialization through [`serde_json`](https://github.com/serde-rs/json).
70+
- `js` enables serialization through [`serde-wasm-bindgen`](https://github.com/cloudflare/serde-wasm-bindgen) and generates the appropriate types for it. This will be the default in future versions.
7171

7272
## Attributes
7373

7474
Tsify container attributes
7575

76-
- `into_wasm_abi` implements `IntoWasmAbi` and `OptionIntoWasmAbi`. This can be converted directly from Rust to JS via `serde_json` or `serde-wasm-bindgen`.
77-
- `from_wasm_abi` implements `FromWasmAbi` and `OptionFromWasmAbi`. This is the opposite operation of the above.
78-
- `namespace` generates a namespace for the enum variants.
76+
- `into_wasm_abi` implements `IntoWasmAbi` and `OptionIntoWasmAbi`. This can be converted directly from Rust to JS via `serde_json` or `serde-wasm-bindgen`.
77+
- `from_wasm_abi` implements `FromWasmAbi` and `OptionFromWasmAbi`. This is the opposite operation of the above.
78+
- `namespace` generates a namespace for the enum variants.
7979

8080
Tsify field attributes
8181

82-
- `type`
83-
- `optional`
82+
- `type`
83+
- `optional`
8484

8585
Serde attributes
8686

87-
- `rename`
88-
- `rename-all`
89-
- `tag`
90-
- `content`
91-
- `untagged`
92-
- `skip`
93-
- `skip_serializing`
94-
- `skip_deserializing`
95-
- `skip_serializing_if = "Option::is_none"`
96-
- `flatten`
97-
- `default`
98-
- `transparent`
87+
- `rename`
88+
- `rename-all`
89+
- `tag`
90+
- `content`
91+
- `untagged`
92+
- `skip`
93+
- `skip_serializing`
94+
- `skip_deserializing`
95+
- `skip_serializing_if = "Option::is_none"`
96+
- `flatten`
97+
- `default`
98+
- `transparent`
9999

100100
## Type Override
101101

@@ -113,7 +113,7 @@ Generated type:
113113

114114
```ts
115115
export interface Foo {
116-
x: 0 | 1 | 2;
116+
x: 0 | 1 | 2;
117117
}
118118
```
119119

@@ -135,9 +135,9 @@ Generated type:
135135

136136
```ts
137137
export interface Optional {
138-
a?: number;
139-
b?: string;
140-
c?: number;
138+
a?: number;
139+
b?: string;
140+
c?: number;
141141
}
142142
```
143143

@@ -162,11 +162,11 @@ Generated type:
162162

163163
```ts
164164
export type Color =
165-
| "Red"
166-
| "Blue"
167-
| "Green"
168-
| { Rgb: [number, number, number] }
169-
| { Hsv: { hue: number; saturation: number; value: number } };
165+
| "Red"
166+
| "Blue"
167+
| "Green"
168+
| { Rgb: [number, number, number] }
169+
| { Hsv: { hue: number; saturation: number; value: number } };
170170
```
171171

172172
## Enum with namespace
@@ -191,19 +191,21 @@ Generated type:
191191

192192
```ts
193193
declare namespace Color {
194-
export type Red = "Red";
195-
export type Blue = "Blue";
196-
export type Green = "Green";
197-
export type Rgb = { Rgb: [number, number, number] };
198-
export type Hsv = { Hsv: { hue: number; saturation: number; value: number } };
194+
export type Red = "Red";
195+
export type Blue = "Blue";
196+
export type Green = "Green";
197+
export type Rgb = { Rgb: [number, number, number] };
198+
export type Hsv = {
199+
Hsv: { hue: number; saturation: number; value: number };
200+
};
199201
}
200202

201203
export type Color =
202-
| "Red"
203-
| "Blue"
204-
| "Green"
205-
| { Rgb: [number, number, number] }
206-
| { Hsv: { hue: number; saturation: number; value: number } };
204+
| "Red"
205+
| "Blue"
206+
| "Green"
207+
| { Rgb: [number, number, number] }
208+
| { Hsv: { hue: number; saturation: number; value: number } };
207209
```
208210

209211
## Type Aliases

src/lib.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
11
#![allow(clippy::wrong_self_convention)]
22

3+
#[cfg(not(any(feature = "json", feature = "js")))]
4+
compile_error!(
5+
"Either the \"json\" or \"js\" feature must be enabled for tsify to function properly"
6+
);
7+
38
#[cfg(all(feature = "json", not(feature = "js")))]
49
pub use gloo_utils::format::JsValueSerdeExt;
10+
#[cfg(feature = "js")]
11+
pub use serde_wasm_bindgen;
512
pub use tsify_macros::*;
613
#[cfg(feature = "wasm-bindgen")]
714
use wasm_bindgen::{JsCast, JsValue};
815

16+
pub struct SerializationConfig {
17+
pub missing_as_null: bool,
18+
pub hashmap_as_object: bool,
19+
pub large_number_types_as_bigints: bool,
20+
}
21+
22+
/// `Tsify` is a trait that allows you to convert a type to and from JavaScript.
23+
/// Can be implemented manually if you need to customize the serialization or deserialization.
924
pub trait Tsify {
1025
#[cfg(feature = "wasm-bindgen")]
1126
type JsType: JsCast;
1227

1328
const DECL: &'static str;
29+
const SERIALIZATION_CONFIG: SerializationConfig = SerializationConfig {
30+
missing_as_null: false,
31+
hashmap_as_object: false,
32+
large_number_types_as_bigints: false,
33+
};
1434

1535
#[cfg(all(feature = "json", not(feature = "js")))]
1636
#[inline]
@@ -36,7 +56,12 @@ pub trait Tsify {
3656
where
3757
Self: serde::Serialize,
3858
{
39-
serde_wasm_bindgen::to_value(self).map(JsCast::unchecked_from_js)
59+
let config = <Self as Tsify>::SERIALIZATION_CONFIG;
60+
let serializer = serde_wasm_bindgen::Serializer::new()
61+
.serialize_missing_as_null(config.missing_as_null)
62+
.serialize_maps_as_objects(config.hashmap_as_object)
63+
.serialize_large_number_types_as_bigints(config.large_number_types_as_bigints);
64+
self.serialize(&serializer).map(JsCast::unchecked_from_js)
4065
}
4166

4267
#[cfg(feature = "js")]

0 commit comments

Comments
 (0)