Skip to content

Commit

Permalink
feat: map Vec<u8> to Uint8Array (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
ImJeremyHe authored Oct 19, 2023
1 parent 1c4c51b commit ef105e9
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
authors = ["ImJeremyHe<[email protected]>"]
edition = "2018"
name = "gents"
version = "0.5.0"
version = "0.6.0"
license = "MIT"
description = "generate Typescript interfaces from Rust code"
repository = "https://github.com/ImJeremyHe/gents"
keywords = ["Typescript", "interface", "ts-rs", "Rust", "wasm"]

[dev-dependencies]
gents_derives = {version = "0.5.0", path = "./derives"}
gents_derives = {version = "0.6.0", path = "./derives"}
2 changes: 1 addition & 1 deletion derives/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gents_derives"
version = "0.5.0"
version = "0.6.0"
description = "provides some macros for gents"
authors = ["ImJeremyHe<[email protected]>"]
license = "MIT"
Expand Down
43 changes: 30 additions & 13 deletions src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@ use crate::utils::remove_ext;
pub trait TS {
fn _register(manager: &mut DescriptorManager) -> usize;
// The name of this Rust type in Typescript.
// u8 -> number
// f64 -> number
fn _ts_name() -> String;
fn _is_optional() -> bool {
false
}
// A special field for us to know about this type. It is always
// used in the Rust builtin types like mapping `Vec<u8>`` to `Uint8Array`
// rather than `readonly number[]`.
fn _tag() -> Option<&'static str> {
None
}
}

#[derive(Default)]
Expand Down Expand Up @@ -201,7 +209,7 @@ pub struct FieldDescriptor {
}

macro_rules! impl_builtin {
($i: ident, $l: literal) => {
($i: ident, $l: literal, $t: literal) => {
impl TS for $i {
fn _register(manager: &mut DescriptorManager) -> usize {
let type_id = TypeId::of::<$i>();
Expand All @@ -214,22 +222,26 @@ macro_rules! impl_builtin {
fn _ts_name() -> String {
$l.to_string()
}

fn _tag() -> Option<&'static str> {
Some($t)
}
}
};
}

impl_builtin!(u8, "number");
impl_builtin!(u16, "number");
impl_builtin!(u32, "number");
impl_builtin!(u64, "number");
impl_builtin!(usize, "number");
impl_builtin!(i8, "number");
impl_builtin!(i32, "number");
impl_builtin!(i64, "number");
impl_builtin!(f32, "number");
impl_builtin!(f64, "number");
impl_builtin!(String, "string");
impl_builtin!(bool, "boolean");
impl_builtin!(u8, "number", "u8");
impl_builtin!(u16, "number", "u16");
impl_builtin!(u32, "number", "u32");
impl_builtin!(u64, "number", "u64");
impl_builtin!(usize, "number", "usize");
impl_builtin!(i8, "number", "i8");
impl_builtin!(i32, "number", "i32");
impl_builtin!(i64, "number", "i64");
impl_builtin!(f32, "number", "f32");
impl_builtin!(f64, "number", "f64");
impl_builtin!(String, "string", "string");
impl_builtin!(bool, "boolean", "bool");

impl<T: TS + 'static> TS for Vec<T> {
fn _register(manager: &mut DescriptorManager) -> usize {
Expand All @@ -244,6 +256,11 @@ impl<T: TS + 'static> TS for Vec<T> {
}

fn _ts_name() -> String {
if let Some(t) = T::_tag() {
if t == "u8" {
return "Uint8Array".to_string();
}
}
format!("readonly {}[]", T::_ts_name())
}
}
Expand Down
20 changes: 20 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,26 @@ export interface StructWithComments {
// field comment1
// field comment2
fieldWithComment: number
}"#
);
}

#[test]
fn test_uint8array() {
#[derive(TS)]
#[ts(file_name = "file.ts", rename_all = "camelCase")]
pub struct File {
pub data: Vec<u8>,
}

let mut manager = DescriptorManager::default();
File::_register(&mut manager);
let (file_name, content) = manager.gen_data().into_iter().next().unwrap();
assert_eq!(file_name, "file.ts");
assert_eq!(
content.trim(),
r#"export interface File {
data: Uint8Array
}"#
);
}
Expand Down

0 comments on commit ef105e9

Please sign in to comment.