Skip to content

Commit

Permalink
askama: make all features explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Jul 26, 2024
1 parent 56431f2 commit a5addba
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
13 changes: 7 additions & 6 deletions askama/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ maintenance = { status = "actively-developed" }
[features]
default = ["config", "humansize", "num-traits", "urlencode"]
config = ["askama_derive/config"]
humansize = ["askama_derive/humansize", "dep_humansize"]
num-traits = ["askama_derive/num-traits", "dep_num_traits"]
serde-json = ["askama_derive/serde-json", "serde", "serde_json"]
urlencode = ["askama_derive/urlencode", "percent-encoding"]
humansize = ["askama_derive/humansize", "dep:humansize"]
num-traits = ["askama_derive/num-traits", "dep:num-traits"]
serde_json = ["askama_derive/serde-json", "dep:serde", "dep:serde_json"]
serde-json = ["serde_json"] # Alias for backwards compatibility
urlencode = ["askama_derive/urlencode", "dep:percent-encoding"]
with-actix-web = ["askama_derive/with-actix-web"]
with-axum = ["askama_derive/with-axum"]
with-rocket = ["askama_derive/with-rocket"]
Expand All @@ -35,8 +36,8 @@ mime_guess = []
[dependencies]
askama_derive = { version = "0.13", path = "../askama_derive" }
askama_escape = { version = "0.11", path = "../askama_escape" }
dep_humansize = { package = "humansize", version = "2", optional = true }
dep_num_traits = { package = "num-traits", version = "0.2.6", optional = true }
humansize = { package = "humansize", version = "2", optional = true }
num-traits = { version = "0.2.6", optional = true }
percent-encoding = { version = "2.1.0", optional = true }
serde = { version = "1.0", optional = true, features = ["derive"] }
serde_json = { version = "1.0", optional = true }
Expand Down
20 changes: 10 additions & 10 deletions askama/src/filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ pub use self::json::json;

use askama_escape::{Escaper, MarkupDisplay};
#[cfg(feature = "humansize")]
use dep_humansize::{ISizeFormatter, ToF64, DECIMAL};
use humansize::{ISizeFormatter, ToF64, DECIMAL};
#[cfg(feature = "num-traits")]
use dep_num_traits::{cast::NumCast, Signed};
#[cfg(feature = "percent-encoding")]
use num_traits::{cast::NumCast, Signed};
#[cfg(feature = "urlencode")]
use percent_encoding::{utf8_percent_encode, AsciiSet, NON_ALPHANUMERIC};

use super::Result;
#[allow(unused_imports)]
use crate::error::Error::Fmt;

#[cfg(feature = "percent-encoding")]
#[cfg(feature = "urlencode")]
// Urlencode char encoding set. Only the characters in the unreserved set don't
// have any special purpose in any part of a URI and can be safely left
// unencoded as specified in https://tools.ietf.org/html/rfc3986.html#section-2.3
Expand All @@ -36,7 +36,7 @@ const URLENCODE_STRICT_SET: &AsciiSet = &NON_ALPHANUMERIC
.remove(b'-')
.remove(b'~');

#[cfg(feature = "percent-encoding")]
#[cfg(feature = "urlencode")]
// Same as URLENCODE_STRICT_SET, but preserves forward slashes for encoding paths
const URLENCODE_SET: &AsciiSet = &URLENCODE_STRICT_SET.remove(b'/');

Expand Down Expand Up @@ -121,7 +121,7 @@ impl fmt::Display for FilesizeFormatFilter {
}
}

#[cfg(feature = "percent-encoding")]
#[cfg(feature = "urlencode")]
/// Percent-encodes the argument for safe use in URI; does not encode `/`.
///
/// This should be safe for all parts of URI (paths segments, query keys, query
Expand All @@ -146,7 +146,7 @@ pub fn urlencode<T: fmt::Display>(s: T) -> Result<impl fmt::Display, Infallible>
Ok(UrlencodeFilter(s, URLENCODE_SET))
}

#[cfg(feature = "percent-encoding")]
#[cfg(feature = "urlencode")]
/// Percent-encodes the argument for safe use in URI; encodes `/`.
///
/// Use this filter for encoding query keys and values in the rare case that
Expand All @@ -166,10 +166,10 @@ pub fn urlencode_strict<T: fmt::Display>(s: T) -> Result<impl fmt::Display, Infa
Ok(UrlencodeFilter(s, URLENCODE_STRICT_SET))
}

#[cfg(feature = "percent-encoding")]
#[cfg(feature = "urlencode")]
struct UrlencodeFilter<T>(T, &'static AsciiSet);

#[cfg(feature = "percent-encoding")]
#[cfg(feature = "urlencode")]
impl<T: fmt::Display> fmt::Display for UrlencodeFilter<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
struct Writer<'a, 'b>(&'a mut fmt::Formatter<'b>, &'static AsciiSet);
Expand Down Expand Up @@ -558,7 +558,7 @@ mod tests {
assert_eq!(filesizeformat(&1024usize).unwrap().to_string(), "1.02 kB");
}

#[cfg(feature = "percent-encoding")]
#[cfg(feature = "urlencode")]
#[test]
fn test_urlencoding() {
// Unreserved (https://tools.ietf.org/html/rfc3986.html#section-2.3)
Expand Down

0 comments on commit a5addba

Please sign in to comment.