aboutsummaryrefslogtreecommitdiffstats
path: root/askama_shared
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2020-01-15 13:41:43 +0100
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2020-01-15 13:48:16 +0100
commit3b8bf97cb6da128f020bb557057269661ac89fea (patch)
treedf4f9a5f08fe35584ada9df69c15528e345278fe /askama_shared
parentf4f82b74c65b31b88d6aacde14c47673a165cee4 (diff)
downloadaskama-3b8bf97cb6da128f020bb557057269661ac89fea.tar.gz
askama-3b8bf97cb6da128f020bb557057269661ac89fea.tar.bz2
askama-3b8bf97cb6da128f020bb557057269661ac89fea.zip
Make dependencies optional where possible
Diffstat (limited to 'askama_shared')
-rw-r--r--askama_shared/Cargo.toml15
-rw-r--r--askama_shared/src/filters/mod.rs18
-rw-r--r--askama_shared/src/lib.rs43
3 files changed, 60 insertions, 16 deletions
diff --git a/askama_shared/Cargo.toml b/askama_shared/Cargo.toml
index 733416a..15a3243 100644
--- a/askama_shared/Cargo.toml
+++ b/askama_shared/Cargo.toml
@@ -9,11 +9,18 @@ license = "MIT/Apache-2.0"
workspace = ".."
edition = "2018"
+[features]
+default = ["config", "humansize", "num-traits"]
+config = ["serde", "toml"]
+json = ["serde", "serde_json"]
+yaml = ["serde", "serde_yaml"]
+
[dependencies]
askama_escape = { version = "0.2.0", path = "../askama_escape" }
-humansize = "1.1.0"
-num-traits = "0.2.6"
-serde = { version = "1.0", features = ["derive"] }
+humansize = { version = "1.1.0", optional = true }
+num-traits = { version = "0.2.6", optional = true }
+serde = { version = "1.0", optional = true, features = ["derive"] }
+serde_derive = { version = "1.0", optional = true }
serde_json = { version = "1.0", optional = true }
serde_yaml = { version = "0.8", optional = true }
-toml = "0.5"
+toml = { version = "0.5", optional = true }
diff --git a/askama_shared/src/filters/mod.rs b/askama_shared/src/filters/mod.rs
index 4484ce4..c06f84a 100644
--- a/askama_shared/src/filters/mod.rs
+++ b/askama_shared/src/filters/mod.rs
@@ -5,6 +5,8 @@
//! see the top-level crate documentation.
#![allow(clippy::trivially_copy_pass_by_ref)]
+use std::fmt;
+
#[cfg(feature = "serde_json")]
mod json;
#[cfg(feature = "serde_json")]
@@ -15,12 +17,13 @@ mod yaml;
#[cfg(feature = "serde_yaml")]
pub use self::yaml::yaml;
+#[allow(unused_imports)]
use crate::error::Error::Fmt;
use askama_escape::{Escaper, MarkupDisplay};
+#[cfg(feature = "humansize")]
use humansize::{file_size_opts, FileSize};
-use num_traits::cast::NumCast;
-use num_traits::Signed;
-use std::fmt;
+#[cfg(feature = "num_traits")]
+use num_traits::{Signed, cast::NumCast};
use super::Result;
@@ -92,6 +95,7 @@ where
escape(e, v)
}
+#[cfg(feature = "humansize")]
/// Returns adequate string representation (in KB, ..) of number of bytes
pub fn filesizeformat(b: usize) -> Result<String> {
b.file_size(file_size_opts::DECIMAL)
@@ -182,6 +186,7 @@ pub fn indent(s: &dyn fmt::Display, width: &usize) -> Result<String> {
Ok(indented)
}
+#[cfg(feature = "num_traits")]
/// Casts number to f64
pub fn into_f64<T>(number: T) -> Result<f64>
where
@@ -190,6 +195,7 @@ where
number.to_f64().ok_or(Fmt(fmt::Error))
}
+#[cfg(feature = "num_traits")]
/// Casts number to isize
pub fn into_isize<T>(number: T) -> Result<isize>
where
@@ -220,6 +226,7 @@ where
Ok(rv)
}
+#[cfg(feature = "num_traits")]
/// Absolute value
pub fn abs<T>(number: T) -> Result<T>
where
@@ -283,8 +290,10 @@ pub fn wordcount(s: &dyn fmt::Display) -> Result<usize> {
#[cfg(test)]
mod tests {
use super::*;
+ #[cfg(feature = "num_traits")]
use std::f64::INFINITY;
+ #[cfg(feature = "humansize")]
#[test]
fn test_filesizeformat() {
assert_eq!(filesizeformat(0).unwrap(), "0 B");
@@ -347,6 +356,7 @@ mod tests {
);
}
+ #[cfg(feature = "num_traits")]
#[test]
#[allow(clippy::float_cmp)]
fn test_into_f64() {
@@ -357,6 +367,7 @@ mod tests {
assert_eq!(into_f64(-INFINITY as f32).unwrap(), -INFINITY);
}
+ #[cfg(feature = "num_traits")]
#[test]
fn test_into_isize() {
assert_eq!(into_isize(1).unwrap(), 1 as isize);
@@ -403,6 +414,7 @@ mod tests {
);
}
+ #[cfg(feature = "num_traits")]
#[test]
#[allow(clippy::float_cmp)]
fn test_abs() {
diff --git a/askama_shared/src/lib.rs b/askama_shared/src/lib.rs
index 87ec0aa..dccec59 100644
--- a/askama_shared/src/lib.rs
+++ b/askama_shared/src/lib.rs
@@ -1,12 +1,14 @@
#![cfg_attr(feature = "cargo-clippy", allow(unused_parens))]
-use toml;
use std::collections::{BTreeMap, HashSet};
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
+#[cfg(feature = "serde")]
use serde::Deserialize;
+#[cfg(feature = "config")]
+use toml;
pub use askama_escape::MarkupDisplay;
@@ -31,8 +33,11 @@ impl<'a> Config<'a> {
let mut syntaxes = BTreeMap::new();
syntaxes.insert(DEFAULT_SYNTAX_NAME.to_string(), Syntax::default());
- let raw: RawConfig<'_> =
- toml::from_str(&s).unwrap_or_else(|_| panic!("invalid TOML in {}", CONFIG_FILE_NAME));
+ let raw = if s.is_empty() {
+ RawConfig::default()
+ } else {
+ RawConfig::from_toml_str(s)
+ };
let (dirs, default_syntax) = match raw.general {
Some(General {
@@ -170,22 +175,35 @@ impl<'a> From<RawSyntax<'a>> for Syntax<'a> {
}
}
-#[derive(Deserialize)]
+#[cfg_attr(feature = "serde", derive(Deserialize))]
+#[derive(Default)]
struct RawConfig<'d> {
- #[serde(borrow)]
+ #[cfg_attr(feature = "serde", serde(borrow))]
general: Option<General<'d>>,
syntax: Option<Vec<RawSyntax<'d>>>,
escaper: Option<Vec<RawEscaper<'d>>>,
}
-#[derive(Deserialize)]
+impl<'d> RawConfig<'d> {
+ #[cfg(feature = "config")]
+ fn from_toml_str<'n>(s: &'n str) -> RawConfig<'n> {
+ toml::from_str(&s).unwrap_or_else(|_| panic!("invalid TOML in {}", CONFIG_FILE_NAME))
+ }
+
+ #[cfg(not(feature = "config"))]
+ fn from_toml_str<'n>(_: &'n str) -> RawConfig<'n> {
+ panic!("toml support not available")
+ }
+}
+
+#[cfg_attr(feature = "serde", derive(Deserialize))]
struct General<'a> {
- #[serde(borrow)]
+ #[cfg_attr(feature = "serde", serde(borrow))]
dirs: Option<Vec<&'a str>>,
default_syntax: Option<&'a str>,
}
-#[derive(Deserialize)]
+#[cfg_attr(feature = "serde", derive(Deserialize))]
struct RawSyntax<'a> {
name: &'a str,
block_start: Option<&'a str>,
@@ -196,7 +214,7 @@ struct RawSyntax<'a> {
comment_end: Option<&'a str>,
}
-#[derive(Deserialize)]
+#[cfg_attr(feature = "serde", derive(Deserialize))]
struct RawEscaper<'a> {
path: &'a str,
extensions: Vec<&'a str>,
@@ -243,6 +261,7 @@ mod tests {
assert_eq!(config.dirs, vec![root]);
}
+ #[cfg(feature = "config")]
#[test]
fn test_config_dirs() {
let mut root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
@@ -291,6 +310,7 @@ mod tests {
assert_eq_rooted(&path, "sub/sub1/d.html");
}
+ #[cfg(feature = "config")]
#[test]
fn add_syntax() {
let raw_config = r#"
@@ -327,6 +347,7 @@ mod tests {
assert_eq!(bar.comment_end, default_syntax.comment_end);
}
+ #[cfg(feature = "config")]
#[test]
fn add_syntax_two() {
let raw_config = r#"
@@ -358,6 +379,7 @@ mod tests {
assert_eq!(bar.comment_end, default_syntax.comment_end);
}
+ #[cfg(feature = "toml")]
#[should_panic]
#[test]
fn use_default_at_syntax_name() {
@@ -368,6 +390,7 @@ mod tests {
let _config = Config::new(raw_config);
}
+ #[cfg(feature = "toml")]
#[should_panic]
#[test]
fn duplicated_syntax_name_on_list() {
@@ -379,6 +402,7 @@ mod tests {
let _config = Config::new(raw_config);
}
+ #[cfg(feature = "toml")]
#[should_panic]
#[test]
fn is_not_exist_default_syntax() {
@@ -390,6 +414,7 @@ mod tests {
let _config = Config::new(raw_config);
}
+ #[cfg(feature = "config")]
#[test]
fn escape_modes() {
let config = Config::new(