Skip to content

Commit

Permalink
Add an Value::is_integer method (#580)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Sep 16, 2024
1 parent 1416e15 commit b3002bb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ All notable changes to MiniJinja are documented here.
values. #571
- Changed sort order of `Ord` to avoid accidentally non total order
that could cause panics on Rust 1.81. #579
- Added a `Value::is_integer` method to allow a user to tell floats
and true integers apart. #580

## 2.2.0

Expand Down
8 changes: 1 addition & 7 deletions minijinja/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,7 @@ mod builtins {
/// ```
#[cfg_attr(docsrs, doc(cfg(feature = "builtins")))]
pub fn is_integer(v: Value) -> bool {
matches!(
v.0,
crate::value::ValueRepr::U64(_)
| crate::value::ValueRepr::I64(_)
| crate::value::ValueRepr::U128(_)
| crate::value::ValueRepr::I128(_)
)
v.is_integer()
}

/// Checks if this value is a float
Expand Down
11 changes: 11 additions & 0 deletions minijinja/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,17 @@ impl Value {
)
}

/// Returns true if the number is a real integer.
///
/// This can be used to distinguish `42` from `42.0`. For the most part
/// the engine keeps these the same.
pub fn is_integer(&self) -> bool {
matches!(
self.0,
ValueRepr::U64(_) | ValueRepr::I64(_) | ValueRepr::I128(_) | ValueRepr::U128(_)
)
}

/// Returns `true` if the map represents keyword arguments.
pub fn is_kwargs(&self) -> bool {
Kwargs::extract(self).is_some()
Expand Down

0 comments on commit b3002bb

Please sign in to comment.