save filters and field name separately

This commit is contained in:
Damien Elmes 2020-01-16 17:44:26 +10:00
parent bdac937802
commit cddfddf1c8
2 changed files with 18 additions and 9 deletions

View file

@ -26,7 +26,7 @@ pub enum TemplateError {
NoClosingBrackets(String), NoClosingBrackets(String),
ConditionalNotClosed(String), ConditionalNotClosed(String),
ConditionalNotOpen(String), ConditionalNotOpen(String),
FieldNotFound(String), FieldNotFound { filters: String, field: String },
} }
impl From<TemplateError> for AnkiError { impl From<TemplateError> for AnkiError {
@ -40,9 +40,9 @@ impl From<TemplateError> for AnkiError {
TemplateError::ConditionalNotOpen(tag) => { TemplateError::ConditionalNotOpen(tag) => {
format!("missing '{{{{#{}}}}}' or '{{{{^{}}}}}'", tag, tag) format!("missing '{{{{#{}}}}}' or '{{{{^{}}}}}'", tag, tag)
} }
TemplateError::FieldNotFound(field) => format!( TemplateError::FieldNotFound { field, filters } => format!(
"found '{{{{{}}}}}', but there is no field called '{}'", "found '{{{{{}{}}}}}', but there is no field called '{}'",
field, field filters, field, field
), ),
}, },
} }

View file

@ -330,14 +330,17 @@ fn render_into(
Some(text) => apply_filters(text, filters, key, context), Some(text) => apply_filters(text, filters, key, context),
None => { None => {
// unknown field encountered // unknown field encountered
let name_including_filters = filters let filters_str = filters
.iter() .iter()
.rev() .rev()
.cloned() .cloned()
.chain(iter::once(*key)) .chain(iter::once(""))
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(":"); .join(":");
return Err(TemplateError::FieldNotFound(name_including_filters)); return Err(TemplateError::FieldNotFound {
field: (*key).to_string(),
filters: filters_str,
});
} }
}; };
@ -726,14 +729,20 @@ mod test {
tmpl = PT::from_text("{{X}}").unwrap(); tmpl = PT::from_text("{{X}}").unwrap();
assert_eq!( assert_eq!(
tmpl.render(&ctx).unwrap_err(), tmpl.render(&ctx).unwrap_err(),
TemplateError::FieldNotFound("X".to_owned()) TemplateError::FieldNotFound {
field: "X".to_owned(),
filters: "".to_owned()
}
); );
// unknown field with filters // unknown field with filters
tmpl = PT::from_text("{{foo:text:X}}").unwrap(); tmpl = PT::from_text("{{foo:text:X}}").unwrap();
assert_eq!( assert_eq!(
tmpl.render(&ctx).unwrap_err(), tmpl.render(&ctx).unwrap_err(),
TemplateError::FieldNotFound("foo:text:X".to_owned()) TemplateError::FieldNotFound {
field: "X".to_owned(),
filters: "foo:text:".to_owned()
}
); );
// a blank field is allowed if it has filters // a blank field is allowed if it has filters