From e64e54804677f058b4f64a143c56966433f77ab6 Mon Sep 17 00:00:00 2001 From: Mattis Marjak Date: Thu, 30 Sep 2021 17:04:09 +0300 Subject: [PATCH 1/7] handle unit structs defined as Unit{} and Unit() --- macros/src/types/mod.rs | 14 +++++++++----- ts-rs/tests/unit.rs | 10 +++++++++- 2 files changed, 18 insertions(+), 6 deletions(-) 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/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()); } From 332a6dc44fae8bd0e30337b72807409f78190d09 Mon Sep 17 00:00:00 2001 From: Mattis Marjak Date: Thu, 30 Sep 2021 17:42:17 +0300 Subject: [PATCH 2/7] bytes-impl feature for TS derives on Bytes and BytesMut --- ts-rs/Cargo.toml | 4 +++- ts-rs/src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/ts-rs/Cargo.toml b/ts-rs/Cargo.toml index 4b88b39..9bb948a 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] @@ -22,4 +23,5 @@ ts-rs-macros = { version = "4.0.0", path = "../macros" } dprint-plugin-typescript = { version = "0.43" } chrono = { version = "0.4.19", optional = true } bigdecimal = {version = "0.1.2", features=["serde"], optional = true} -uuid = { version = "0.8.2", optional = true } \ No newline at end of file +uuid = { version = "0.8.2", optional = true } +bytes = { version = "1.0", optional = true } 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; From 9660e8565bb1838eea7be3738fe9dab833451393 Mon Sep 17 00:00:00 2001 From: Michael Bock Date: Mon, 25 Oct 2021 14:18:34 +0200 Subject: [PATCH 3/7] Add export_here macro --- ts-rs/src/export.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/ts-rs/src/export.rs b/ts-rs/src/export.rs index 21d3175..72008d8 100644 --- a/ts-rs/src/export.rs +++ b/ts-rs/src/export.rs @@ -44,9 +44,28 @@ use crate::TS; /// for `bindings/declared.d.ts`. #[macro_export] macro_rules! export { - ($($(($decl:ident))? $($p:path),+ => $l:literal),* $(,)?) => { + ($($arg:tt)*) => { #[cfg(test)] #[test] + ts_rs::export_core!($($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 then a test. +#[macro_export] +macro_rules! export_here { + ($($arg:tt)*) => { + ts_rs::export_core!($($arg)*); + export_typescript(); + }; +} + +#[macro_export] +macro_rules! export_core { + ($($(($decl:ident))? $($p:path),+ => $l:literal),* $(,)?) => { + //#[cfg(test)] + //#[test] fn export_typescript() { use std::fmt::Write; use std::collections::{BTreeMap as __BTreeMap, BTreeSet as __BTreeSet}; From bf7519a50925e3ee9043c035988efa8e3f82f7cd Mon Sep 17 00:00:00 2001 From: Michael Bock Date: Mon, 25 Oct 2021 14:23:13 +0200 Subject: [PATCH 4/7] Remove comments --- ts-rs/src/export.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/ts-rs/src/export.rs b/ts-rs/src/export.rs index 72008d8..56f1da6 100644 --- a/ts-rs/src/export.rs +++ b/ts-rs/src/export.rs @@ -64,8 +64,6 @@ macro_rules! export_here { #[macro_export] macro_rules! export_core { ($($(($decl:ident))? $($p:path),+ => $l:literal),* $(,)?) => { - //#[cfg(test)] - //#[test] fn export_typescript() { use std::fmt::Write; use std::collections::{BTreeMap as __BTreeMap, BTreeSet as __BTreeSet}; From 153f3d198efe6d5129d53e4b614710f298903c6d Mon Sep 17 00:00:00 2001 From: Michael Bock Date: Mon, 25 Oct 2021 14:23:45 +0200 Subject: [PATCH 5/7] Fix typo --- ts-rs/src/export.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ts-rs/src/export.rs b/ts-rs/src/export.rs index 56f1da6..fb11593 100644 --- a/ts-rs/src/export.rs +++ b/ts-rs/src/export.rs @@ -52,7 +52,7 @@ macro_rules! export { } /// 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 then a test. +/// 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 { ($($arg:tt)*) => { From 5d1d2d62fad4df655cfb8596a761bd26e191f066 Mon Sep 17 00:00:00 2001 From: Michael Bock Date: Mon, 25 Oct 2021 16:25:51 +0200 Subject: [PATCH 6/7] Generate code block instead of function - removes export_core --- ts-rs/src/export.rs | 14 ++++---------- ts-rs/tests/macro.rs | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 10 deletions(-) create mode 100644 ts-rs/tests/macro.rs diff --git a/ts-rs/src/export.rs b/ts-rs/src/export.rs index fb11593..dbbfb2c 100644 --- a/ts-rs/src/export.rs +++ b/ts-rs/src/export.rs @@ -47,7 +47,9 @@ macro_rules! export { ($($arg:tt)*) => { #[cfg(test)] #[test] - ts_rs::export_core!($($arg)*); + fn export_typescript() { + ts_rs::export_here!($($arg)*) + } }; } @@ -55,16 +57,8 @@ macro_rules! export { /// 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 { - ($($arg:tt)*) => { - ts_rs::export_core!($($arg)*); - export_typescript(); - }; -} - -#[macro_export] -macro_rules! export_core { ($($(($decl:ident))? $($p:path),+ => $l:literal),* $(,)?) => { - fn export_typescript() { + { use std::fmt::Write; use std::collections::{BTreeMap as __BTreeMap, BTreeSet as __BTreeSet}; diff --git a/ts-rs/tests/macro.rs b/ts-rs/tests/macro.rs new file mode 100644 index 0000000..d849606 --- /dev/null +++ b/ts-rs/tests/macro.rs @@ -0,0 +1,14 @@ +use ts_rs::TS; + +#[derive(TS)] +struct T { + foo: String, +} + +#[test] +fn test_indented() { + println!("hello test"); + + ts_rs::export_here!(T => "a.ts"); + assert_eq!(1, 1); +} From 55028ff84d321335ef4d7f9f452fcf2dc727e78c Mon Sep 17 00:00:00 2001 From: Michael Bock Date: Mon, 25 Oct 2021 18:07:33 +0200 Subject: [PATCH 7/7] Add test for export_here. --- ts-rs/src/export.rs | 2 +- ts-rs/tests/macro.rs | 14 -------------- ts-rs/tests/macro/export_here.rs | 31 +++++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 15 deletions(-) delete mode 100644 ts-rs/tests/macro.rs create mode 100644 ts-rs/tests/macro/export_here.rs diff --git a/ts-rs/src/export.rs b/ts-rs/src/export.rs index dbbfb2c..c305fb8 100644 --- a/ts-rs/src/export.rs +++ b/ts-rs/src/export.rs @@ -57,7 +57,7 @@ macro_rules! export { /// 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:literal),* $(,)?) => { + ($($(($decl:ident))? $($p:path),+ => $l:expr),* $(,)?) => { { use std::fmt::Write; use std::collections::{BTreeMap as __BTreeMap, BTreeSet as __BTreeSet}; diff --git a/ts-rs/tests/macro.rs b/ts-rs/tests/macro.rs deleted file mode 100644 index d849606..0000000 --- a/ts-rs/tests/macro.rs +++ /dev/null @@ -1,14 +0,0 @@ -use ts_rs::TS; - -#[derive(TS)] -struct T { - foo: String, -} - -#[test] -fn test_indented() { - println!("hello test"); - - ts_rs::export_here!(T => "a.ts"); - assert_eq!(1, 1); -} 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); +}