From 4953467e4491f2939d87bd263ed5ec76fb6f98d0 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sat, 7 Jan 2023 11:14:01 -0800 Subject: [PATCH] templater: expect a list of labels instead of splitting on whitespace It's clearly the parser's job to split labels in a string provided by the user. This patch moves the splitting we were doing in `LabelTemplate` and `DynamicLabelTemplate` to the parser. In the former case, the string isn't even provided by the user and it doesn't contain whitespace, we can drop the splitting altogether. --- src/template_parser.rs | 12 ++++++++---- src/templater.rs | 13 +++++-------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/template_parser.rs b/src/template_parser.rs index 83abfa2c3..7b57dc98d 100644 --- a/src/template_parser.rs +++ b/src/template_parser.rs @@ -390,14 +390,14 @@ fn parse_commit_term<'a>( } } Rule::identifier => { - let (term_property, labels) = parse_commit_keyword(repo, workspace_id, expr); + let (term_property, label) = parse_commit_keyword(repo, workspace_id, expr); let property = parse_method_chain(maybe_method, term_property); let string_property = coerce_to_string(property); Box::new(LabelTemplate::new( Box::new(StringPropertyTemplate { property: string_property, }), - labels, + vec![label], )) } Rule::function => { @@ -420,11 +420,15 @@ fn parse_commit_term<'a>( } let content: Box + 'a> = parse_commit_template_rule(repo, workspace_id, arg_template); - let get_labels = move |commit: &Commit| -> String { + let get_labels = move |commit: &Commit| -> Vec { let mut buf = vec![]; let mut formatter = PlainTextFormatter::new(&mut buf); label_template.format(commit, &mut formatter).unwrap(); - String::from_utf8(buf).unwrap() + String::from_utf8(buf) + .unwrap() + .split_whitespace() + .map(ToString::to_string) + .collect() }; Box::new(DynamicLabelTemplate::new(content, Box::new(get_labels))) } diff --git a/src/templater.rs b/src/templater.rs index b598addbc..4fb03f882 100644 --- a/src/templater.rs +++ b/src/templater.rs @@ -64,11 +64,7 @@ pub struct LabelTemplate<'a, C> { } impl<'a, C> LabelTemplate<'a, C> { - pub fn new(content: Box + 'a>, labels: String) -> Self { - let labels = labels - .split_whitespace() - .map(|label| label.to_string()) - .collect_vec(); + pub fn new(content: Box + 'a>, labels: Vec) -> Self { LabelTemplate { content, labels } } } @@ -86,16 +82,18 @@ impl<'a, C> Template for LabelTemplate<'a, C> { } } +pub type DynamicLabelFunction<'a, C> = Box Vec + 'a>; + // TODO: figure out why this lifetime is needed pub struct DynamicLabelTemplate<'a, C> { content: Box + 'a>, - label_property: Box String + 'a>, + label_property: DynamicLabelFunction<'a, C>, } impl<'a, C> DynamicLabelTemplate<'a, C> { pub fn new( content: Box + 'a>, - label_property: Box String + 'a>, + label_property: DynamicLabelFunction<'a, C>, ) -> Self { DynamicLabelTemplate { content, @@ -107,7 +105,6 @@ impl<'a, C> DynamicLabelTemplate<'a, C> { impl<'a, C> Template for DynamicLabelTemplate<'a, C> { fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()> { let labels = self.label_property.as_ref()(context); - let labels = labels.split_whitespace().collect_vec(); for label in &labels { formatter.add_label(label)?; }