diff --git a/rslib/src/err.rs b/rslib/src/err.rs index b664dee9e..9b50a9d30 100644 --- a/rslib/src/err.rs +++ b/rslib/src/err.rs @@ -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, + }, + FieldNotFound { + filters: String, + field: String, + }, } impl From for AnkiError { @@ -37,8 +43,18 @@ impl From 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 '{}'", diff --git a/rslib/src/template.rs b/rslib/src/template.rs index 42deccb1e..cdf0b5995 100644 --- a/rslib/src/template.rs +++ b/rslib/src/template.rs @@ -164,13 +164,20 @@ fn parse_inner<'a, I: Iterator>>>( 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, + }); } }); }