-
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.
- Loading branch information
Showing
3 changed files
with
31 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,21 @@ | ||
use std::{collections::HashMap, path::Path}; | ||
|
||
use super::{impl_primitives, impl_shadow, typelist::TypeList, TS}; | ||
|
||
// Manually implement `TS` for `Value`. | ||
// Defining an untagged enum and deriving `TS` doesn't work for the following reasons: | ||
// - `#[derive(TS)]` doesn't work in this crate, since the macro generates `::ts_rs::TS` | ||
// - `Value` is self-referential, and in this case (an untagged enum), tsc doesn't compile | ||
// `type JsonValue = ... | Record<string, JsonValue>`, but `{ [x: string]: JsonValue }` works. | ||
impl TS for serde_json::Value { | ||
type WithoutGenerics = Self; | ||
|
||
fn ident() -> String { | ||
"JsonValue".to_owned() | ||
} | ||
|
||
fn decl() -> String { | ||
Self::decl_concrete() | ||
} | ||
|
||
fn decl_concrete() -> String { | ||
format!("type {} = {};", "JsonValue", Self::inline()) | ||
} | ||
|
||
fn name() -> String { | ||
Self::ident() | ||
} | ||
|
||
fn inline() -> String { | ||
let name = Self::name(); | ||
format!("number | string | Array<{name}> | {{ [key: string]: JsonValue }}") | ||
} | ||
|
||
fn inline_flattened() -> String { | ||
Self::inline() | ||
} | ||
|
||
fn dependency_types() -> impl TypeList { | ||
().push::<Self>() | ||
} | ||
|
||
fn output_path() -> Option<&'static Path> { | ||
Some(Path::new("serde_json/Value.ts")) | ||
} | ||
use std::collections::HashMap; | ||
|
||
use super::{impl_primitives, impl_shadow, TS}; | ||
|
||
#[derive(TS)] | ||
#[ts( | ||
crate = "crate", | ||
rename = "Value", | ||
untagged, | ||
export_to = "serde_json/" | ||
)] | ||
pub enum TsJsonValue { | ||
Number(i32), | ||
String(String), | ||
Array(Vec<TsJsonValue>), | ||
Object(HashMap<String, TsJsonValue>), | ||
} | ||
|
||
impl_shadow!(as TsJsonValue: impl TS for serde_json::Value); | ||
impl_primitives!(serde_json::Number => "number"); | ||
impl_shadow!(as HashMap<K, V>: impl<K: TS, V: TS> TS for serde_json::Map<K, V>); |
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