revset: extract function that handles kind:"value" pattern syntax

I also removed comment about the error span. It's unclear whether the kind
was invalid or the value had syntax error.
This commit is contained in:
Yuya Nishihara 2024-04-06 15:05:34 +09:00
parent 47150d2bb4
commit 3c1d485452

View file

@ -1510,33 +1510,43 @@ fn parse_function_argument_to_string_pattern(
pair: Pair<Rule>, pair: Pair<Rule>,
state: ParseState, state: ParseState,
) -> Result<StringPattern, RevsetParseError> { ) -> Result<StringPattern, RevsetParseError> {
let parse_pattern = |value: &str, kind: Option<&str>| match kind {
Some(kind) => StringPattern::from_str_kind(value, kind),
None => Ok(StringPattern::Substring(value.to_owned())),
};
parse_function_argument_as_pattern("string pattern", name, pair, state, parse_pattern)
}
fn parse_function_argument_as_pattern<T, E: Into<Box<dyn error::Error + Send + Sync>>>(
type_name: &str,
function_name: &str,
pair: Pair<Rule>,
state: ParseState,
parse_pattern: impl FnOnce(&str, Option<&str>) -> Result<T, E>,
) -> Result<T, RevsetParseError> {
let span = pair.as_span(); let span = pair.as_span();
let wrap_error = |err: E| {
RevsetParseError::invalid_arguments(function_name, format!("Invalid {type_name}"), span)
.with_source(err)
};
let expression = { let expression = {
let mut inner_state = state; let mut inner_state = state;
inner_state.allow_string_pattern = true; inner_state.allow_string_pattern = true;
parse_expression_rule(pair.into_inner(), inner_state)? parse_expression_rule(pair.into_inner(), inner_state)?
}; };
let pattern = match expression.as_ref() { match expression.as_ref() {
RevsetExpression::CommitRef(RevsetCommitRef::Symbol(symbol)) => { RevsetExpression::CommitRef(RevsetCommitRef::Symbol(symbol)) => {
let needle = symbol.to_owned(); parse_pattern(symbol, None).map_err(wrap_error)
StringPattern::Substring(needle)
} }
RevsetExpression::StringPattern { kind, value } => { RevsetExpression::StringPattern { kind, value } => {
// TODO: error span can be narrowed to the lhs node parse_pattern(value, Some(kind)).map_err(wrap_error)
StringPattern::from_str_kind(value, kind).map_err(|err| {
RevsetParseError::invalid_arguments(name, "Invalid string pattern", span)
.with_source(err)
})?
} }
_ => { _ => Err(RevsetParseError::invalid_arguments(
return Err(RevsetParseError::invalid_arguments( function_name,
name, format!("Expected function argument of {type_name}"),
"Expected function argument of string pattern", span,
span, )),
)); }
}
};
Ok(pattern)
} }
fn parse_function_argument_as_literal<T: FromStr>( fn parse_function_argument_as_literal<T: FromStr>(