Skip to content

Commit

Permalink
rewrite using #277 and #274
Browse files Browse the repository at this point in the history
  • Loading branch information
NyxCode committed Mar 20, 2024
1 parent 2a272c3 commit 34c85bb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 52 deletions.
4 changes: 2 additions & 2 deletions ts-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,8 @@ macro_rules! impl_shadow {
{
<$s>::generics()
}
fn decl() -> String { panic!("{} cannot be declared", Self::name()) }
fn decl_concrete() -> String { panic!("{} cannot be declared", Self::name()) }
fn decl() -> String { <$s>::decl() }
fn decl_concrete() -> String { <$s>::decl_concrete() }
}
};
}
Expand Down
61 changes: 17 additions & 44 deletions ts-rs/src/serde_json.rs
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>);
18 changes: 12 additions & 6 deletions ts-rs/tests/serde_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn using_serde_json() {
assert_eq!(serde_json::Number::inline(), "number");
assert_eq!(
serde_json::Map::<String, i32>::inline(),
"Record<string, number>"
"{ [key: string]: number }"
);
assert_eq!(
serde_json::Value::decl(),
Expand All @@ -31,11 +31,11 @@ fn using_serde_json() {
UsingSerdeJson::decl(),
"type UsingSerdeJson = { \
num: number, \
map1: Record<string, number>, \
map2: Record<string, UsingSerdeJson>, \
map3: Record<string, Record<string, number>>, \
map4: Record<string, number>, \
map5: Record<string, JsonValue>, \
map1: { [key: string]: number }, \
map2: { [key: string]: UsingSerdeJson }, \
map3: { [key: string]: { [key: string]: number } }, \
map4: { [key: string]: number }, \
map5: { [key: string]: JsonValue }, \
any: JsonValue, \
};"
)
Expand All @@ -57,3 +57,9 @@ fn inlined_value() {
};"
);
}

#[derive(TS)]
#[ts(export, export_to = "serde_json_impl/")]
struct Simple {
json: serde_json::Value
}

0 comments on commit 34c85bb

Please sign in to comment.