From 78104b5e82c5149c2f0155cb4d8558000f1cc8cb Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Wed, 13 Mar 2024 20:08:25 +0900 Subject: [PATCH] cli: extract function that stringifies RevsetParseError --- cli/src/command_error.rs | 9 ++++----- cli/src/revset_util.rs | 6 ++++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/cli/src/command_error.rs b/cli/src/command_error.rs index 5ac297b25..8fa0af42b 100644 --- a/cli/src/command_error.rs +++ b/cli/src/command_error.rs @@ -38,7 +38,7 @@ use thiserror::Error; use crate::merge_tools::{ ConflictResolveError, DiffEditError, DiffGenerateError, MergeToolConfigError, }; -use crate::revset_util::UserRevsetEvaluationError; +use crate::revset_util::{self, UserRevsetEvaluationError}; use crate::template_parser::{TemplateParseError, TemplateParseErrorKind}; use crate::ui::Ui; @@ -347,10 +347,9 @@ impl From for CommandError { impl From for CommandError { 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 - 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 { op: _, similar_op, @@ -372,7 +371,7 @@ impl From for CommandError { } => format_similarity_hint(candidates), _ => 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) } } diff --git a/cli/src/revset_util.rs b/cli/src/revset_util.rs index e938be40f..0c88c9b49 100644 --- a/cli/src/revset_util.rs +++ b/cli/src/revset_util.rs @@ -14,6 +14,7 @@ //! Utility for parsing and evaluating user-provided revset expressions. +use std::iter; use std::rc::Rc; use itertools::Itertools as _; @@ -135,3 +136,8 @@ pub fn parse_immutable_expression( 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}") +}