use crate::errors::{Error, TeraResult};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Delimiters {
pub block_start: &'static str,
pub block_end: &'static str,
pub variable_start: &'static str,
pub variable_end: &'static str,
pub comment_start: &'static str,
pub comment_end: &'static str,
}
impl Default for Delimiters {
fn default() -> Self {
Self {
block_start: "{%",
block_end: "%}",
variable_start: "{{",
variable_end: "}}",
comment_start: "{#",
comment_end: "#}",
}
}
}
impl Delimiters {
pub(crate) fn validate(&self) -> TeraResult<()> {
if self.block_start.chars().count() != 2 {
return Err(Error::message(
"`block_start` delimiter must be 2 characters",
));
}
if self.block_end.chars().count() != 2 {
return Err(Error::message("`block_end` delimiter must be 2 characters"));
}
if self.variable_start.chars().count() != 2 {
return Err(Error::message(
"`variable_start` delimiter must be 2 characters",
));
}
if self.variable_end.chars().count() != 2 {
return Err(Error::message(
"`variable_end` delimiter must be 2 characters",
));
}
if self.comment_start.chars().count() != 2 {
return Err(Error::message(
"`comment_start` delimiter must be 2 characters",
));
}
if self.comment_end.chars().count() != 2 {
return Err(Error::message(
"`comment_end` delimiter must be 2 characters",
));
}
if self.block_start == self.variable_start {
return Err(Error::message(
"`block_start` and `variable_start` cannot have the same value",
));
}
if self.block_start == self.comment_start {
return Err(Error::message(
"`block_start` and `comment_start` cannot have the same value",
));
}
if self.variable_start == self.comment_start {
return Err(Error::message(
"`variable_start` and `comment_start` cannot have the same value",
));
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn errors_on_invalid_delimiters() {
let inputs = vec![
Delimiters {
block_start: "",
..Delimiters::default()
},
Delimiters {
block_start: "[[[",
..Delimiters::default()
},
Delimiters {
block_start: "[[",
comment_start: "[[",
..Delimiters::default()
},
];
for i in inputs {
assert!(i.validate().is_err());
}
}
}