From 973b8a8cd4c0df29a278d57eb31fa75527f39a2a Mon Sep 17 00:00:00 2001 From: DeFiDude <59237470+DeFiDude@users.noreply.github.com> Date: Wed, 18 Nov 2026 21:47:32 -0700 Subject: [PATCH] config: integer keys accept underscore digit groups, as Python int() does --- crates/rns-runtime/src/config.rs | 42 +++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/crates/rns-runtime/src/config.rs b/crates/rns-runtime/src/config.rs index db50b1a..4c44d89 100644 --- a/crates/rns-runtime/src/config.rs +++ b/crates/rns-runtime/src/config.rs @@ -94,14 +94,13 @@ impl ConfigSection { .find(|key| self.has(key) && self.get_bool(key).is_none()) } + /// Parses like Python `int()`, which ConfigObj `as_int` uses. pub fn get_int(&self, key: &str) -> Option { - let v = self.get(key)?; - v.parse().ok() + python_number_text(self.get(key)?)?.parse().ok() } pub fn get_uint(&self, key: &str) -> Option { - let v = self.get(key)?; - v.parse().ok() + python_number_text(self.get(key)?)?.parse().ok() } /// Parses like Python `float()`, which ConfigObj `as_float` uses. @@ -595,9 +594,15 @@ fn strip_comment(line: &str) -> &str { /// Python `float()` also accepts single underscores between digits (PEP 515). fn parse_python_float(s: &str) -> Option { + python_number_text(s)?.parse().ok() +} + +/// Number text as Python's `int()` and `float()` read it: surrounding +/// whitespace dropped, and `_` allowed only between two digits. +fn python_number_text(s: &str) -> Option> { let s = s.trim(); if !s.contains('_') { - return s.parse().ok(); + return Some(s.into()); } let bytes = s.as_bytes(); let grouped = bytes.iter().enumerate().all(|(i, &b)| { @@ -606,10 +611,7 @@ fn parse_python_float(s: &str) -> Option { && bytes[i - 1].is_ascii_digit() && bytes.get(i + 1).is_some_and(u8::is_ascii_digit)) }); - if !grouped { - return None; - } - s.replace('_', "").parse().ok() + grouped.then(|| s.replace('_', "").into()) } fn parse_bool(s: &str) -> Option { @@ -851,6 +853,28 @@ mod tests { assert!(sec.get_float("k").unwrap().is_nan()); } + #[test] + fn get_int_accepts_python_int_syntax() { + let mut sec = ConfigSection::new(); + for (raw, expected) in [ + ("1_0", Some(10)), + ("1_000_000", Some(1_000_000)), + ("+4", Some(4)), + ("-1_2", Some(-12)), + (" 7 ", Some(7)), + ("1__0", None), + ("_1", None), + ("1_", None), + ("1.0", None), + ("", None), + ] { + sec.set("k", raw); + assert_eq!(sec.get_int("k"), expected, "{raw:?}"); + } + sec.set("k", "1_0"); + assert_eq!(sec.get_uint("k"), Some(10)); + } + #[test] fn test_parse_empty() { let config = Config::parse("").unwrap();