diff --git a/macros/src/types/mod.rs b/macros/src/types/mod.rs index f5f0d87..cfea6a4 100644 --- a/macros/src/types/mod.rs +++ b/macros/src/types/mod.rs @@ -29,11 +29,15 @@ fn type_def( generics: &Generics, ) -> Result { match fields { - Fields::Named(named) => named::named(name, rename_all, named, generics), - Fields::Unnamed(unnamed) if unnamed.unnamed.len() == 1 => { - newtype::newtype(name, rename_all, unnamed) - } - Fields::Unnamed(unnamed) => tuple::tuple(name, rename_all, unnamed), + Fields::Named(named) => match named.named.len() { + 0 => unit::unit(name, rename_all), + _ => named::named(name, rename_all, named, generics), + }, + Fields::Unnamed(unnamed) => match unnamed.unnamed.len() { + 0 => unit::unit(name, rename_all), + 1 => newtype::newtype(name, rename_all, unnamed), + _ => tuple::tuple(name, rename_all, unnamed), + }, Fields::Unit => unit::unit(name, rename_all), } } diff --git a/ts-rs/Cargo.toml b/ts-rs/Cargo.toml index 320de6f..1b96eb1 100644 --- a/ts-rs/Cargo.toml +++ b/ts-rs/Cargo.toml @@ -12,6 +12,7 @@ repository = "https://github.com/Aleph-Alpha/ts-rs" chrono-impl = ["chrono"] bigdecimal-impl = ["bigdecimal"] uuid-impl = ["uuid"] +bytes-impl = ["bytes"] serde-compat = ["ts-rs-macros/serde-compat"] [dev-dependencies] @@ -23,4 +24,4 @@ dprint-plugin-typescript = { version = "0.43" } chrono = { version = "0.4.19", optional = true } bigdecimal = {version = ">=0.0.13, < 0.4.0", features=["serde"], optional = true} uuid = { version = "0.8.2", optional = true } - +bytes = { version = "1.0", optional = true } \ No newline at end of file diff --git a/ts-rs/src/export.rs b/ts-rs/src/export.rs index 21d3175..c305fb8 100644 --- a/ts-rs/src/export.rs +++ b/ts-rs/src/export.rs @@ -44,10 +44,21 @@ use crate::TS; /// for `bindings/declared.d.ts`. #[macro_export] macro_rules! export { - ($($(($decl:ident))? $($p:path),+ => $l:literal),* $(,)?) => { + ($($arg:tt)*) => { #[cfg(test)] #[test] fn export_typescript() { + ts_rs::export_here!($($arg)*) + } + }; +} + +/// Like `export!` but instead of creating a test function it executes the binding generation right here. +/// This may be useful if you'd like to run the binding generation in any other context than a test. +#[macro_export] +macro_rules! export_here { + ($($(($decl:ident))? $($p:path),+ => $l:expr),* $(,)?) => { + { use std::fmt::Write; use std::collections::{BTreeMap as __BTreeMap, BTreeSet as __BTreeSet}; diff --git a/ts-rs/src/lib.rs b/ts-rs/src/lib.rs index 1aaf2fc..94abf55 100644 --- a/ts-rs/src/lib.rs +++ b/ts-rs/src/lib.rs @@ -250,6 +250,48 @@ impl_primitives! { () => "null" } +#[cfg(feature = "bytes-impl")] +mod bytes { + use super::TS; + use std::any::TypeId; + + impl TS for bytes::Bytes { + fn name() -> String { + "Array".to_owned() + } + + fn inline(indent: usize) -> String { + format!("Array<{}>", u8::inline(indent)) + } + + fn dependencies() -> Vec<(TypeId, String)> { + vec![(TypeId::of::(), u8::name())] + } + + fn transparent() -> bool { + true + } + } + + impl TS for bytes::BytesMut { + fn name() -> String { + "Array".to_owned() + } + + fn inline(indent: usize) -> String { + format!("Array<{}>", u8::inline(indent)) + } + + fn dependencies() -> Vec<(TypeId, String)> { + vec![(TypeId::of::(), u8::name())] + } + + fn transparent() -> bool { + true + } + } +} + #[cfg(feature = "chrono-impl")] mod chrono_impls { use super::TS; diff --git a/ts-rs/tests/macro/export_here.rs b/ts-rs/tests/macro/export_here.rs new file mode 100644 index 0000000..e478c7a --- /dev/null +++ b/ts-rs/tests/macro/export_here.rs @@ -0,0 +1,31 @@ +#![allow(dead_code)] + +use std::{concat, env, fs}; + +use ts_rs::TS; + +#[derive(TS)] +struct User { + name: String, + age: i32, + active: bool, +} + +#[test] +fn test_export_here() { + let dir = env::temp_dir(); + let file_path = dir.join("User.ts"); + ts_rs::export_here!(User => file_path.to_str().unwrap()); + + let expected_content = concat!( + "export interface User {\n", + " name: string;\n", + " age: number;\n", + " active: boolean;\n", + "}" + ); + + let actual_content = fs::read_to_string(file_path).unwrap(); + + assert_eq!(actual_content, expected_content); +} diff --git a/ts-rs/tests/unit.rs b/ts-rs/tests/unit.rs index 3f9ddd4..bc5cf0b 100644 --- a/ts-rs/tests/unit.rs +++ b/ts-rs/tests/unit.rs @@ -3,7 +3,15 @@ use ts_rs::TS; #[derive(TS)] struct Unit; +#[derive(TS)] +struct Unit2{} + +#[derive(TS)] +struct Unit3(); + #[test] fn test() { - assert_eq!("type Unit = null;", Unit::decl()) + assert_eq!("type Unit = null;", Unit::decl()); + assert_eq!("type Unit2 = null;", Unit2::decl()); + assert_eq!("type Unit3 = null;", Unit3::decl()); }