From b77a110e8ab89411bcaa96c00f4564ab60dd9411 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sat, 23 Mar 2024 15:56:34 +0900 Subject: [PATCH] cli: remove less-frequently-used user_error factories --- cli/src/command_error.rs | 47 +++++++++++++++------------------------- cli/src/commands/git.rs | 7 +++--- 2 files changed, 22 insertions(+), 32 deletions(-) diff --git a/cli/src/command_error.rs b/cli/src/command_error.rs index 0d52ee594..6908a81ed 100644 --- a/cli/src/command_error.rs +++ b/cli/src/command_error.rs @@ -126,23 +126,6 @@ pub fn user_error_with_message( user_error(ErrorWithMessage::new(message, source)) } -pub fn user_error_with_message_and_hint( - message: impl Into, - hint: impl Into, - source: impl Into>, -) -> CommandError { - user_error_with_message(message, source).hinted(hint) -} - -pub fn user_error_with_hint_opt( - err: impl Into>, - hint: Option, -) -> CommandError { - let mut err = CommandError::new(CommandErrorKind::User, err); - err.extend_hints(hint); - err -} - pub fn config_error(err: impl Into>) -> CommandError { CommandError::new(CommandErrorKind::Config, err) } @@ -278,12 +261,12 @@ impl From for CommandError { impl From for CommandError { fn from(err: SnapshotError) -> Self { match err { - SnapshotError::NewFileTooLarge { .. } => user_error_with_message_and_hint( - "Failed to snapshot the working copy", - r#"Increase the value of the `snapshot.max-new-file-size` config option if you + SnapshotError::NewFileTooLarge { .. } => { + user_error_with_message("Failed to snapshot the working copy", err).hinted( + r#"Increase the value of the `snapshot.max-new-file-size` config option if you want this file to be snapshotted. Otherwise add it to your `.gitignore` file."#, - err, - ), + ) + } err => internal_error_with_message("Failed to snapshot the working copy", err), } } @@ -364,10 +347,10 @@ repository contents." GitImportError::InternalGitError(_) => None, GitImportError::UnexpectedBackend => None, }; - user_error_with_hint_opt( - ErrorWithMessage::new("Failed to import refs from underlying Git repo", err), - hint, - ) + let mut cmd_err = + user_error_with_message("Failed to import refs from underlying Git repo", err); + cmd_err.extend_hints(hint); + cmd_err } } @@ -415,7 +398,9 @@ impl From for CommandError { } => format_similarity_hint(candidates), _ => None, }; - user_error_with_hint_opt(revset_util::format_parse_error(&err), hint) + let mut cmd_err = user_error(revset_util::format_parse_error(&err)); + cmd_err.extend_hints(hint); + cmd_err } } @@ -432,7 +417,9 @@ impl From for CommandError { | RevsetResolutionError::AmbiguousChangeIdPrefix(_) | RevsetResolutionError::StoreError(_) => None, }; - user_error_with_hint_opt(err, hint) + let mut cmd_err = user_error(err); + cmd_err.extend_hints(hint); + cmd_err } } @@ -458,7 +445,9 @@ impl From for CommandError { } _ => None, }; - user_error_with_hint_opt(format!("Failed to parse template: {message}"), hint) + let mut cmd_err = user_error(format!("Failed to parse template: {message}")); + cmd_err.extend_hints(hint); + cmd_err } } diff --git a/cli/src/commands/git.rs b/cli/src/commands/git.rs index aae5c0efb..79252d683 100644 --- a/cli/src/commands/git.rs +++ b/cli/src/commands/git.rs @@ -45,8 +45,7 @@ use crate::cli_util::{ start_repo_transaction, CommandHelper, RevisionArg, WorkspaceCommandHelper, }; use crate::command_error::{ - user_error, user_error_with_hint, user_error_with_hint_opt, user_error_with_message, - CommandError, + user_error, user_error_with_hint, user_error_with_message, CommandError, }; use crate::git_util::{ get_git_repo, is_colocated_git_workspace, print_failed_git_export, print_git_import_stats, @@ -1107,7 +1106,9 @@ impl RejectedBranchUpdateReason { impl From for CommandError { fn from(reason: RejectedBranchUpdateReason) -> Self { let RejectedBranchUpdateReason { message, hint } = reason; - user_error_with_hint_opt(message, hint) + let mut cmd_err = user_error(message); + cmd_err.extend_hints(hint); + cmd_err } }