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

Isolate chrono use from rest of "builtins" features #711

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Changelog
## TBD (2022-02-18)

- Move all `chrono` definition and use under `chrono` feature instead of bundling with `builtins`. This
allows optional use of `chrono` given it's not fully maintained.

## 1.15.0 (2021-11-03)

Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ tempfile = "3"

[features]
default = ["builtins"]
builtins = ["slug", "percent-encoding", "humansize", "chrono", "chrono-tz", "rand"]
builtins = ["slug", "percent-encoding", "humansize", "rand", "chrono", "chrono-tz"]
builtins-less-chrono = ["slug", "percent-encoding", "humansize", "rand"]
preserve_order = ["serde_json/preserve_order"]

[badges]
Expand Down
28 changes: 14 additions & 14 deletions src/builtins/filters/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use std::iter::FromIterator;

use crate::errors::{Error, Result};
use crate::utils::render_to_string;
#[cfg(feature = "builtins")]
#[cfg(feature = "chrono")]
use chrono::{
format::{Item, StrftimeItems},
DateTime, FixedOffset, NaiveDate, NaiveDateTime, Utc,
};
#[cfg(feature = "builtins")]
#[cfg(feature = "chrono")]
use chrono_tz::Tz;
use serde_json::value::{to_value, Value};
use serde_json::{to_string, to_string_pretty};
Expand Down Expand Up @@ -65,7 +65,7 @@ pub fn json_encode(value: &Value, args: &HashMap<String, Value>) -> Result<Value
///
/// a full reference for the time formatting syntax is available
/// on [chrono docs](https://lifthrasiir.github.io/rust-chrono/chrono/format/strftime/index.html)
#[cfg(feature = "builtins")]
#[cfg(feature = "chrono")]
pub fn date(value: &Value, args: &HashMap<String, Value>) -> Result<Value> {
let format = match args.get("format") {
Some(val) => try_get_value!("date", "format", String, val),
Expand Down Expand Up @@ -151,7 +151,7 @@ pub fn as_str(value: &Value, _: &HashMap<String, Value>) -> Result<Value> {
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "builtins")]
#[cfg(feature = "chrono")]
use chrono::{DateTime, Local};
use serde_json;
use serde_json::value::to_value;
Expand Down Expand Up @@ -232,7 +232,7 @@ mod tests {
);
}

