Patchwork D9876: rhg: replace `map_*_error` functions with `From` impls

login
register
mail settings
Submitter phabricator
Date Jan. 26, 2021, 8 p.m.
Message ID <differential-rev-PHID-DREV-v7tzrksfiruawm75m3fy-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/48185/
State Superseded
Headers show

Comments

phabricator - Jan. 26, 2021, 8 p.m.
SimonSapin created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D9876

AFFECTED FILES
  rust/rhg/src/commands/cat.rs
  rust/rhg/src/commands/debugdata.rs
  rust/rhg/src/commands/files.rs
  rust/rhg/src/error.rs

CHANGE DETAILS




To: SimonSapin, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/rust/rhg/src/error.rs b/rust/rhg/src/error.rs
--- a/rust/rhg/src/error.rs
+++ b/rust/rhg/src/error.rs
@@ -1,7 +1,11 @@ 
 use crate::exitcode;
+use crate::ui::utf8_to_local;
 use crate::ui::UiError;
 use format_bytes::format_bytes;
-use hg::operations::FindRootError;
+use hg::operations::{
+    CatRevError, DebugDataError, FindRootError, ListDirstateTrackedFilesError,
+    ListRevTrackedFilesError,
+};
 use hg::requirements::RequirementsError;
 use hg::utils::files::get_bytes_from_path;
 use std::convert::From;
@@ -94,3 +98,160 @@ 
         }
     }
 }
