templater: rename ListTemplate and "list" parsing node to "concat"

It's getting confusing since we now have a list property type.

expand/normalize_list() functions aren't renamed since they are also applied
to a list of function arguments.
This commit is contained in:
Yuya Nishihara 2023-03-07 18:11:54 +09:00
parent aff5cd01d2
commit f8f24399f2
3 changed files with 15 additions and 15 deletions

View file

@ -53,11 +53,11 @@ term = {
primary ~ ("." ~ function)*
}
list = _{
concat = _{
term ~ (whitespace* ~ "++" ~ whitespace* ~ term)+
}
template = { list | term }
template = { concat | term }
program = _{ SOI ~ whitespace* ~ template? ~ whitespace* ~ EOI }

View file

@ -25,7 +25,7 @@ use pest_derive::Parser;
use thiserror::Error;
use crate::templater::{
ConditionalTemplate, IndentTemplate, IntoTemplate, LabelTemplate, ListTemplate, Literal,
ConcatTemplate, ConditionalTemplate, IndentTemplate, IntoTemplate, LabelTemplate, Literal,
PlainTextFormattedProperty, SeparateTemplate, Template, TemplateFunction, TemplateProperty,
TimestampRange,
};
@ -226,7 +226,7 @@ enum ExpressionKind<'i> {
Identifier(&'i str),
Integer(i64),
String(String),
List(Vec<ExpressionNode<'i>>),
Concat(Vec<ExpressionNode<'i>>),
FunctionCall(FunctionCallNode<'i>),
MethodCall(MethodCallNode<'i>),
/// Identity node to preserve the span in the source template text.
@ -335,7 +335,7 @@ fn parse_template_node(pair: Pair<Rule>) -> TemplateParseResult<ExpressionNode>
if nodes.len() == 1 {
Ok(nodes.pop().unwrap())
} else {
Ok(ExpressionNode::new(ExpressionKind::List(nodes), span))
Ok(ExpressionNode::new(ExpressionKind::Concat(nodes), span))
}
}
@ -345,7 +345,7 @@ pub fn parse_template(template_text: &str) -> TemplateParseResult<ExpressionNode
let first_pair = pairs.next().unwrap();
if first_pair.as_rule() == Rule::EOI {
let span = first_pair.as_span();
Ok(ExpressionNode::new(ExpressionKind::List(Vec::new()), span))
Ok(ExpressionNode::new(ExpressionKind::Concat(vec![]), span))
} else {
parse_template_node(first_pair)
}
@ -540,8 +540,8 @@ pub fn expand_aliases<'i>(
}
ExpressionKind::Integer(_) => Ok(node),
ExpressionKind::String(_) => Ok(node),
ExpressionKind::List(nodes) => {
node.kind = ExpressionKind::List(expand_list(nodes, state)?);
ExpressionKind::Concat(nodes) => {
node.kind = ExpressionKind::Concat(expand_list(nodes, state)?);
Ok(node)
}
ExpressionKind::FunctionCall(function) => {
@ -1077,7 +1077,7 @@ fn build_global_function<'a, L: TemplateLanguage<'a>>(
.iter()
.map(|node| build_expression(language, node).map(|x| x.into_template()))
.try_collect()?;
language.wrap_template(ListTemplate(contents))
language.wrap_template(ConcatTemplate(contents))
}
"separate" => {
let ([separator_node], content_nodes) = expect_some_arguments(function)?;
@ -1111,12 +1111,12 @@ pub fn build_expression<'a, L: TemplateLanguage<'a>>(
let property = language.wrap_string(Literal(value.clone()));
Ok(Expression::unlabeled(property))
}
ExpressionKind::List(nodes) => {
ExpressionKind::Concat(nodes) => {
let templates = nodes
.iter()
.map(|node| build_expression(language, node).map(|x| x.into_template()))
.try_collect()?;
let property = language.wrap_template(ListTemplate(templates));
let property = language.wrap_template(ConcatTemplate(templates));
Ok(Expression::unlabeled(property))
}
ExpressionKind::FunctionCall(function) => build_global_function(language, function),
@ -1211,7 +1211,7 @@ mod tests {
ExpressionKind::Identifier(_)
| ExpressionKind::Integer(_)
| ExpressionKind::String(_) => node.kind,
ExpressionKind::List(nodes) => ExpressionKind::List(normalize_list(nodes)),
ExpressionKind::Concat(nodes) => ExpressionKind::Concat(normalize_list(nodes)),
ExpressionKind::FunctionCall(function) => {
ExpressionKind::FunctionCall(normalize_function_call(function))
}

View file

@ -182,9 +182,9 @@ where
}
}
pub struct ListTemplate<T>(pub Vec<T>);
pub struct ConcatTemplate<T>(pub Vec<T>);
impl<C, T: Template<C>> Template<C> for ListTemplate<T> {
impl<C, T: Template<C>> Template<C> for ConcatTemplate<T> {
fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()> {
for template in &self.0 {
template.format(context, formatter)?
@ -193,7 +193,7 @@ impl<C, T: Template<C>> Template<C> for ListTemplate<T> {
}
}
/// Like `ListTemplate`, but inserts a separator between non-empty templates.
/// Like `ConcatTemplate`, but inserts a separator between non-empty templates.
pub struct SeparateTemplate<S, T> {
separator: S,
contents: Vec<T>,