mirror of
https://github.com/xazukx/ts-rs.git
synced 2026-11-19 22:40:25 +00:00
Merge branch 'main' into main
This commit is contained in:
@@ -29,11 +29,15 @@ fn type_def(
|
||||
generics: &Generics,
|
||||
) -> Result<DerivedTS> {
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -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 }
|
||||
+12
-1
@@ -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};
|
||||
|
||||
|
||||
@@ -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<number>".to_owned()
|
||||
}
|
||||
|
||||
fn inline(indent: usize) -> String {
|
||||
format!("Array<{}>", u8::inline(indent))
|
||||
}
|
||||
|
||||
fn dependencies() -> Vec<(TypeId, String)> {
|
||||
vec![(TypeId::of::<u8>(), u8::name())]
|
||||
}
|
||||
|
||||
fn transparent() -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl TS for bytes::BytesMut {
|
||||
fn name() -> String {
|
||||
"Array<number>".to_owned()
|
||||
}
|
||||
|
||||
fn inline(indent: usize) -> String {
|
||||
format!("Array<{}>", u8::inline(indent))
|
||||
}
|
||||
|
||||
fn dependencies() -> Vec<(TypeId, String)> {
|
||||
vec![(TypeId::of::<u8>(), u8::name())]
|
||||
}
|
||||
|
||||
fn transparent() -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "chrono-impl")]
|
||||
mod chrono_impls {
|
||||
use super::TS;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
+9
-1
@@ -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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user