Skip to content

Commit

Permalink
frontend-utils: Add integer parse methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepycatcoding authored and Dinnerbone committed Apr 26, 2024
1 parent de003e1 commit 6203f99
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions frontend-utils/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,22 @@ 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.
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.
Expand Down Expand Up @@ -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
}
}

0 comments on commit 6203f99

Please sign in to comment.