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 { pub enum TemplateError {
NoClosingBrackets(String), NoClosingBrackets(String),
ConditionalNotClosed(String), ConditionalNotClosed(String),
ConditionalNotOpen(String), ConditionalNotOpen {
FieldNotFound { filters: String, field: String }, closed: String,
currently_open: Option<String>,
},
FieldNotFound {
filters: String,
field: String,
},
} }
impl From<TemplateError> for AnkiError { impl From<TemplateError> for AnkiError {
@ -37,8 +43,18 @@ impl From<TemplateError> for AnkiError {
format!("missing '}}}}' in '{}'", context) format!("missing '}}}}' in '{}'", context)
} }
TemplateError::ConditionalNotClosed(tag) => format!("missing '{{{{/{}}}}}'", tag), TemplateError::ConditionalNotClosed(tag) => format!("missing '{{{{/{}}}}}'", tag),
TemplateError::ConditionalNotOpen(tag) => { TemplateError::ConditionalNotOpen {
format!("missing '{{{{#{}}}}}' or '{{{{^{}}}}}'", tag, tag) 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!( TemplateError::FieldNotFound { field, filters } => format!(
"found '{{{{{}{}}}}}', but there is no field called '{}'", "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))?, children: parse_inner(iter, Some(t))?,
}, },
CloseConditional(t) => { CloseConditional(t) => {
if let Some(open) = open_tag { let currently_open = if let Some(open) = open_tag {
if open == t { if open == t {
// matching closing tag, move back to parent // matching closing tag, move back to parent
return Ok(nodes); return Ok(nodes);
} else {
Some(open.to_string())
} }
} } else {
return Err(TemplateError::ConditionalNotOpen(t.to_string())); None
};
return Err(TemplateError::ConditionalNotOpen {
closed: t.to_string(),
currently_open,
});
} }
}); });
} }