ok/jj
1
0
Fork 0
forked from mirrors/jj

cli: extract function that stringifies RevsetParseError

This commit is contained in:
Yuya Nishihara 2024-03-13 20:08:25 +09:00
parent 34eb446037
commit 78104b5e82
2 changed files with 10 additions and 5 deletions

View file

@ -38,7 +38,7 @@ use thiserror::Error;
use crate::merge_tools::{ use crate::merge_tools::{
ConflictResolveError, DiffEditError, DiffGenerateError, MergeToolConfigError, ConflictResolveError, DiffEditError, DiffGenerateError, MergeToolConfigError,
}; };
use crate::revset_util::UserRevsetEvaluationError; use crate::revset_util::{self, UserRevsetEvaluationError};
use crate::template_parser::{TemplateParseError, TemplateParseErrorKind}; use crate::template_parser::{TemplateParseError, TemplateParseErrorKind};
use crate::ui::Ui; use crate::ui::Ui;
@ -347,10 +347,9 @@ impl From<RevsetEvaluationError> for CommandError {
impl From<RevsetParseError> for CommandError { impl From<RevsetParseError> for CommandError {
fn from(err: RevsetParseError) -> Self { fn from(err: RevsetParseError) -> Self {
let err_chain = iter::successors(Some(&err), |e| e.origin());
let message = err_chain.clone().join("\n");
// Only for the bottom error, which is usually the root cause // Only for the bottom error, which is usually the root cause
let hint = match err_chain.last().unwrap().kind() { let bottom_err = iter::successors(Some(&err), |e| e.origin()).last().unwrap();
let hint = match bottom_err.kind() {
RevsetParseErrorKind::NotPrefixOperator { RevsetParseErrorKind::NotPrefixOperator {
op: _, op: _,
similar_op, similar_op,
@ -372,7 +371,7 @@ impl From<RevsetParseError> for CommandError {
} => format_similarity_hint(candidates), } => format_similarity_hint(candidates),
_ => None, _ => None,
}; };
user_error_with_hint_opt(format!("Failed to parse revset: {message}"), hint) user_error_with_hint_opt(revset_util::format_parse_error(&err), hint)
} }
} }

View file

@ -14,6 +14,7 @@
//! Utility for parsing and evaluating user-provided revset expressions. //! Utility for parsing and evaluating user-provided revset expressions.
use std::iter;
use std::rc::Rc; use std::rc::Rc;
use itertools::Itertools as _; use itertools::Itertools as _;
@ -135,3 +136,8 @@ pub fn parse_immutable_expression(
repo.store().root_commit_id().clone(), repo.store().root_commit_id().clone(),
))) )))
} }
pub fn format_parse_error(err: &RevsetParseError) -> String {
let message = iter::successors(Some(err), |e| e.origin()).join("\n");
format!("Failed to parse revset: {message}")
}