revset: make span of parse error mandatory, remove Option<_>

Since all callers of RevsetParseError have some reasonable span, we don't
need a special case for WorkingCopyWithoutWorkspace error.
This commit is contained in:
Yuya Nishihara 2024-03-26 11:49:12 +09:00
parent 790b5846f6
commit 32efb4034d

View file

@ -144,7 +144,7 @@ impl Rule {
#[derive(Debug)] #[derive(Debug)]
pub struct RevsetParseError { pub struct RevsetParseError {
kind: RevsetParseErrorKind, kind: RevsetParseErrorKind,
pest_error: Option<Box<pest::error::Error<Rule>>>, pest_error: Box<pest::error::Error<Rule>>,
origin: Option<Box<RevsetParseError>>, origin: Option<Box<RevsetParseError>>,
} }
@ -192,23 +192,15 @@ pub enum RevsetParseErrorKind {
} }
impl RevsetParseError { impl RevsetParseError {
fn new(kind: RevsetParseErrorKind) -> Self {
RevsetParseError {
kind,
pest_error: None,
origin: None,
}
}
fn with_span(kind: RevsetParseErrorKind, span: pest::Span<'_>) -> Self { fn with_span(kind: RevsetParseErrorKind, span: pest::Span<'_>) -> Self {
let message = iter::successors(Some(&kind as &dyn error::Error), |e| e.source()).join(": "); let message = iter::successors(Some(&kind as &dyn error::Error), |e| e.source()).join(": ");
let err = pest::error::Error::new_from_span( let pest_error = Box::new(pest::error::Error::new_from_span(
pest::error::ErrorVariant::CustomError { message }, pest::error::ErrorVariant::CustomError { message },
span, span,
); ));
RevsetParseError { RevsetParseError {
kind, kind,
pest_error: Some(Box::new(err)), pest_error,
origin: None, origin: None,
} }
} }
@ -219,13 +211,13 @@ impl RevsetParseError {
origin: Self, origin: Self,
) -> Self { ) -> Self {
let message = iter::successors(Some(&kind as &dyn error::Error), |e| e.source()).join(": "); let message = iter::successors(Some(&kind as &dyn error::Error), |e| e.source()).join(": ");
let err = pest::error::Error::new_from_span( let pest_error = Box::new(pest::error::Error::new_from_span(
pest::error::ErrorVariant::CustomError { message }, pest::error::ErrorVariant::CustomError { message },
span, span,
); ));
RevsetParseError { RevsetParseError {
kind, kind,
pest_error: Some(Box::new(err)), pest_error,
origin: Some(Box::new(origin)), origin: Some(Box::new(origin)),
} }
} }
@ -244,7 +236,7 @@ impl From<pest::error::Error<Rule>> for RevsetParseError {
fn from(err: pest::error::Error<Rule>) -> Self { fn from(err: pest::error::Error<Rule>) -> Self {
RevsetParseError { RevsetParseError {
kind: RevsetParseErrorKind::SyntaxError, kind: RevsetParseErrorKind::SyntaxError,
pest_error: Some(Box::new(rename_rules_in_pest_error(err))), pest_error: Box::new(rename_rules_in_pest_error(err)),
origin: None, origin: None,
} }
} }
@ -252,11 +244,7 @@ impl From<pest::error::Error<Rule>> for RevsetParseError {
impl fmt::Display for RevsetParseError { impl fmt::Display for RevsetParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(err) = &self.pest_error { self.pest_error.fmt(f)
err.fmt(f)
} else {
self.kind.fmt(f)
}
} }
} }
@ -267,9 +255,7 @@ impl error::Error for RevsetParseError {
} }
match &self.kind { match &self.kind {
// SyntaxError is a wrapper for pest::error::Error. // SyntaxError is a wrapper for pest::error::Error.
RevsetParseErrorKind::SyntaxError => { RevsetParseErrorKind::SyntaxError => Some(&self.pest_error as &dyn error::Error),
self.pest_error.as_ref().map(|e| e as &dyn error::Error)
}
// Otherwise the kind represents this error. // Otherwise the kind represents this error.
e => e.source(), e => e.source(),
} }
@ -1000,7 +986,7 @@ fn parse_primary_rule(
Rule::at_op => { Rule::at_op => {
// nullary "@" // nullary "@"
let ctx = state.workspace_ctx.as_ref().ok_or_else(|| { let ctx = state.workspace_ctx.as_ref().ok_or_else(|| {
RevsetParseError::new(RevsetParseErrorKind::WorkingCopyWithoutWorkspace) RevsetParseError::with_span(RevsetParseErrorKind::WorkingCopyWithoutWorkspace, span)
})?; })?;
Ok(RevsetExpression::working_copy(ctx.workspace_id.clone())) Ok(RevsetExpression::working_copy(ctx.workspace_id.clone()))
} }
@ -1295,8 +1281,8 @@ static BUILTIN_FUNCTION_MAP: Lazy<HashMap<&'static str, RevsetFunction>> = Lazy:
Ok(RevsetExpression::filter(RevsetFilterPredicate::File(None)).negated()) Ok(RevsetExpression::filter(RevsetFilterPredicate::File(None)).negated())
}); });
map.insert("file", |name, arguments_pair, state| { map.insert("file", |name, arguments_pair, state| {
let arguments_span = arguments_pair.as_span();
if let Some(ctx) = state.workspace_ctx { if let Some(ctx) = state.workspace_ctx {
let arguments_span = arguments_pair.as_span();
let paths: Vec<_> = arguments_pair let paths: Vec<_> = arguments_pair
.into_inner() .into_inner()
.map(|arg| -> Result<_, RevsetParseError> { .map(|arg| -> Result<_, RevsetParseError> {
@ -1326,8 +1312,9 @@ static BUILTIN_FUNCTION_MAP: Lazy<HashMap<&'static str, RevsetFunction>> = Lazy:
)))) ))))
} }
} else { } else {
Err(RevsetParseError::new( Err(RevsetParseError::with_span(
RevsetParseErrorKind::FsPathWithoutWorkspace, RevsetParseErrorKind::FsPathWithoutWorkspace,
arguments_span,
)) ))
} }
}); });