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_shared/src/filters/mod.rs | 18 ++++++++++++++--- askama_shared/src/lib.rs | 43 +++++++++++++++++++++++++++++++--------- 2 files changed, 49 insertions(+), 12 deletions(-) (limited to 'askama_shared/src') 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