Skip to content

Commit

Permalink
🐛 Fixed default rust type conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
nwrenger committed Aug 17, 2024
1 parent 996bcd1 commit 51a129f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ struct Age {
#[metadata]
#[derive(Serialize, Deserialize, Default)]
struct AgeInner {
age: u8,
// Thats quite big and also supported
/// It gets converted as a string on the TypeScript side
/// because a ts `number` cannot be greater than 64 bits
age: u128,
}

#[metadata]
Expand Down
10 changes: 6 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1289,9 +1289,11 @@ impl RustType {

match &self {
Self::BuiltIn(ty) => match ty.as_str() {
"str" | "String" => return Ok(ApiType::Unknown(String::from("string"))),
"char" | "str" | "String" | "u128" | "i128" | "f128" => {
return Ok(ApiType::Unknown(String::from("string")))
}
"usize" | "isize" | "u8" | "u16" | "u32" | "u64" | "i8" | "i16" | "i32" | "i64"
| "f32" | "f64" => return Ok(ApiType::Unknown(String::from("number"))),
| "f16" | "f32" | "f64" => return Ok(ApiType::Unknown(String::from("number"))),
"bool" => return Ok(ApiType::Unknown(String::from("boolean"))),
"()" => return Ok(ApiType::Unknown(String::from("void"))),
_ => {}
Expand Down Expand Up @@ -1435,7 +1437,7 @@ impl RustType {

const RUST_TYPES: &[&str] = &[
"bool", "char", "str", "u8", "u16", "u32", "u64", "u128", "i8", "i16", "i32", "i64", "i128",
"usize", "isize", "f32", "f64", "String",
"usize", "isize", "f16", "f32", "f64", "f128", "String",
];

const SKIP_TYPES: &[&str] = &["State", "Headers", "Bytes", "Request", "Extension"];
Expand Down Expand Up @@ -1510,7 +1512,7 @@ fn to_rust_type(ty: &syn::Type, custom: &[String]) -> Option<RustType> {

syn::Type::Tuple(type_tuple) => {
if type_tuple.elems.is_empty() {
return Some(RustType::BuiltIn("()".to_string()));
return Some(RustType::BuiltIn(String::from("()")));
}
let inner_types: Vec<RustType> = type_tuple
.elems
Expand Down
6 changes: 5 additions & 1 deletion tests/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ namespace api {
}

export interface AgeInner {
age: number;
/**
It gets converted as a string on the TypeScript side
because a ts `number` cannot be greater than 64 bits
*/
age: string;
}

export interface Hello<T, S> {
Expand Down
5 changes: 4 additions & 1 deletion tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ struct Age {
#[metadata]
#[derive(Serialize, Deserialize, Default)]
struct AgeInner {
age: u8,
// Thats quite big and also supported
/// It gets converted as a string on the TypeScript side
/// because a ts `number` cannot be greater than 64 bits
age: u128,
}

#[metadata]
Expand Down

0 comments on commit 51a129f

Please sign in to comment.