aboutsummaryrefslogtreecommitdiffstats
path: root/askama_shared/src
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2018-10-06 14:19:12 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2018-10-07 07:37:46 +0200
commite1497adb506ac1d7fa8d063b4474a5519d0c10d0 (patch)
treed41791a089ddb598c2b52963aff9cfce44b80f39 /askama_shared/src
parent6e6691b06b2792108bef8fc2da142bb10e3f9dba (diff)
downloadaskama-e1497adb506ac1d7fa8d063b4474a5519d0c10d0.tar.gz
askama-e1497adb506ac1d7fa8d063b4474a5519d0c10d0.tar.bz2
askama-e1497adb506ac1d7fa8d063b4474a5519d0c10d0.zip
Handle syntax validation during Config instantiation
Diffstat (limited to 'askama_shared/src')
-rw-r--r--askama_shared/src/lib.rs54
1 files changed, 38 insertions, 16 deletions
diff --git a/askama_shared/src/lib.rs b/askama_shared/src/lib.rs
index 5d8b85f..0b9ceb6 100644
--- a/askama_shared/src/lib.rs
+++ b/askama_shared/src/lib.rs
@@ -121,15 +121,37 @@ impl<'a> Default for Syntax<'a> {
impl<'a> From<RawSyntax<'a>> for Syntax<'a> {
fn from(raw: RawSyntax<'a>) -> Self {
- let syntax = Self::default();
- Self {
- block_start: raw.block_start.unwrap_or(syntax.block_start),
- block_end: raw.block_end.unwrap_or(syntax.block_end),
- expr_start: raw.expr_start.unwrap_or(syntax.expr_start),
- expr_end: raw.expr_end.unwrap_or(syntax.expr_end),
- comment_start: raw.comment_start.unwrap_or(syntax.comment_start),
- comment_end: raw.comment_end.unwrap_or(syntax.comment_end),
+ let default = Self::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),
+ };
+
+ if syntax.block_start.len() != 2
+ || syntax.block_end.len() != 2
+ || syntax.expr_start.len() != 2
+ || syntax.expr_end.len() != 2
+ || syntax.comment_start.len() != 2
+ || syntax.comment_end.len() != 2
+ {
+ panic!("length of delimiters must be two")
}
+
+ let bs = syntax.block_start.as_bytes()[0];
+ let be = syntax.block_start.as_bytes()[1];
+ let cs = syntax.comment_start.as_bytes()[0];
+ let ce = syntax.comment_start.as_bytes()[1];
+ let es = syntax.block_start.as_bytes()[0];
+ let ee = syntax.block_start.as_bytes()[1];
+ if !(bs == cs && bs == es) && !(be == ce && be == ee) {
+ panic!("bad delimiters block_start: {}, comment_start: {}, expr_start: {}, needs one of the two characters in common", syntax.block_start, syntax.comment_start, syntax.expr_start);
+ }
+
+ syntax
}
}
@@ -239,11 +261,11 @@ mod tests {
[[syntax]]
name = "foo"
- block_start = "~<"
+ block_start = "{<"
[[syntax]]
name = "bar"
- expr_start = "%%"
+ expr_start = "{!"
"#;
let default_syntax = Syntax::default();
@@ -251,7 +273,7 @@ mod tests {
assert_eq!(config.default_syntax, "foo");
let foo = config.syntaxes.get("foo").unwrap();
- assert_eq!(foo.block_start, "~<");
+ assert_eq!(foo.block_start, "{<");
assert_eq!(foo.block_end, default_syntax.block_end);
assert_eq!(foo.expr_start, default_syntax.expr_start);
assert_eq!(foo.expr_end, default_syntax.expr_end);
@@ -261,7 +283,7 @@ mod tests {
let bar = config.syntaxes.get("bar").unwrap();
assert_eq!(bar.block_start, default_syntax.block_start);
assert_eq!(bar.block_end, default_syntax.block_end);
- assert_eq!(bar.expr_start, "%%");
+ assert_eq!(bar.expr_start, "{!");
assert_eq!(bar.expr_end, default_syntax.expr_end);
assert_eq!(bar.comment_start, default_syntax.comment_start);
assert_eq!(bar.comment_end, default_syntax.comment_end);
@@ -272,8 +294,8 @@ mod tests {
let raw_config = r#"
default_syntax = "foo"
- syntax = [{ name = "foo", block_start = "~<" },
- { name = "bar", expr_start = "%%" } ]
+ syntax = [{ name = "foo", block_start = "{<" },
+ { name = "bar", expr_start = "{!" } ]
"#;
let default_syntax = Syntax::default();
@@ -281,7 +303,7 @@ mod tests {
assert_eq!(config.default_syntax, "foo");
let foo = config.syntaxes.get("foo").unwrap();
- assert_eq!(foo.block_start, "~<");
+ assert_eq!(foo.block_start, "{<");
assert_eq!(foo.block_end, default_syntax.block_end);
assert_eq!(foo.expr_start, default_syntax.expr_start);
assert_eq!(foo.expr_end, default_syntax.expr_end);
@@ -291,7 +313,7 @@ mod tests {
let bar = config.syntaxes.get("bar").unwrap();
assert_eq!(bar.block_start, default_syntax.block_start);
assert_eq!(bar.block_end, default_syntax.block_end);
- assert_eq!(bar.expr_start, "%%");
+ assert_eq!(bar.expr_start, "{!");
assert_eq!(bar.expr_end, default_syntax.expr_end);
assert_eq!(bar.comment_start, default_syntax.comment_start);
assert_eq!(bar.comment_end, default_syntax.comment_end);