mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-19 19:08:08 +00:00
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:
parent
db15571eca
commit
b09732f4f8
2 changed files with 16 additions and 43 deletions
|
@ -119,21 +119,9 @@ impl TemplateParseError {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn with_span_and_source(
|
||||
kind: TemplateParseErrorKind,
|
||||
span: pest::Span<'_>,
|
||||
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()),
|
||||
}
|
||||
pub fn with_source(mut self, source: impl Into<Box<dyn error::Error + Send + Sync>>) -> Self {
|
||||
self.source = Some(source.into());
|
||||
self
|
||||
}
|
||||
|
||||
// TODO: migrate all callers to table-based lookup_method()
|
||||
|
@ -179,19 +167,19 @@ impl TemplateParseError {
|
|||
source: impl Into<Box<dyn error::Error + Send + Sync>>,
|
||||
span: pest::Span<'_>,
|
||||
) -> Self {
|
||||
TemplateParseError::with_span_and_source(
|
||||
TemplateParseError::with_span(
|
||||
TemplateParseErrorKind::UnexpectedExpression(message.into()),
|
||||
span,
|
||||
source,
|
||||
)
|
||||
.with_source(source)
|
||||
}
|
||||
|
||||
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()),
|
||||
span,
|
||||
self,
|
||||
)
|
||||
.with_source(self)
|
||||
}
|
||||
|
||||
/// 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 => {
|
||||
let value = expr.as_str().parse().map_err(|err| {
|
||||
TemplateParseError::with_span_and_source(
|
||||
TemplateParseErrorKind::ParseIntError,
|
||||
span,
|
||||
err,
|
||||
)
|
||||
TemplateParseError::with_span(TemplateParseErrorKind::ParseIntError, span)
|
||||
.with_source(err)
|
||||
})?;
|
||||
ExpressionNode::new(ExpressionKind::Integer(value), span)
|
||||
}
|
||||
|
|
|
@ -204,21 +204,9 @@ impl RevsetParseError {
|
|||
}
|
||||
}
|
||||
|
||||
fn with_span_and_source(
|
||||
kind: RevsetParseErrorKind,
|
||||
span: pest::Span<'_>,
|
||||
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()),
|
||||
}
|
||||
fn with_source(mut self, source: impl Into<Box<dyn error::Error + Send + Sync>>) -> Self {
|
||||
self.source = Some(source.into());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn kind(&self) -> &RevsetParseErrorKind {
|
||||
|
@ -803,11 +791,11 @@ impl ParseState<'_> {
|
|||
allow_string_pattern: self.allow_string_pattern,
|
||||
};
|
||||
f(expanding_state).map_err(|e| {
|
||||
RevsetParseError::with_span_and_source(
|
||||
RevsetParseError::with_span(
|
||||
RevsetParseErrorKind::BadAliasExpansion(id.to_string()),
|
||||
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 path = RepoPathBuf::parse_fs_path(ctx.cwd, ctx.workspace_root, needle)
|
||||
.map_err(|e| {
|
||||
RevsetParseError::with_span_and_source(
|
||||
RevsetParseError::with_span(
|
||||
RevsetParseErrorKind::InvalidFunctionArguments {
|
||||
name: name.to_owned(),
|
||||
message: "Invalid file pattern".to_owned(),
|
||||
},
|
||||
span,
|
||||
e,
|
||||
)
|
||||
.with_source(e)
|
||||
})?;
|
||||
Ok(path)
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue