From 553bee6ac4dc770788dfb11bc5f2f51ae851b766 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Mon, 30 Jan 2023 19:52:41 +0900 Subject: [PATCH] templater: extract method that coerces property to boolean --- src/template_parser.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/template_parser.rs b/src/template_parser.rs index 3007ea88a..f90374a97 100644 --- a/src/template_parser.rs +++ b/src/template_parser.rs @@ -89,6 +89,16 @@ impl<'a, I: 'a> Property<'a, I> { } } + fn try_into_boolean(self) -> Option + 'a>> { + match self { + Property::String(property) => { + Some(Box::new(TemplateFunction::new(property, |s| !s.is_empty()))) + } + Property::Boolean(property) => Some(property), + _ => None, + } + } + fn into_template(self) -> Box + 'a> { fn wrap<'a, I: 'a, O: Template<()> + 'a>( property: Box + 'a>, @@ -273,13 +283,10 @@ fn parse_boolean_commit_property<'a>( let _method = inner.next().unwrap(); assert!(inner.next().is_none()); match pair.as_rule() { - Rule::identifier => match parse_commit_keyword(repo, workspace_id, pair.clone()).0 { - Property::Boolean(property) => property, - Property::String(property) => { - Box::new(TemplateFunction::new(property, |string| !string.is_empty())) - } - _ => panic!("cannot yet use this as boolean: {pair:?}"), - }, + Rule::identifier => parse_commit_keyword(repo, workspace_id, pair.clone()) + .0 + .try_into_boolean() + .unwrap_or_else(|| panic!("cannot yet use this as boolean: {pair:?}")), _ => panic!("cannot yet use this as boolean: {pair:?}"), } }