+
+impl From<(DebugDataError, &str)> for CommandError {
+    fn from((err, rev): (DebugDataError, &str)) -> CommandError {
+        match err {
+            DebugDataError::IoError(err) => CommandError::Abort(Some(
+                utf8_to_local(&format!("abort: {}\n", err)).into(),
+            )),
+            DebugDataError::InvalidRevision => CommandError::Abort(Some(
+                utf8_to_local(&format!(
+                    "abort: invalid revision identifier{}\n",
+                    rev
+                ))
+                .into(),
+            )),
+            DebugDataError::AmbiguousPrefix => CommandError::Abort(Some(
+                utf8_to_local(&format!(
+                    "abort: ambiguous revision identifier{}\n",
+                    rev
+                ))
+                .into(),
+            )),
+            DebugDataError::UnsuportedRevlogVersion(version) => {
+                CommandError::Abort(Some(
+                    utf8_to_local(&format!(
+                        "abort: unsupported revlog version {}\n",
+                        version
+                    ))
+                    .into(),
+                ))
+            }
+            DebugDataError::CorruptedRevlog => {
+                CommandError::Abort(Some("abort: corrupted revlog\n".into()))
+            }
+            DebugDataError::UnknowRevlogDataFormat(format) => {
+                CommandError::Abort(Some(
+                    utf8_to_local(&format!(
+                        "abort: unknow revlog dataformat {:?}\n",
+                        format
+                    ))
+                    .into(),
+                ))
+            }
+        }
+    }
+}
+
+impl From<(ListRevTrackedFilesError, &str)> for CommandError {
+    fn from((err, rev): (ListRevTrackedFilesError, &str)) -> CommandError {
+        match err {
+            ListRevTrackedFilesError::IoError(err) => CommandError::Abort(
+                Some(utf8_to_local(&format!("abort: {}\n", err)).into()),
+            ),
+            ListRevTrackedFilesError::InvalidRevision => {
+                CommandError::Abort(Some(
+                    utf8_to_local(&format!(
+                        "abort: invalid revision identifier {}\n",
+                        rev
+                    ))
+                    .into(),
+                ))
+            }
+            ListRevTrackedFilesError::AmbiguousPrefix => {
+                CommandError::Abort(Some(
+                    utf8_to_local(&format!(
+                        "abort: ambiguous revision identifier {}\n",
+                        rev
+                    ))
+                    .into(),
+                ))
+            }
+            ListRevTrackedFilesError::UnsuportedRevlogVersion(version) => {
+                CommandError::Abort(Some(
+                    utf8_to_local(&format!(
+                        "abort: unsupported revlog version {}\n",
+                        version
+                    ))
+                    .into(),
+                ))
+            }
+            ListRevTrackedFilesError::CorruptedRevlog => {
+                CommandError::Abort(Some("abort: corrupted revlog\n".into()))
+            }
+            ListRevTrackedFilesError::UnknowRevlogDataFormat(format) => {
+                CommandError::Abort(Some(
+                    utf8_to_local(&format!(
+                        "abort: unknow revlog dataformat {:?}\n",
+                        format
+                    ))
+                    .into(),
+                ))
+            }
+        }
+    }
+}
+
+impl From<(CatRevError, &str)> for CommandError {
+    fn from((err, rev): (CatRevError, &str)) -> CommandError {
+        match err {
+            CatRevError::IoError(err) => CommandError::Abort(Some(
+                utf8_to_local(&format!("abort: {}\n", err)).into(),
+            )),
+            CatRevError::InvalidRevision => CommandError::Abort(Some(
+                utf8_to_local(&format!(
+                    "abort: invalid revision identifier {}\n",
+                    rev
+                ))
+                .into(),
+            )),
+            CatRevError::AmbiguousPrefix => CommandError::Abort(Some(
+                utf8_to_local(&format!(
+                    "abort: ambiguous revision identifier {}\n",
+                    rev
+                ))
+                .into(),
+            )),
+            CatRevError::UnsuportedRevlogVersion(version) => {
+                CommandError::Abort(Some(
+                    utf8_to_local(&format!(
+                        "abort: unsupported revlog version {}\n",
+                        version
+                    ))
+                    .into(),
+                ))
+            }
+            CatRevError::CorruptedRevlog => {
+                CommandError::Abort(Some("abort: corrupted revlog\n".into()))
+            }
+            CatRevError::UnknowRevlogDataFormat(format) => {
+                CommandError::Abort(Some(
+                    utf8_to_local(&format!(
+                        "abort: unknow revlog dataformat {:?}\n",
+                        format
+                    ))
+                    .into(),
+                ))
+            }
+        }
+    }
+}
+
+impl From<ListDirstateTrackedFilesError> for CommandError {
+    fn from(err: ListDirstateTrackedFilesError) -> Self {
+        match err {
+            ListDirstateTrackedFilesError::IoError(err) => {
+                CommandError::Abort(Some(
+                    utf8_to_local(&format!("abort: {}\n", err)).into(),
+                ))
+            }
+            ListDirstateTrackedFilesError::ParseError(_) => {
+                CommandError::Abort(Some(
+                    // TODO find a better error message
+                    b"abort: parse error\n".to_vec(),
+                ))
+            }
+        }
+    }
+}
diff --git a/rust/rhg/src/commands/files.rs b/rust/rhg/src/commands/files.rs
--- a/rust/rhg/src/commands/files.rs
+++ b/rust/rhg/src/commands/files.rs
@@ -1,9 +1,8 @@ 
 use crate::commands::Command;
 use crate::error::CommandError;
-use crate::ui::utf8_to_local;
 use crate::ui::Ui;
-use hg::operations::{list_rev_tracked_files, ListRevTrackedFilesError};
-use hg::operations::{Dirstate, ListDirstateTrackedFilesError};
+use hg::operations::list_rev_tracked_files;
+use hg::operations::Dirstate;
 use hg::repo::Repo;
 use hg::utils::files::{get_bytes_from_path, relativize_path};
 use hg::utils::hg_path::{HgPath, HgPathBuf};
