-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from timephy/main
Added `serde(skip)` functionality inside Enum Variants
- Loading branch information
Showing
4 changed files
with
167 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#![allow(dead_code)] | ||
|
||
use serde::Deserialize; | ||
use ts_rs::TS; | ||
|
||
#[derive(TS, Deserialize)] | ||
#[serde(untagged)] | ||
enum TestUntagged { | ||
A, // serde_json -> `null` | ||
B(), // serde_json -> `[]` | ||
C { | ||
#[serde(skip)] | ||
val: i32, | ||
}, // serde_json -> `{}` | ||
} | ||
|
||
#[derive(TS, Deserialize)] | ||
enum TestExternally { | ||
A, // serde_json -> `"A"` | ||
B(), // serde_json -> `{"B":[]}` | ||
C { | ||
#[serde(skip)] | ||
val: i32, | ||
}, // serde_json -> `{"C":{}}` | ||
} | ||
|
||
#[derive(TS, Deserialize)] | ||
#[serde(tag = "type", content = "content")] | ||
enum TestAdjacently { | ||
A, // serde_json -> `{"type":"A"}` | ||
B(), // serde_json -> `{"type":"B","content":[]}` | ||
C { | ||
#[serde(skip)] | ||
val: i32, | ||
}, // serde_json -> `{"type":"C","content":{}}` | ||
} | ||
|
||
#[derive(TS, Deserialize)] | ||
#[serde(tag = "type")] | ||
enum TestInternally { | ||
A, // serde_json -> `{"type":"A"}` | ||
B, // serde_json -> `{"type":"B"}` | ||
C { | ||
#[serde(skip)] | ||
val: i32, | ||
}, // serde_json -> `{"type":"C"}` | ||
} | ||
|
||
#[cfg(feature = "serde-compat")] | ||
#[test] | ||
fn test() { | ||
assert_eq!( | ||
TestUntagged::decl(), | ||
r#"type TestUntagged = null | never[] | { };"# | ||
); | ||
|
||
assert_eq!( | ||
TestExternally::decl(), | ||
r#"type TestExternally = "A" | { "B": never[] } | { "C": { } };"# | ||
); | ||
|
||
assert_eq!( | ||
TestAdjacently::decl(), | ||
r#"type TestAdjacently = { "type": "A" } | { "type": "B", "content": never[] } | { "type": "C", "content": { } };"# | ||
); | ||
|
||
assert_eq!( | ||
TestInternally::decl(), | ||
r#"type TestInternally = { "type": "A" } | { "type": "B" } | { "type": "C", };"# | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#![allow(dead_code)] | ||
|
||
use serde::Deserialize; | ||
use ts_rs::TS; | ||
|
||
#[derive(TS, Deserialize)] | ||
#[serde(untagged)] | ||
enum TestUntagged { | ||
A, // serde_json -> `null` | ||
B(), // serde_json -> `[]` | ||
C(#[serde(skip)] i32), // serde_json -> `null` | ||
} | ||
|
||
#[derive(TS, Deserialize)] | ||
enum TestExternally { | ||
A, // serde_json -> `"A"` | ||
B(), // serde_json -> `{"B":[]}` | ||
C(#[serde(skip)] i32), // serde_json -> `"C"` | ||
} | ||
|
||
#[derive(TS, Deserialize)] | ||
#[serde(tag = "type", content = "content")] | ||
enum TestAdjacently { | ||
A, // serde_json -> `{"type":"A"}` | ||
B(), // serde_json -> `{"type":"B","content":[]}` | ||
C(#[serde(skip)] i32), // serde_json -> `{"type":"C"}` | ||
} | ||
|
||
#[derive(TS, Deserialize)] | ||
#[serde(tag = "type")] | ||
enum TestInternally { | ||
A, // serde_json -> `{"type":"A"}` | ||
B, // serde_json -> `{"type":"B"}` | ||
C(#[serde(skip)] i32), // serde_json -> `{"type":"C"}` | ||
} | ||
|
||
#[cfg(feature = "serde-compat")] | ||
#[test] | ||
fn test() { | ||
assert_eq!( | ||
TestUntagged::decl(), | ||
r#"type TestUntagged = null | never[] | null;"# | ||
); | ||
|
||
assert_eq!( | ||
TestExternally::decl(), | ||
r#"type TestExternally = "A" | { "B": never[] } | "C";"# | ||
); | ||
|
||
assert_eq!( | ||
TestAdjacently::decl(), | ||
r#"type TestAdjacently = { "type": "A" } | { "type": "B", "content": never[] } | { "type": "C" };"# | ||
); | ||
|
||
assert_eq!( | ||
TestInternally::decl(), | ||
r#"type TestInternally = { "type": "A" } | { "type": "B" } | { "type": "C" };"# | ||
); | ||
} |