frontend-utils: Add integer parse methods

This commit is contained in:
sleepycatcoding 2024-04-26 15:53:12 +03:00 committed by Nathan Adams
parent de003e12e7
commit 6203f99dd7
1 changed files with 19 additions and 0 deletions

View File

@ -237,6 +237,14 @@ pub trait ReadExt<'a> {
result
}
fn get_integer(&'a self, cx: &mut ParseContext, key: &'static str) -> Option<i64> {
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<bool>;
fn as_float_or_warn(&self, cx: &mut ParseContext) -> Option<f64>;
fn as_integer_or_warn(&self, cx: &mut ParseContext) -> Option<i64>;
}
// 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<i64> {
if let Some(value) = self.as_integer() {
return Some(value);
} else {
cx.unexpected_type("integer", self.type_name());
}
None
}
}