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

Ranges are now iterables #493

Merged
merged 2 commits into from
Apr 11, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ For upgrade instructions read the [UPDATING](UPDATING.md) guide.
- The parser no longer panics when using dotted assignments in unexpected places. #479
- The CLI now enables unicode support by default.
- `Value::from_serializable` is now `Value::from_serialize`.
- Ranges are now iterables and no longer sequences. #493

## 1.0.20

Expand Down
10 changes: 6 additions & 4 deletions minijinja/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,17 @@ mod builtins {
///
/// This function will refuse to create ranges over 10.000 items.
#[cfg_attr(docsrs, doc(cfg(feature = "builtins")))]
pub fn range(lower: u32, upper: Option<u32>, step: Option<u32>) -> Result<Vec<u32>, Error> {
fn to_result<I: ExactSizeIterator<Item = u32>>(i: I) -> Result<Vec<u32>, Error> {
if i.len() > 10000 {
pub fn range(lower: u32, upper: Option<u32>, step: Option<u32>) -> Result<Value, Error> {
fn to_result<I: ExactSizeIterator<Item = u32> + Send + Sync + Clone + 'static>(
i: I,
) -> Result<Value, Error> {
if i.len() > 100000 {
Err(Error::new(
ErrorKind::InvalidOperation,
"range has too many elements",
))
} else {
Ok(i.collect())
Ok(Value::make_iterable(move || i.clone()))
}
}

Expand Down
2 changes: 1 addition & 1 deletion minijinja/tests/inputs/err_bad_range.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{}
---
{{ range(10001) }}
{{ range(100001) }}
5 changes: 5 additions & 0 deletions minijinja/tests/inputs/functions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{}
---
short-range: {{ range(10) }}
range-is-iterable: {{ range(10) is iterable }}
range-is-not-a-sequence: {{ range(10) is not sequence }}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: minijinja/tests/test_templates.rs
description: "{{ range(10001) }}"
description: "{{ range(100001) }}"
info: {}
input_file: minijinja/tests/inputs/err_bad_range.txt
---
Expand All @@ -15,11 +15,10 @@ Error {

invalid operation: range has too many elements (in err_bad_range.txt:1)
------------------------------ err_bad_range.txt ------------------------------
1 > {{ range(10001) }}
i ^^^^^^^^^^^^ invalid operation
1 > {{ range(100001) }}
i ^^^^^^^^^^^^^ invalid operation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Referenced variables: {
range: minijinja::functions::builtins::range,
}
-------------------------------------------------------------------------------

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: minijinja/tests/test_templates.rs
description: "short-range: {{ range(10) }}\nrange-is-iterable: {{ range(10) is iterable }}\nrange-is-not-a-sequence: {{ range(10) is not sequence }}"
info: {}
input_file: minijinja/tests/inputs/functions.txt
---
short-range: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range-is-iterable: true
range-is-not-a-sequence: true