@@ -52,76 +51,13 @@ 
         let repo = Repo::find()?;
         repo.check_requirements()?;
         if let Some(rev) = self.rev {
-            let files = list_rev_tracked_files(&repo, rev)
-                .map_err(|e| map_rev_error(rev, e))?;
+            let files =
+                list_rev_tracked_files(&repo, rev).map_err(|e| (e, rev))?;
             self.display_files(ui, &repo, files.iter())
         } else {
-            let distate = Dirstate::new(&repo).map_err(map_dirstate_error)?;
-            let files = distate.tracked_files().map_err(map_dirstate_error)?;
+            let distate = Dirstate::new(&repo)?;
+            let files = distate.tracked_files()?;
             self.display_files(ui, &repo, files)
         }
     }
 }
-
-/// Convert `ListRevTrackedFilesError` to `CommandError`
-fn map_rev_error(rev: &str, err: ListRevTrackedFilesError) -> CommandError {
-    match err {
-        ListRevTrackedFilesError::IoError(err) => CommandError::Abort(Some(
-            utf8_to_local(&format!("abort: {}\n", err)).into(),
-        )),
-        ListRevTrackedFilesError::InvalidRevision => {
-            CommandError::Abort(Some(
-                utf8_to_local(&format!(
-                    "abort: invalid revision identifier {}\n",
-                    rev
-                ))
-                .into(),
-            ))
-        }
-        ListRevTrackedFilesError::AmbiguousPrefix => {
-            CommandError::Abort(Some(
-                utf8_to_local(&format!(
-                    "abort: ambiguous revision identifier {}\n",
-                    rev
-                ))
-                .into(),
-            ))
-        }
-        ListRevTrackedFilesError::UnsuportedRevlogVersion(version) => {
-            CommandError::Abort(Some(
-                utf8_to_local(&format!(
-                    "abort: unsupported revlog version {}\n",
-                    version
-                ))
-                .into(),
-            ))
-        }
-        ListRevTrackedFilesError::CorruptedRevlog => {
-            CommandError::Abort(Some("abort: corrupted revlog\n".into()))
-        }
-        ListRevTrackedFilesError::UnknowRevlogDataFormat(format) => {
-            CommandError::Abort(Some(
-                utf8_to_local(&format!(
-                    "abort: unknow revlog dataformat {:?}\n",
-                    format
-                ))
-                .into(),
-            ))
-        }
-    }
-}
-
-/// Convert `ListDirstateTrackedFilesError` to `CommandError`
-fn map_dirstate_error(err: ListDirstateTrackedFilesError) -> CommandError {
-    match err {
-        ListDirstateTrackedFilesError::IoError(err) => CommandError::Abort(
-            Some(utf8_to_local(&format!("abort: {}\n", err)).into()),
-        ),
-        ListDirstateTrackedFilesError::ParseError(_) => {
-            CommandError::Abort(Some(
-                // TODO find a better error message
-                b"abort: parse error\n".to_vec(),
-            ))
-        }
-    }
-}
diff --git a/rust/rhg/src/commands/debugdata.rs b/rust/rhg/src/commands/debugdata.rs
--- a/rust/rhg/src/commands/debugdata.rs
+++ b/rust/rhg/src/commands/debugdata.rs
@@ -1,8 +1,7 @@ 
 use crate::commands::Command;
 use crate::error::CommandError;
-use crate::ui::utf8_to_local;
 use crate::ui::Ui;
-use hg::operations::{debug_data, DebugDataError, DebugDataKind};
+use hg::operations::{debug_data, DebugDataKind};
 use hg::repo::Repo;
 use micro_timer::timed;
 
@@ -26,7 +25,7 @@ 
     fn run(&self, ui: &Ui) -> Result<(), CommandError> {
         let repo = Repo::find()?;
         let data = debug_data(&repo, self.rev, self.kind)
-            .map_err(|e| to_command_error(self.rev, e))?;
+            .map_err(|e| (e, self.rev))?;
 
         let mut stdout = ui.stdout_buffer();
         stdout.write_all(&data)?;
@@ -35,47 +34,3 @@ 
         Ok(())
     }
 }
