aboutsummaryrefslogtreecommitdiffstats
path: root/askama_derive/src
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-07-01 15:38:34 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-07-31 10:27:15 +0200
commit8b184511edff62bd3786be3f5d62c1965430278b (patch)
tree340209532d26c9f177ba21fd1e444ea6f24f37a7 /askama_derive/src
parentac8de6260e34c7dc7f7f2228e832d1860a31707d (diff)
downloadaskama-8b184511edff62bd3786be3f5d62c1965430278b.tar.gz
askama-8b184511edff62bd3786be3f5d62c1965430278b.tar.bz2
askama-8b184511edff62bd3786be3f5d62c1965430278b.zip
derive: move Syntax into parser module
Diffstat (limited to 'askama_derive/src')
-rw-r--r--askama_derive/src/config.rs45
-rw-r--r--askama_derive/src/generator.rs3
-rw-r--r--askama_derive/src/input.rs3
-rw-r--r--askama_derive/src/parser/mod.rs24
-rw-r--r--askama_derive/src/parser/tests.rs3
5 files changed, 38 insertions, 40 deletions
diff --git a/askama_derive/src/config.rs b/askama_derive/src/config.rs
index f01bd65..1fee3c8 100644
--- a/askama_derive/src/config.rs
+++ b/askama_derive/src/config.rs
@@ -1,11 +1,11 @@
use std::collections::{BTreeMap, HashSet};
-use std::convert::TryFrom;
use std::path::{Path, PathBuf};
use std::{env, fs};
#[cfg(feature = "serde")]
use serde::Deserialize;
+use crate::parser::Syntax;
use crate::CompileError;
#[derive(Debug)]
@@ -66,7 +66,7 @@ impl<'a> Config<'a> {
let name = raw_s.name;
if syntaxes
- .insert(name.to_string(), Syntax::try_from(raw_s)?)
+ .insert(name.to_string(), raw_s.try_into()?)
.is_some()
{
return Err(format!("syntax \"{name}\" is already defined").into());
@@ -131,41 +131,18 @@ impl<'a> Config<'a> {
}
}
-#[derive(Debug)]
-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<'static> {
- fn default() -> Self {
- Self {
- block_start: "{%",
- block_end: "%}",
- expr_start: "{{",
- expr_end: "}}",
- comment_start: "{#",
- comment_end: "#}",
- }
- }
-}
-
-impl<'a> TryFrom<RawSyntax<'a>> for Syntax<'a> {
+impl<'a> TryInto<Syntax<'a>> for RawSyntax<'a> {
type Error = CompileError;
- fn try_from(raw: RawSyntax<'a>) -> std::result::Result<Self, Self::Error> {
+ fn try_into(self) -> Result<Syntax<'a>, 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),
- expr_start: raw.expr_start.unwrap_or(default.expr_start),
- expr_end: raw.expr_end.unwrap_or(default.expr_end),
- comment_start: raw.comment_start.unwrap_or(default.comment_start),
- comment_end: raw.comment_end.unwrap_or(default.comment_end),
+ let syntax = Syntax {
+ block_start: self.block_start.unwrap_or(default.block_start),
+ block_end: self.block_end.unwrap_or(default.block_end),
+ expr_start: self.expr_start.unwrap_or(default.expr_start),
+ expr_end: self.expr_end.unwrap_or(default.expr_end),
+ comment_start: self.comment_start.unwrap_or(default.comment_start),
+ comment_end: self.comment_end.unwrap_or(default.comment_end),
};
for s in [
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs
index f990330..f8b9ecb 100644
--- a/askama_derive/src/generator.rs
+++ b/askama_derive/src/generator.rs
@@ -244,8 +244,7 @@ fn find_used_templates(
mod _parsed {
use std::mem;
- use crate::config::Syntax;
- use crate::parser::{parse, Node};
+ use crate::parser::{parse, Node, Syntax};
use crate::CompileError;
pub(super) struct Parsed {
diff --git a/askama_derive/src/input.rs b/askama_derive/src/input.rs
index 47d51bd..be425a3 100644
--- a/askama_derive/src/input.rs
+++ b/askama_derive/src/input.rs
@@ -1,5 +1,6 @@
-use crate::config::{Config, Syntax};
+use crate::config::Config;
use crate::generator::TemplateArgs;
+use crate::parser::Syntax;
use crate::CompileError;
use std::path::{Path, PathBuf};
diff --git a/askama_derive/src/parser/mod.rs b/askama_derive/src/parser/mod.rs
index ccc9ff9..f81bcc8 100644
--- a/askama_derive/src/parser/mod.rs
+++ b/askama_derive/src/parser/mod.rs
@@ -13,7 +13,6 @@ use nom::{error_position, AsChar, IResult, InputTakeAtPosition};
pub(crate) use self::expr::Expr;
pub(crate) use self::node::{Cond, CondTest, Loop, Macro, Node, Target, When, Whitespace, Ws};
-use crate::config::Syntax;
use crate::CompileError;
mod expr;
@@ -312,3 +311,26 @@ fn tag_expr_start<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> {
fn tag_expr_end<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, &'a str> {
tag(s.syntax.expr_end)(i)
}
+
+#[derive(Debug)]
+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<'static> {
+ fn default() -> Self {
+ Self {
+ block_start: "{%",
+ block_end: "%}",
+ expr_start: "{{",
+ expr_end: "}}",
+ comment_start: "{#",
+ comment_end: "#}",
+ }
+ }
+}
diff --git a/askama_derive/src/parser/tests.rs b/askama_derive/src/parser/tests.rs
index 801e787..68cf14b 100644
--- a/askama_derive/src/parser/tests.rs
+++ b/askama_derive/src/parser/tests.rs
@@ -1,5 +1,4 @@
-use crate::config::Syntax;
-use crate::parser::{Expr, Node, Whitespace, Ws};
+use super::{Expr, Node, Syntax, Whitespace, Ws};
fn check_ws_split(s: &str, res: &(&str, &str, &str)) {
match super::split_ws_parts(s) {