Skip to content

Commit

Permalink
WIP: move askama_escape into askama
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed Apr 26, 2022
1 parent e988d29 commit c9e7657
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 72 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ members = [
"askama_axum",
"askama_gotham",
"askama_derive",
"askama_escape",
"askama_mendes",
"askama_rocket",
"askama_tide",
Expand All @@ -16,6 +15,5 @@ members = [
default-members = [
"askama",
"askama_derive",
"askama_escape",
"testing",
]
11 changes: 8 additions & 3 deletions askama/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ config = ["askama_derive/config"]
humansize = ["askama_derive/humansize", "dep:humansize"]
markdown = ["askama_derive/markdown", "comrak"]
urlencode = ["askama_derive/urlencode", "percent-encoding"]
serde-json = ["askama_derive/serde-json", "askama_escape/json", "serde", "serde_json"]
serde-json = ["askama_derive/serde-json", "serde", "serde_json"]
serde-yaml = ["askama_derive/serde-yaml", "serde", "serde_yaml"]
num-traits = ["askama_derive/num-traits", "dep:num-traits"]
with-actix-web = ["askama_derive/with-actix-web"]
Expand All @@ -38,8 +38,6 @@ mime_guess = []

[dependencies]
askama_derive = { version = "0.12.0", path = "../askama_derive" }
askama_escape = { version = "0.10.3", path = "../askama_escape" }

comrak = { version = "0.12", optional = true, default-features = false }
humansize = { version = "1.1.0", optional = true }
num-traits = { version = "0.2.6", optional = true }
Expand All @@ -48,5 +46,12 @@ serde_json = { version = "1.0", optional = true }
serde_yaml = { version = "0.8", optional = true }
percent-encoding = { version = "2.1.0", optional = true }

[dev-dependencies]
criterion = "0.3"

[package.metadata.docs.rs]
features = ["config", "humansize", "num-traits", "serde-json", "serde-yaml", "urlencode"]

[[bench]]
name = "escape"
harness = false
2 changes: 1 addition & 1 deletion askama_escape/benches/all.rs → askama/benches/escape.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[macro_use]
extern crate criterion;

use askama_escape::{Html, MarkupDisplay};
use askama::{Html, MarkupDisplay};
use criterion::Criterion;

criterion_main!(benches);
Expand Down
38 changes: 17 additions & 21 deletions askama_escape/src/lib.rs → askama/src/escape.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#![cfg_attr(not(any(feature = "json", test)), no_std)]
#![deny(elided_lifetimes_in_paths)]
#![deny(unreachable_pub)]

use core::fmt::{self, Display, Formatter, Write};
use core::str;
use std::fmt::{self, Display, Formatter, Write};
use std::str;

#[derive(Debug)]
pub struct MarkupDisplay<E, T>
Expand Down Expand Up @@ -65,7 +61,7 @@ where
}

#[derive(Debug)]
pub struct EscapeWriter<'a, E, W> {
struct EscapeWriter<'a, E, W> {
fmt: W,
escaper: &'a E,
}
Expand All @@ -80,15 +76,8 @@ where
}
}

pub fn escape<E>(string: &str, escaper: E) -> Escaped<'_, E>
where
E: Escaper,
{
Escaped { string, escaper }
}

#[derive(Debug)]
pub struct Escaped<'a, E>
pub(crate) struct Escaped<'a, E>
where
E: Escaper,
{
Expand Down Expand Up @@ -162,22 +151,22 @@ pub trait Escaper {
}

/// Escape chevrons, ampersand and apostrophes for use in JSON
#[cfg(feature = "json")]
#[cfg(feature = "serde-json")]
#[derive(Debug, Clone, Default)]
pub struct JsonEscapeBuffer(Vec<u8>);
pub(crate) struct JsonEscapeBuffer(Vec<u8>);

#[cfg(feature = "json")]
#[cfg(feature = "serde-json")]
impl JsonEscapeBuffer {
pub fn new() -> Self {
pub(crate) fn new() -> Self {
Self(Vec::new())
}

pub fn finish(self) -> String {
pub(crate) fn finish(self) -> String {
unsafe { String::from_utf8_unchecked(self.0) }
}
}

#[cfg(feature = "json")]
#[cfg(feature = "serde-json")]
impl std::io::Write for JsonEscapeBuffer {
fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
let mut last = 0;
Expand Down Expand Up @@ -212,6 +201,13 @@ mod tests {
use super::*;
use std::string::ToString;

pub(crate) fn escape<E>(string: &str, escaper: E) -> Escaped<'_, E>
where
E: Escaper,
{
Escaped { string, escaper }
}

#[test]
fn test_escape() {
assert_eq!(escape("", Html).to_string(), "");
Expand Down
5 changes: 3 additions & 2 deletions askama/src/filters/json.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::error::{Error, Result};
use askama_escape::JsonEscapeBuffer;
use serde::Serialize;
use serde_json::to_writer_pretty;

use crate::error::{Error, Result};
use crate::escape::JsonEscapeBuffer;

/// Serialize to JSON (requires `json` feature)
///
/// The generated string does not contain ampersands `&`, chevrons `< >`, or apostrophes `'`.
Expand Down
10 changes: 5 additions & 5 deletions askama/src/filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@

use std::fmt;

#[cfg(feature = "serde_json")]
#[cfg(feature = "serde-json")]
mod json;
#[cfg(feature = "serde_json")]
#[cfg(feature = "serde-json")]
pub use self::json::json;

#[cfg(feature = "serde_yaml")]
#[cfg(feature = "serde-yaml")]
mod yaml;
#[cfg(feature = "serde_yaml")]
#[cfg(feature = "serde-yaml")]
pub use self::yaml::yaml;

#[allow(unused_imports)]
use crate::error::Error::Fmt;
use askama_escape::{Escaper, MarkupDisplay};
use crate::escape::{Escaper, MarkupDisplay};
#[cfg(feature = "humansize")]
use humansize::{file_size_opts, FileSize};
#[cfg(feature = "num-traits")]
Expand Down
4 changes: 2 additions & 2 deletions askama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@
//! in the configuration file. The default syntax , "default", is the one
//! provided by Askama.
#![forbid(unsafe_code)]
#![deny(elided_lifetimes_in_paths)]
#![deny(unreachable_pub)]

mod error;
mod escape;
pub mod filters;
pub mod helpers;

use std::fmt;

pub use askama_derive::*;
pub use askama_escape::{Html, MarkupDisplay, Text};
pub use escape::{Html, MarkupDisplay, Text};

#[doc(hidden)]
pub use crate as shared;
Expand Down
25 changes: 0 additions & 25 deletions askama_escape/Cargo.toml

This file was deleted.

1 change: 0 additions & 1 deletion askama_escape/LICENSE-APACHE

This file was deleted.

1 change: 0 additions & 1 deletion askama_escape/LICENSE-MIT

This file was deleted.

9 changes: 0 additions & 9 deletions askama_escape/README.md

This file was deleted.

0 comments on commit c9e7657

Please sign in to comment.