From 185549f0318daebb2614ab9177a30acb1938da9d Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sat, 25 Mar 2023 12:32:36 +0900 Subject: [PATCH] revset: extract helper to parse literal to e.g. usize --- lib/src/revset.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/src/revset.rs b/lib/src/revset.rs index 5ae0339db..bbe9b841e 100644 --- a/lib/src/revset.rs +++ b/lib/src/revset.rs @@ -16,6 +16,7 @@ use std::collections::HashMap; use std::ops::Range; use std::path::Path; use std::rc::Rc; +use std::str::FromStr; use std::sync::Arc; use std::{error, fmt}; @@ -1037,17 +1038,29 @@ fn parse_function_argument_to_string( pair: Pair, state: ParseState, ) -> Result { + parse_function_argument_as_literal("string", name, pair, state) +} + +fn parse_function_argument_as_literal( + type_name: &str, + name: &str, + pair: Pair, + state: ParseState, +) -> Result { let span = pair.as_span(); - let expression = parse_expression_rule(pair.into_inner(), state)?; - match expression.as_ref() { - RevsetExpression::Symbol(symbol) => Ok(symbol.clone()), - _ => Err(RevsetParseError::with_span( + let make_error = || { + RevsetParseError::with_span( RevsetParseErrorKind::InvalidFunctionArguments { name: name.to_string(), - message: "Expected function argument of type string".to_owned(), + message: format!("Expected function argument of type {type_name}"), }, span, - )), + ) + }; + let expression = parse_expression_rule(pair.into_inner(), state)?; + match expression.as_ref() { + RevsetExpression::Symbol(symbol) => symbol.parse().map_err(|_| make_error()), + _ => Err(make_error()), } }