diff --git a/src/to_typescript/enums.rs b/src/to_typescript/enums.rs index 3d72445..4269cbe 100644 --- a/src/to_typescript/enums.rs +++ b/src/to_typescript/enums.rs @@ -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; @@ -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::() { + if let Some(new_disc) = parse_number_autoradix(disc.to_token_stream().to_string()) { num = new_disc; } } diff --git a/src/utils.rs b/src/utils.rs index b7791b2..fa60d73 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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 { + 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::().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)); +} diff --git a/test/issue-60/rust.rs b/test/issue-60/rust.rs new file mode 100644 index 0000000..e0ab45e --- /dev/null +++ b/test/issue-60/rust.rs @@ -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, +} diff --git a/test/issue-60/tsync.sh b/test/issue-60/tsync.sh new file mode 100755 index 0000000..1764bd6 --- /dev/null +++ b/test/issue-60/tsync.sh @@ -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 \ No newline at end of file diff --git a/test/issue-60/typescript.d.ts b/test/issue-60/typescript.d.ts new file mode 100644 index 0000000..7049ee0 --- /dev/null +++ b/test/issue-60/typescript.d.ts @@ -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, +} diff --git a/test/issue-60/typescript.ts b/test/issue-60/typescript.ts new file mode 100644 index 0000000..4bef37d --- /dev/null +++ b/test/issue-60/typescript.ts @@ -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, +} diff --git a/test/test_all.sh b/test/test_all.sh index ab4571b..a182cdf 100755 --- a/test/test_all.sh +++ b/test/test_all.sh @@ -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