From e5fca8fadd9eac249a60180e693afb79c88aa123 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Tue, 21 May 2024 12:39:22 +0900 Subject: [PATCH] templater: add helper that maps FunctionCallNode to error --- cli/src/template_parser.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/cli/src/template_parser.rs b/cli/src/template_parser.rs index e61b35400..91b2f469b 100644 --- a/cli/src/template_parser.rs +++ b/cli/src/template_parser.rs @@ -691,9 +691,10 @@ impl<'i> FunctionCallNode<'i> { pub fn expect_exact_arguments( &self, ) -> TemplateParseResult<&[ExpressionNode<'i>; N]> { - self.args.as_slice().try_into().map_err(|_| { - TemplateParseError::invalid_arguments(self, format!("Expected {N} arguments")) - }) + self.args + .as_slice() + .try_into() + .map_err(|_| self.invalid_arguments(format!("Expected {N} arguments"))) } /// Extracts N required arguments and remainders. @@ -704,10 +705,7 @@ impl<'i> FunctionCallNode<'i> { let (required, rest) = self.args.split_at(N); Ok((required.try_into().unwrap(), rest)) } else { - Err(TemplateParseError::invalid_arguments( - self, - format!("Expected at least {N} arguments"), - )) + Err(self.invalid_arguments(format!("Expected at least {N} arguments"))) } } @@ -722,12 +720,14 @@ impl<'i> FunctionCallNode<'i> { optional.resize(M, None); Ok((required.try_into().unwrap(), optional.try_into().unwrap())) } else { - Err(TemplateParseError::invalid_arguments( - self, - format!("Expected {min} to {max} arguments", min = N, max = N + M), - )) + let (min, max) = count_range.into_inner(); + Err(self.invalid_arguments(format!("Expected {min} to {max} arguments"))) } } + + fn invalid_arguments(&self, message: String) -> TemplateParseError { + TemplateParseError::invalid_arguments(self, message) + } } /// Applies the given function if the `node` is a string literal.