-
-/// Convert operation errors to command errors
-fn to_command_error(rev: &str, err: DebugDataError) -> CommandError {
-    match err {
-        DebugDataError::IoError(err) => CommandError::Abort(Some(
-            utf8_to_local(&format!("abort: {}\n", err)).into(),
-        )),
-        DebugDataError::InvalidRevision => CommandError::Abort(Some(
-            utf8_to_local(&format!(
-                "abort: invalid revision identifier{}\n",
-                rev
-            ))
-            .into(),
-        )),
-        DebugDataError::AmbiguousPrefix => CommandError::Abort(Some(
-            utf8_to_local(&format!(
-                "abort: ambiguous revision identifier{}\n",
-                rev
-            ))
-            .into(),
-        )),
-        DebugDataError::UnsuportedRevlogVersion(version) => {
-            CommandError::Abort(Some(
-                utf8_to_local(&format!(
-                    "abort: unsupported revlog version {}\n",
-                    version
-                ))
-                .into(),
-            ))
-        }
-        DebugDataError::CorruptedRevlog => {
-            CommandError::Abort(Some("abort: corrupted revlog\n".into()))
-        }
-        DebugDataError::UnknowRevlogDataFormat(format) => {
-            CommandError::Abort(Some(
-                utf8_to_local(&format!(
-                    "abort: unknow revlog dataformat {:?}\n",
-                    format
-                ))
-                .into(),
-            ))
-        }
-    }
-}
diff --git a/rust/rhg/src/commands/cat.rs b/rust/rhg/src/commands/cat.rs
--- a/rust/rhg/src/commands/cat.rs
+++ b/rust/rhg/src/commands/cat.rs
@@ -1,8 +1,7 @@ 
 use crate::commands::Command;
 use crate::error::CommandError;
-use crate::ui::utf8_to_local;
 use crate::ui::Ui;
-use hg::operations::{cat, CatRevError};
+use hg::operations::cat;
 use hg::repo::Repo;
 use hg::utils::hg_path::HgPathBuf;
 use micro_timer::timed;
@@ -49,55 +48,10 @@ 
 
         match self.rev {
             Some(rev) => {
-                let data = cat(&repo, rev, &files)
-                    .map_err(|e| map_rev_error(rev, e))?;
+                let data = cat(&repo, rev, &files).map_err(|e| (e, rev))?;
                 self.display(ui, &data)
             }
             None => Err(CommandError::Unimplemented.into()),
         }
     }
 }
-
-/// Convert `CatRevError` to `CommandError`
-fn map_rev_error(rev: &str, err: CatRevError) -> CommandError {
-    match err {
-        CatRevError::IoError(err) => CommandError::Abort(Some(
-            utf8_to_local(&format!("abort: {}\n", err)).into(),
-        )),
-        CatRevError::InvalidRevision => CommandError::Abort(Some(
-            utf8_to_local(&format!(
-                "abort: invalid revision identifier {}\n",
-                rev
-            ))
-            .into(),
-        )),
-        CatRevError::AmbiguousPrefix => CommandError::Abort(Some(
-            utf8_to_local(&format!(
-                "abort: ambiguous revision identifier {}\n",
-                rev
-            ))
-            .into(),
-        )),
-        CatRevError::UnsuportedRevlogVersion(version) => {
-            CommandError::Abort(Some(
-                utf8_to_local(&format!(
-                    "abort: unsupported revlog version {}\n",
-                    version
-                ))
-                .into(),
-            ))
-        }
-        CatRevError::CorruptedRevlog => {
-            CommandError::Abort(Some("abort: corrupted revlog\n".into()))
-        }
-        CatRevError::UnknowRevlogDataFormat(format) => {
-            CommandError::Abort(Some(
-                utf8_to_local(&format!(
-                    "abort: unknow revlog dataformat {:?}\n",
-                    format
-                ))
-                .into(),
-            ))
-        }
-    }
-}