#[cfg(feature = "builtins")]
#[cfg(feature = "chrono")]
#[test]
fn date_default() {
let args = HashMap::new();
Expand All @@ -241,7 +241,7 @@ mod tests {
assert_eq!(result.unwrap(), to_value("2016-12-26").unwrap());
}

#[cfg(feature = "builtins")]
#[cfg(feature = "chrono")]
#[test]
fn date_custom_format() {
let mut args = HashMap::new();
Expand All @@ -253,7 +253,7 @@ mod tests {

// https://zola.discourse.group/t/can-i-generate-a-random-number-within-a-range/238?u=keats
// https://github.com/chronotope/chrono/issues/47
#[cfg(feature = "builtins")]
#[cfg(feature = "chrono")]
#[test]
fn date_errors_on_incorrect_format() {
let mut args = HashMap::new();
Expand All @@ -262,7 +262,7 @@ mod tests {
assert!(result.is_err());
}

#[cfg(feature = "builtins")]
#[cfg(feature = "chrono")]
#[test]
fn date_rfc3339() {
let args = HashMap::new();
Expand All @@ -272,7 +272,7 @@ mod tests {
assert_eq!(result.unwrap(), to_value(dt.format("%Y-%m-%d").to_string()).unwrap());
}

#[cfg(feature = "builtins")]
#[cfg(feature = "chrono")]
#[test]
fn date_rfc3339_preserves_timezone() {
let mut args = HashMap::new();
Expand All @@ -282,7 +282,7 @@ mod tests {
assert_eq!(result.unwrap(), to_value("1996-12-19 -0800").unwrap());
}

#[cfg(feature = "builtins")]
#[cfg(feature = "chrono")]
#[test]
fn date_yyyy_mm_dd() {
let mut args = HashMap::new();
Expand All @@ -292,7 +292,7 @@ mod tests {
assert_eq!(result.unwrap(), to_value("Sun, 05 Mar 2017 00:00:00 +0000").unwrap());
}

#[cfg(feature = "builtins")]
#[cfg(feature = "chrono")]
#[test]
fn date_from_naive_datetime() {
let mut args = HashMap::new();
Expand All @@ -304,7 +304,7 @@ mod tests {
}

// https://github.com/getzola/zola/issues/1279
#[cfg(feature = "builtins")]
#[cfg(feature = "chrono")]
#[test]
fn date_format_doesnt_panic() {
let mut args = HashMap::new();
Expand All @@ -313,7 +313,7 @@ mod tests {
assert!(result.is_ok());
}

#[cfg(feature = "builtins")]
#[cfg(feature = "chrono")]
#[test]
fn date_with_timezone() {
let mut args = HashMap::new();
Expand All @@ -323,7 +323,7 @@ mod tests {
assert_eq!(result.unwrap(), to_value("2019-09-18").unwrap());
}

#[cfg(feature = "builtins")]
#[cfg(feature = "chrono")]
#[test]
fn date_with_invalid_timezone() {
let mut args = HashMap::new();
Expand Down
14 changes: 8 additions & 6 deletions src/builtins/functions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::collections::HashMap;

#[cfg(feature = "builtins")]
use chrono::prelude::*;
#[cfg(feature = "chrono")]
use chrono::{
Utc, Local,
};
#[cfg(feature = "builtins")]
use rand::Rng;
use serde_json::value::{from_value, to_value, Value};
Expand Down Expand Up @@ -83,7 +85,7 @@ pub fn range(args: &HashMap<String, Value>) -> Result<Value> {
Ok(to_value(res).unwrap())
}

#[cfg(feature = "builtins")]
#[cfg(feature = "chrono")]
pub fn now(args: &HashMap<String, Value>) -> Result<Value> {
let utc = match args.get("utc") {
Some(val) => match from_value::<bool>(val.clone()) {
Expand Down Expand Up @@ -240,7 +242,7 @@ mod tests {
assert_eq!(res, to_value(vec![0, 2, 4, 6, 8]).unwrap());
}

#[cfg(feature = "builtins")]
#[cfg(feature = "chrono")]
#[test]
fn now_default() {
let args = HashMap::new();
Expand All @@ -250,7 +252,7 @@ mod tests {
assert!(res.as_str().unwrap().contains("T"));
}

#[cfg(feature = "builtins")]
#[cfg(feature = "chrono")]
#[test]
fn now_datetime_utc() {
let mut args = HashMap::new();
Expand All @@ -264,7 +266,7 @@ mod tests {
assert!(val.contains("+00:00"));
}

#[cfg(feature = "builtins")]
#[cfg(feature = "chrono")]
#[test]
fn now_timestamp() {
let mut args = HashMap::new();
Expand Down
4 changes: 2 additions & 2 deletions src/tera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ impl Tera {

self.register_filter("length", common::length);
self.register_filter("reverse", common::reverse);
#[cfg(feature = "builtins")]
#[cfg(feature = "chrono")]
self.register_filter("date", common::date);
self.register_filter("json_encode", common::json_encode);
self.register_filter("as_str", common::as_str);
Expand All @@ -639,7 +639,7 @@ impl Tera {

fn register_tera_functions(&mut self) {
self.register_function("range", functions::range);
#[cfg(feature = "builtins")]
#[cfg(feature = "chrono")]
self.register_function("now", functions::now);
self.register_function("throw", functions::throw);
#[cfg(feature = "builtins")]
Expand Down