From dc62866577281a8bb1084ed36756b969acdeb22a Mon Sep 17 00:00:00 2001 From: hodasemi Date: Thu, 13 Apr 2023 14:30:48 +0200 Subject: [PATCH] Keep hex representation --- build.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/build.rs b/build.rs index 3844e13..9542b13 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,21 @@ +use std::fmt; + +enum Num<'a> { + Hex(&'a str), + Dec(i32), +} + +impl<'a> fmt::Display for Num<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Num::Hex(h) => write!(f, "{h}"), + Num::Dec(d) => write!(f, "{d}"), + } + } +} + enum Variable<'a> { - Int(&'a str, i32), + Int(&'a str, Num<'a>), String(&'a str, &'a str), } @@ -33,9 +49,9 @@ fn create_config_struct(content: &str, path: &str, struct_name: &str) { Variable::Int( name, if value.starts_with("0x") { - i32::from_str_radix(value.trim_start_matches("0x"), 16).unwrap() + Num::Hex(value) } else { - str::parse(value).unwrap() + Num::Dec(str::parse(value).unwrap()) }, ) }