Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace toml_edit with basic-toml #780

Merged
merged 2 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions askama_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ rust-version = "1.58"
proc-macro = true

[features]
config = ["serde", "toml_edit"]
config = ["serde", "basic-toml"]
humansize = []
markdown = []
urlencode = []
Expand All @@ -38,4 +38,4 @@ proc-macro2 = "1"
quote = "1"
serde = { version = "1.0", optional = true, features = ["derive"] }
syn = "1"
toml_edit = { version = "0.19", optional = true, features = ["serde"] }
basic-toml = { version = "0.1.1", optional = true }
98 changes: 50 additions & 48 deletions askama_derive/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ use serde::Deserialize;
use crate::CompileError;

#[derive(Debug)]
pub(crate) struct Config {
pub(crate) struct Config<'a> {
pub(crate) dirs: Vec<PathBuf>,
pub(crate) syntaxes: BTreeMap<String, Syntax>,
pub(crate) default_syntax: String,
pub(crate) syntaxes: BTreeMap<String, Syntax<'a>>,
pub(crate) default_syntax: &'a str,
pub(crate) escapers: Vec<(HashSet<String>, String)>,
pub(crate) whitespace: WhitespaceHandling,
}

impl Config {
pub(crate) fn new(s: &str) -> std::result::Result<Config, CompileError> {
impl Config<'_> {
pub(crate) fn new(s: &str) -> std::result::Result<Config<'_>, CompileError> {
let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let default_dirs = vec![root.join("templates")];

Expand All @@ -40,19 +40,19 @@ impl Config {
dirs.map_or(default_dirs, |v| {
v.into_iter().map(|dir| root.join(dir)).collect()
}),
default_syntax.unwrap_or_else(|| DEFAULT_SYNTAX_NAME.to_owned()),
default_syntax.unwrap_or(DEFAULT_SYNTAX_NAME),
whitespace,
),
None => (
default_dirs,
DEFAULT_SYNTAX_NAME.to_owned(),
DEFAULT_SYNTAX_NAME,
WhitespaceHandling::default(),
),
};

if let Some(raw_syntaxes) = raw.syntax {
for raw_s in raw_syntaxes {
let name = raw_s.name.clone();
let name = raw_s.name;

if syntaxes
.insert(name.to_string(), Syntax::try_from(raw_s)?)
Expand All @@ -63,7 +63,7 @@ impl Config {
}
}

if !syntaxes.contains_key(&default_syntax) {
if !syntaxes.contains_key(default_syntax) {
return Err(format!("default syntax \"{default_syntax}\" not found").into());
}

Expand Down Expand Up @@ -121,33 +121,33 @@ impl Config {
}

#[derive(Debug)]
pub(crate) struct Syntax {
pub(crate) block_start: String,
pub(crate) block_end: String,
pub(crate) expr_start: String,
pub(crate) expr_end: String,
pub(crate) comment_start: String,
pub(crate) comment_end: String,
pub(crate) struct Syntax<'a> {
pub(crate) block_start: &'a str,
pub(crate) block_end: &'a str,
pub(crate) expr_start: &'a str,
pub(crate) expr_end: &'a str,
pub(crate) comment_start: &'a str,
pub(crate) comment_end: &'a str,
}

impl Default for Syntax {
impl Default for Syntax<'static> {
fn default() -> Self {
Self {
block_start: "{%".to_owned(),
block_end: "%}".to_owned(),
expr_start: "{{".to_owned(),
expr_end: "}}".to_owned(),
comment_start: "{#".to_owned(),
comment_end: "#}".to_owned(),
block_start: "{%",
block_end: "%}",
expr_start: "{{",
expr_end: "}}",
comment_start: "{#",
comment_end: "#}",
}
}
}

impl TryFrom<RawSyntax> for Syntax {
impl<'a> TryFrom<RawSyntax<'a>> for Syntax<'a> {
type Error = CompileError;

fn try_from(raw: RawSyntax) -> std::result::Result<Self, Self::Error> {
let default = Self::default();
fn try_from(raw: RawSyntax<'a>) -> std::result::Result<Self, Self::Error> {
let default = Syntax::default();
let syntax = Self {
block_start: raw.block_start.unwrap_or(default.block_start),
block_end: raw.block_end.unwrap_or(default.block_end),
Expand Down Expand Up @@ -183,21 +183,22 @@ impl TryFrom<RawSyntax> for Syntax {

#[cfg_attr(feature = "serde", derive(Deserialize))]
#[derive(Default)]
struct RawConfig {
general: Option<General>,
syntax: Option<Vec<RawSyntax>>,
escaper: Option<Vec<RawEscaper>>,
struct RawConfig<'a> {
#[cfg_attr(feature = "serde", serde(borrow))]
general: Option<General<'a>>,
syntax: Option<Vec<RawSyntax<'a>>>,
escaper: Option<Vec<RawEscaper<'a>>>,
}

impl RawConfig {
impl RawConfig<'_> {
#[cfg(feature = "config")]
fn from_toml_str(s: &str) -> std::result::Result<RawConfig, CompileError> {
toml_edit::de::from_str(s)
fn from_toml_str(s: &str) -> std::result::Result<RawConfig<'_>, CompileError> {
basic_toml::from_str(s)
.map_err(|e| format!("invalid TOML in {CONFIG_FILE_NAME}: {e}").into())
}

#[cfg(not(feature = "config"))]
fn from_toml_str(_: &str) -> std::result::Result<RawConfig, CompileError> {
fn from_toml_str(_: &str) -> std::result::Result<RawConfig<'_>, CompileError> {
Err("TOML support not available".into())
}
}
Expand All @@ -223,28 +224,29 @@ impl Default for WhitespaceHandling {
}

#[cfg_attr(feature = "serde", derive(Deserialize))]
struct General {
dirs: Option<Vec<String>>,
default_syntax: Option<String>,
struct General<'a> {
#[cfg_attr(feature = "serde", serde(borrow))]
dirs: Option<Vec<&'a str>>,
default_syntax: Option<&'a str>,
#[cfg_attr(feature = "serde", serde(default))]
whitespace: WhitespaceHandling,
}

#[cfg_attr(feature = "serde", derive(Deserialize))]
struct RawSyntax {
name: String,
block_start: Option<String>,
block_end: Option<String>,
expr_start: Option<String>,
expr_end: Option<String>,
comment_start: Option<String>,
comment_end: Option<String>,
struct RawSyntax<'a> {
name: &'a str,
block_start: Option<&'a str>,
block_end: Option<&'a str>,
expr_start: Option<&'a str>,
expr_end: Option<&'a str>,
comment_start: Option<&'a str>,
comment_end: Option<&'a str>,
}

#[cfg_attr(feature = "serde", derive(Deserialize))]
struct RawEscaper {
path: String,
extensions: Vec<String>,
struct RawEscaper<'a> {
path: &'a str,
extensions: Vec<&'a str>,
}

pub(crate) fn read_config_file(
Expand Down
2 changes: 1 addition & 1 deletion askama_derive/src/heritage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub(crate) struct Context<'a> {

impl Context<'_> {
pub(crate) fn new<'n>(
config: &Config,
config: &Config<'_>,
path: &Path,
nodes: &'n [Node<'n>],
) -> Result<Context<'n>, CompileError> {
Expand Down
8 changes: 4 additions & 4 deletions askama_derive/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use mime::Mime;

pub(crate) struct TemplateInput<'a> {
pub(crate) ast: &'a syn::DeriveInput,
pub(crate) config: &'a Config,
pub(crate) syntax: &'a Syntax,
pub(crate) config: &'a Config<'a>,
pub(crate) syntax: &'a Syntax<'a>,
pub(crate) source: Source,
pub(crate) print: Print,
pub(crate) escaper: &'a str,
Expand All @@ -25,7 +25,7 @@ impl TemplateInput<'_> {
/// `template()` attribute list fields.
pub(crate) fn new<'n>(
ast: &'n syn::DeriveInput,
config: &'n Config,
config: &'n Config<'_>,
args: TemplateArgs,
) -> Result<TemplateInput<'n>, CompileError> {
let TemplateArgs {
Expand All @@ -51,7 +51,7 @@ impl TemplateInput<'_> {

// Validate syntax
let syntax = syntax.map_or_else(
|| Ok(config.syntaxes.get(&config.default_syntax).unwrap()),
|| Ok(config.syntaxes.get(config.default_syntax).unwrap()),
|s| {
config
.syntaxes
Expand Down
29 changes: 16 additions & 13 deletions askama_derive/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ mod node;
mod tests;

struct State<'a> {
syntax: &'a Syntax,
syntax: &'a Syntax<'a>,
loop_depth: Cell<usize>,
}

impl State<'_> {
fn new(syntax: &Syntax) -> State<'_> {
impl<'a> State<'a> {
fn new(syntax: &'a Syntax<'a>) -> State<'a> {
State {
syntax,
loop_depth: Cell::new(0),
Expand Down Expand Up @@ -58,7 +58,10 @@ impl From<char> for Whitespace {
}
}

pub(crate) fn parse<'a>(src: &'a str, syntax: &'a Syntax) -> Result<Vec<Node<'a>>, CompileError> {
pub(crate) fn parse<'a>(
src: &'a str,
syntax: &'a Syntax<'_>,
) -> Result<Vec<Node<'a>>, CompileError> {
match Node::parse(src, &State::new(syntax)) {
Ok((left, res)) => {
if !left.is_empty() {
Expand Down Expand Up @@ -271,9 +274,9 @@ fn path(i: &str) -> IResult<&str, Vec<&str>> {

fn take_content<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, Node<'a>> {
let p_start = alt((
tag(s.syntax.block_start.as_str()),
tag(s.syntax.comment_start.as_str()),
tag(s.syntax.expr_start.as_str()),
tag(s.syntax.block_start),
tag(s.syntax.comment_start),
tag(s.syntax.expr_start),
));

let (i, _) = not(eof)(i)?;
Expand All @@ -290,25 +293,25 @@ fn take_content<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, Node<'a>> {
}

fn tag_block_start<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> {
tag(s.syntax.block_start.as_str())(i)
tag(s.syntax.block_start)(i)
}

fn tag_block_end<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> {
tag(s.syntax.block_end.as_str())(i)
tag(s.syntax.block_end)(i)
}

fn tag_comment_start<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> {
tag(s.syntax.comment_start.as_str())(i)
tag(s.syntax.comment_start)(i)
}

fn tag_comment_end<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> {
tag(s.syntax.comment_end.as_str())(i)
tag(s.syntax.comment_end)(i)
}

fn tag_expr_start<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> {
tag(s.syntax.expr_start.as_str())(i)
tag(s.syntax.expr_start)(i)
}

fn tag_expr_end<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> {
tag(s.syntax.expr_end.as_str())(i)
tag(s.syntax.expr_end)(i)
}
4 changes: 2 additions & 2 deletions askama_derive/src/parser/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,8 @@ fn block_node<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, Node<'a>> {
fn block_comment_body<'a>(mut i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> {
let mut level = 0;
loop {
let (end, tail) = take_until(s.syntax.comment_end.as_str())(i)?;
match take_until::<_, _, Error<_>>(s.syntax.comment_start.as_str())(i) {
let (end, tail) = take_until(s.syntax.comment_end)(i)?;
match take_until::<_, _, Error<_>>(s.syntax.comment_start)(i) {
Ok((start, _)) if start.as_ptr() < end.as_ptr() => {
level += 1;
i = &start[2..];
Expand Down
4 changes: 2 additions & 2 deletions askama_derive/src/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ fn test_parse_root_path() {
#[test]
fn change_delimiters_parse_filter() {
let syntax = Syntax {
expr_start: "{=".to_owned(),
expr_end: "=}".to_owned(),
expr_start: "{=",
expr_end: "=}",
..Syntax::default()
};

Expand Down