Skip to content

Commit a54c53b

Browse files
authored
#60 fix hex, oct and binary numbers (#71)
* feat: add parse_number_autoradix utility and update enum handling * Add new test to test_all.sh
1 parent 2500879 commit a54c53b

File tree

7 files changed

+135
-1
lines changed

7 files changed

+135
-1
lines changed

src/to_typescript/enums.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::utils::parse_number_autoradix;
12
use crate::{typescript::convert_type, utils, BuildState};
23
use convert_case::{Case, Casing};
34
use syn::__private::ToTokens;
@@ -151,7 +152,7 @@ fn add_numeric_enum(
151152
variant.ident.unraw().to_string()
152153
};
153154
if let Some((_, disc)) = variant.discriminant {
154-
if let Ok(new_disc) = disc.to_token_stream().to_string().parse::<i32>() {
155+
if let Some(new_disc) = parse_number_autoradix(disc.to_token_stream().to_string()) {
155156
num = new_disc;
156157
}
157158
}

src/utils.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,30 @@ pub(crate) fn parse_serde_case(
273273
.map(|(_, rule)| *rule)
274274
})
275275
}
276+
277+
/// Parse a number automaticly handling radix like rust does.
278+
/// 0x - Hex notation
279+
/// 0o - Octal notation
280+
/// 0b - Binary notation
281+
pub fn parse_number_autoradix(input: String) -> Option<i32> {
282+
let normalized = input.to_lowercase().replace("_", "");
283+
let trimmed = normalized.trim();
284+
285+
if let Some(hex) = trimmed.strip_prefix("0x") {
286+
i32::from_str_radix(&hex, 16).ok()
287+
} else if let Some(oct) = trimmed.strip_prefix("0o") {
288+
i32::from_str_radix(&oct, 8).ok()
289+
} else if let Some(bin) = trimmed.strip_prefix("0b") {
290+
i32::from_str_radix(&bin, 2).ok()
291+
} else {
292+
trimmed.parse::<i32>().ok()
293+
}
294+
}
295+
296+
#[test]
297+
fn test_parse_number() {
298+
assert_eq!(parse_number_autoradix("2_3".into()), Some(23));
299+
assert_eq!(parse_number_autoradix("0xf".into()), Some(15));
300+
assert_eq!(parse_number_autoradix("0b11".into()), Some(3));
301+
assert_eq!(parse_number_autoradix("0o1_1".into()), Some(0o1_1));
302+
}

test/issue-60/rust.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#[derive(Serialize_repr)]
2+
#[repr(u16)]
3+
#[tsync]
4+
pub enum KeyCode {
5+
Space = 32,
6+
Apostrophe = 39,
7+
Comma = 44,
8+
Minus = 45,
9+
}
10+
11+
#[derive(Serialize_repr)]
12+
#[repr(u16)]
13+
#[tsync]
14+
pub enum KeyCodeHex {
15+
Space = 0x0020,
16+
Apostrophe = 0x0027,
17+
Comma = 0x002c,
18+
Minus = 0x002d,
19+
}
20+
21+
#[derive(Serialize_repr)]
22+
#[repr(u16)]
23+
#[tsync]
24+
pub enum KeyCodeOct {
25+
Space = 0o40,
26+
Apostrophe = 0o47,
27+
Comma = 0o54,
28+
Minus = 0o55,
29+
}
30+
31+
#[derive(Serialize_repr)]
32+
#[repr(u16)]
33+
#[tsync]
34+
pub enum KeyCodeBin {
35+
Space = 0b100000,
36+
Apostrophe = 0b100111,
37+
Comma = 0b101100,
38+
Minus = 0b101101,
39+
}

test/issue-60/tsync.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
4+
5+
cd $SCRIPT_DIR
6+
7+
cargo run -- -i rust.rs -o typescript.d.ts
8+
cargo run -- -i rust.rs -o typescript.ts

test/issue-60/typescript.d.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* This file is generated and managed by tsync */
2+
3+
declare enum KeyCode {
4+
Space = 32,
5+
Apostrophe = 39,
6+
Comma = 44,
7+
Minus = 45,
8+
}
9+
10+
declare enum KeyCodeHex {
11+
Space = 32,
12+
Apostrophe = 39,
13+
Comma = 44,
14+
Minus = 45,
15+
}
16+
17+
declare enum KeyCodeOct {
18+
Space = 32,
19+
Apostrophe = 39,
20+
Comma = 44,
21+
Minus = 45,
22+
}
23+
24+
declare enum KeyCodeBin {
25+
Space = 32,
26+
Apostrophe = 39,
27+
Comma = 44,
28+
Minus = 45,
29+
}

test/issue-60/typescript.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* This file is generated and managed by tsync */
2+
3+
export enum KeyCode {
4+
Space = 32,
5+
Apostrophe = 39,
6+
Comma = 44,
7+
Minus = 45,
8+
}
9+
10+
export enum KeyCodeHex {
11+
Space = 32,
12+
Apostrophe = 39,
13+
Comma = 44,
14+
Minus = 45,
15+
}
16+
17+
export enum KeyCodeOct {
18+
Space = 32,
19+
Apostrophe = 39,
20+
Comma = 44,
21+
Minus = 45,
22+
}
23+
24+
export enum KeyCodeBin {
25+
Space = 32,
26+
Apostrophe = 39,
27+
Comma = 44,
28+
Minus = 45,
29+
}

test/test_all.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ cd $SCRIPT_DIR
1717
./issue-43/tsync.sh
1818
./issue-55/tsync.sh
1919
./issue-58/tsync.sh
20+
./issue-60/tsync.sh
2021
./issue-63/tsync.sh
2122
./issue-65-untagged-enums/tsync.sh
2223
./raw_identifiers/tsync.sh

0 commit comments

Comments
 (0)