revset, templater: split parse error constructor that sets source error object

I'm going to add RevsetParseError constructor for InvalidFunctionArguments,
with/without a source error, and I don't want to duplicate code for all
combinations. The templater change is just for consistency.

I couldn't find a good naming convention for the builder-like API, so it's
called .with_source(mut self, _). Another option was .source_set(source).
Apparently, it's not uncommon to name consuming constructor as
with_<something>().
This commit is contained in:
Yuya Nishihara 2024-03-29 12:39:34 +09:00
parent db15571eca
commit b09732f4f8
2 changed files with 16 additions and 43 deletions

View file

@ -119,21 +119,9 @@ impl TemplateParseError {
} }
} }
pub fn with_span_and_source( pub fn with_source(mut self, source: impl Into<Box<dyn error::Error + Send + Sync>>) -> Self {
kind: TemplateParseErrorKind, self.source = Some(source.into());
span: pest::Span<'_>, self
source: impl Into<Box<dyn error::Error + Send + Sync>>,
) -> Self {
let message = kind.to_string();
let pest_error = Box::new(pest::error::Error::new_from_span(
pest::error::ErrorVariant::CustomError { message },
span,
));
TemplateParseError {
kind,
pest_error,
source: Some(source.into()),
}
} }
// TODO: migrate all callers to table-based lookup_method() // TODO: migrate all callers to table-based lookup_method()
@ -179,19 +167,19 @@ impl TemplateParseError {
source: impl Into<Box<dyn error::Error + Send + Sync>>, source: impl Into<Box<dyn error::Error + Send + Sync>>,
span: pest::Span<'_>, span: pest::Span<'_>,
) -> Self { ) -> Self {
TemplateParseError::with_span_and_source( TemplateParseError::with_span(
TemplateParseErrorKind::UnexpectedExpression(message.into()), TemplateParseErrorKind::UnexpectedExpression(message.into()),
span, span,
source,
) )
.with_source(source)
} }
pub fn within_alias_expansion(self, id: TemplateAliasId<'_>, span: pest::Span<'_>) -> Self { pub fn within_alias_expansion(self, id: TemplateAliasId<'_>, span: pest::Span<'_>) -> Self {
TemplateParseError::with_span_and_source( TemplateParseError::with_span(
TemplateParseErrorKind::BadAliasExpansion(id.to_string()), TemplateParseErrorKind::BadAliasExpansion(id.to_string()),
span, span,
self,
) )
.with_source(self)
} }
/// If this is a `NoSuchKeyword` error, expands the candidates list with the /// If this is a `NoSuchKeyword` error, expands the candidates list with the
@ -435,11 +423,8 @@ fn parse_term_node(pair: Pair<Rule>) -> TemplateParseResult<ExpressionNode> {
} }
Rule::integer_literal => { Rule::integer_literal => {
let value = expr.as_str().parse().map_err(|err| { let value = expr.as_str().parse().map_err(|err| {
TemplateParseError::with_span_and_source( TemplateParseError::with_span(TemplateParseErrorKind::ParseIntError, span)
TemplateParseErrorKind::ParseIntError, .with_source(err)
span,
err,
)
})?; })?;
ExpressionNode::new(ExpressionKind::Integer(value), span) ExpressionNode::new(ExpressionKind::Integer(value), span)
} }

View file

@ -204,21 +204,9 @@ impl RevsetParseError {
} }
} }
fn with_span_and_source( fn with_source(mut self, source: impl Into<Box<dyn error::Error + Send + Sync>>) -> Self {
kind: RevsetParseErrorKind, self.source = Some(source.into());
span: pest::Span<'_>, self
source: impl Into<Box<dyn error::Error + Send + Sync>>,
) -> Self {
let message = kind.to_string();
let pest_error = Box::new(pest::error::Error::new_from_span(
pest::error::ErrorVariant::CustomError { message },
span,
));
RevsetParseError {
kind,
pest_error,
source: Some(source.into()),
}
} }
pub fn kind(&self) -> &RevsetParseErrorKind { pub fn kind(&self) -> &RevsetParseErrorKind {
@ -803,11 +791,11 @@ impl ParseState<'_> {
allow_string_pattern: self.allow_string_pattern, allow_string_pattern: self.allow_string_pattern,
}; };
f(expanding_state).map_err(|e| { f(expanding_state).map_err(|e| {
RevsetParseError::with_span_and_source( RevsetParseError::with_span(
RevsetParseErrorKind::BadAliasExpansion(id.to_string()), RevsetParseErrorKind::BadAliasExpansion(id.to_string()),
span, span,
e,
) )
.with_source(e)
}) })
} }
} }
@ -1269,14 +1257,14 @@ static BUILTIN_FUNCTION_MAP: Lazy<HashMap<&'static str, RevsetFunction>> = Lazy:
let needle = parse_function_argument_to_string(name, arg, state)?; let needle = parse_function_argument_to_string(name, arg, state)?;
let path = RepoPathBuf::parse_fs_path(ctx.cwd, ctx.workspace_root, needle) let path = RepoPathBuf::parse_fs_path(ctx.cwd, ctx.workspace_root, needle)
.map_err(|e| { .map_err(|e| {
RevsetParseError::with_span_and_source( RevsetParseError::with_span(
RevsetParseErrorKind::InvalidFunctionArguments { RevsetParseErrorKind::InvalidFunctionArguments {
name: name.to_owned(), name: name.to_owned(),
message: "Invalid file pattern".to_owned(), message: "Invalid file pattern".to_owned(),
}, },
span, span,
e,
) )
.with_source(e)
})?; })?;
Ok(path) Ok(path)
}) })