clearer error when closing tags transposed

This commit is contained in:
Damien Elmes 2020-02-08 10:32:34 +10:00
parent 58c643d5bf
commit ed8c1ae9c5
2 changed files with 30 additions and 7 deletions

View File

@ -25,8 +25,14 @@ impl AnkiError {
pub enum TemplateError {
NoClosingBrackets(String),
ConditionalNotClosed(String),
ConditionalNotOpen(String),
FieldNotFound { filters: String, field: String },
ConditionalNotOpen {
closed: String,
currently_open: Option<String>,
},
FieldNotFound {
filters: String,
field: String,
},
}
impl From<TemplateError> for AnkiError {
@ -37,8 +43,18 @@ impl From<TemplateError> for AnkiError {
format!("missing '}}}}' in '{}'", context)
}
TemplateError::ConditionalNotClosed(tag) => format!("missing '{{{{/{}}}}}'", tag),
TemplateError::ConditionalNotOpen(tag) => {
format!("missing '{{{{#{}}}}}' or '{{{{^{}}}}}'", tag, tag)
TemplateError::ConditionalNotOpen {
closed,
currently_open,
} => {
if let Some(open) = currently_open {
format!("Found {{{{/{}}}}}, but expected {{{{/{}}}}}", closed, open)
} else {
format!(
"Found {{{{/{}}}}}, but missing '{{{{#{}}}}}' or '{{{{^{}}}}}'",
closed, closed, closed
)
}
}
TemplateError::FieldNotFound { field, filters } => format!(
"found '{{{{{}{}}}}}', but there is no field called '{}'",

View File

@ -164,13 +164,20 @@ fn parse_inner<'a, I: Iterator<Item = TemplateResult<Token<'a>>>>(
children: parse_inner(iter, Some(t))?,
},
CloseConditional(t) => {
if let Some(open) = open_tag {
let currently_open = if let Some(open) = open_tag {
if open == t {
// matching closing tag, move back to parent
return Ok(nodes);
} else {
Some(open.to_string())
}
}
return Err(TemplateError::ConditionalNotOpen(t.to_string()));
} else {
None
};
return Err(TemplateError::ConditionalNotOpen {
closed: t.to_string(),
currently_open,
});
}
});
}