From 3b8bf97cb6da128f020bb557057269661ac89fea Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Wed, 15 Jan 2020 13:41:43 +0100 Subject: Make dependencies optional where possible --- askama/Cargo.toml | 13 +++++++----- askama_shared/Cargo.toml | 15 ++++++++++---- askama_shared/src/filters/mod.rs | 18 ++++++++++++++--- askama_shared/src/lib.rs | 43 +++++++++++++++++++++++++++++++--------- 4 files changed, 68 insertions(+), 21 deletions(-) diff --git a/askama/Cargo.toml b/askama/Cargo.toml index c325395..6a96976 100644 --- a/askama/Cargo.toml +++ b/askama/Cargo.toml @@ -18,9 +18,12 @@ azure-devops = { project = "dochtman/Projects", pipeline = "Askama", build = "2" maintenance = { status = "actively-developed" } [features] -default = [] -serde-json = ["askama_shared/serde_json"] -serde-yaml = ["askama_shared/serde_yaml"] +default = ["config", "humansize", "num-traits"] +config = ["askama_shared/config"] +humansize = ["askama_shared/humansize"] +serde-json = ["askama_shared/json"] +serde-yaml = ["askama_shared/yaml"] +num-traits = ["askama_shared/num-traits"] with-iron = ["iron", "askama_derive/iron"] with-rocket = ["rocket", "askama_derive/rocket"] with-actix-web = ["actix-web", "askama_derive/actix-web", "mime", "mime_guess", "bytes", "futures"] @@ -29,7 +32,7 @@ with-gotham = ["gotham", "askama_derive/gotham", "hyper", "mime", "mime_guess"] [dependencies] askama_derive = { version = "0.8.0", path = "../askama_derive" } askama_escape = { version = "0.2.0", path = "../askama_escape" } -askama_shared = { version = "0.8.0", path = "../askama_shared" } +askama_shared = { version = "0.8.0", path = "../askama_shared", default-features = false } iron = { version = ">= 0.5, < 0.7", optional = true } rocket = { version = "0.4", optional = true } actix-web = { version = "2", optional = true } @@ -41,4 +44,4 @@ hyper = { version = "0.12", optional = true } bytes = { version = "0.5", optional = true } [package.metadata.docs.rs] -features = [ "serde-json" ] +features = ["config", "humansize", "num-traits", "serde-json", "serde-yaml"] 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 { b.file_size(file_size_opts::DECIMAL) @@ -182,6 +186,7 @@ pub fn indent(s: &dyn fmt::Display, width: &usize) -> Result { Ok(indented) } +#[cfg(feature = "num_traits")] /// Casts number to f64 pub fn into_f64(number: T) -> Result 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(number: T) -> Result where @@ -220,6 +226,7 @@ where Ok(rv) } +#[cfg(feature = "num_traits")] /// Absolute value pub fn abs(number: T) -> Result where @@ -283,8 +290,10 @@ pub fn wordcount(s: &dyn fmt::Display) -> Result { #[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> 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>, syntax: Option>>, escaper: Option>>, } -#[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>, 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( -- cgit