Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/to_typescript/enums.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::utils::parse_number_autoradix;
use crate::{typescript::convert_type, utils, BuildState};
use convert_case::{Case, Casing};
use syn::__private::ToTokens;
Expand Down Expand Up @@ -151,7 +152,7 @@ fn add_numeric_enum(
variant.ident.unraw().to_string()
};
if let Some((_, disc)) = variant.discriminant {
if let Ok(new_disc) = disc.to_token_stream().to_string().parse::<i32>() {
if let Some(new_disc) = parse_number_autoradix(disc.to_token_stream().to_string()) {
num = new_disc;
}
}
Expand Down
27 changes: 27 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,30 @@ pub(crate) fn parse_serde_case(
.map(|(_, rule)| *rule)
})
}

/// Parse a number automaticly handling radix like rust does.
/// 0x - Hex notation
/// 0o - Octal notation
/// 0b - Binary notation
pub fn parse_number_autoradix(input: String) -> Option<i32> {
let normalized = input.to_lowercase().replace("_", "");
let trimmed = normalized.trim();

if let Some(hex) = trimmed.strip_prefix("0x") {
i32::from_str_radix(&hex, 16).ok()
} else if let Some(oct) = trimmed.strip_prefix("0o") {
i32::from_str_radix(&oct, 8).ok()
} else if let Some(bin) = trimmed.strip_prefix("0b") {
i32::from_str_radix(&bin, 2).ok()
} else {
trimmed.parse::<i32>().ok()
}
}

#[test]
fn test_parse_number() {
assert_eq!(parse_number_autoradix("2_3".into()), Some(23));
assert_eq!(parse_number_autoradix("0xf".into()), Some(15));
assert_eq!(parse_number_autoradix("0b11".into()), Some(3));
assert_eq!(parse_number_autoradix("0o1_1".into()), Some(0o1_1));
}
39 changes: 39 additions & 0 deletions test/issue-60/rust.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#[derive(Serialize_repr)]
#[repr(u16)]
#[tsync]
pub enum KeyCode {
Space = 32,
Apostrophe = 39,
Comma = 44,
Minus = 45,
}

#[derive(Serialize_repr)]
#[repr(u16)]
#[tsync]
pub enum KeyCodeHex {
Space = 0x0020,
Apostrophe = 0x0027,
Comma = 0x002c,
Minus = 0x002d,
}

#[derive(Serialize_repr)]
#[repr(u16)]
#[tsync]
pub enum KeyCodeOct {
Space = 0o40,
Apostrophe = 0o47,
Comma = 0o54,
Minus = 0o55,
}

#[derive(Serialize_repr)]
#[repr(u16)]
#[tsync]
pub enum KeyCodeBin {
Space = 0b100000,
Apostrophe = 0b100111,
Comma = 0b101100,
Minus = 0b101101,
}
8 changes: 8 additions & 0 deletions test/issue-60/tsync.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

cd $SCRIPT_DIR

cargo run -- -i rust.rs -o typescript.d.ts
cargo run -- -i rust.rs -o typescript.ts
29 changes: 29 additions & 0 deletions test/issue-60/typescript.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* This file is generated and managed by tsync */

declare enum KeyCode {
Space = 32,
Apostrophe = 39,
Comma = 44,
Minus = 45,
}

declare enum KeyCodeHex {
Space = 32,
Apostrophe = 39,
Comma = 44,
Minus = 45,
}

declare enum KeyCodeOct {
Space = 32,
Apostrophe = 39,
Comma = 44,
Minus = 45,
}

declare enum KeyCodeBin {
Space = 32,
Apostrophe = 39,
Comma = 44,
Minus = 45,
}
29 changes: 29 additions & 0 deletions test/issue-60/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* This file is generated and managed by tsync */

export enum KeyCode {
Space = 32,
Apostrophe = 39,
Comma = 44,
Minus = 45,
}

export enum KeyCodeHex {
Space = 32,
Apostrophe = 39,
Comma = 44,
Minus = 45,
}

export enum KeyCodeOct {
Space = 32,
Apostrophe = 39,
Comma = 44,
Minus = 45,
}

export enum KeyCodeBin {
Space = 32,
Apostrophe = 39,
Comma = 44,
Minus = 45,
}
1 change: 1 addition & 0 deletions test/test_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ cd $SCRIPT_DIR
./issue-43/tsync.sh
./issue-55/tsync.sh
./issue-58/tsync.sh
./issue-60/tsync.sh
./issue-63/tsync.sh
./issue-65-untagged-enums/tsync.sh
./raw_identifiers/tsync.sh