Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an Value::is_integer method #580

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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