From 6203f99dd7b59b5f0f70fdbfbb9665abbae46091 Mon Sep 17 00:00:00 2001 From: sleepycatcoding <131554884+sleepycatcoding@users.noreply.github.com> Date: Fri, 26 Apr 2024 15:53:12 +0300 Subject: [PATCH] frontend-utils: Add integer parse methods --- frontend-utils/src/parse.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/frontend-utils/src/parse.rs b/frontend-utils/src/parse.rs index 7bf7a3517..9e647a33d 100644 --- a/frontend-utils/src/parse.rs +++ b/frontend-utils/src/parse.rs @@ -237,6 +237,14 @@ pub trait ReadExt<'a> { result } + + fn get_integer(&'a self, cx: &mut ParseContext, key: &'static str) -> Option { + cx.push_key(key); + let result = self.get_impl(key).and_then(|x| x.as_integer_or_warn(cx)); + cx.pop_key(); + + result + } } /// Extension trait to provide casting methods with warning capabilities. @@ -244,6 +252,7 @@ pub trait ItemExt<'a> { fn as_str_or_warn(&'a self, cx: &mut ParseContext) -> Option<&'a str>; fn as_bool_or_warn(&self, cx: &mut ParseContext) -> Option; fn as_float_or_warn(&self, cx: &mut ParseContext) -> Option; + fn as_integer_or_warn(&self, cx: &mut ParseContext) -> Option; } // Implementations for toml_edit types. @@ -302,4 +311,14 @@ impl<'a> ItemExt<'a> for Item { None } + + fn as_integer_or_warn(&self, cx: &mut ParseContext) -> Option { + if let Some(value) = self.as_integer() { + return Some(value); + } else { + cx.unexpected_type("integer", self.type_name()); + } + + None + } }