changeset 46436:252d1bdba33d

rhg: replace `map_*_error` functions with `From` impls Differential Revision: https://phab.mercurial-scm.org/D9876
author Simon Sapin <simon.sapin@octobus.net>
date Tue, 26 Jan 2021 20:31:26 +0100
parents 2e2033081274
children b274aa2f20fd
files rust/rhg/src/commands/cat.rs rust/rhg/src/commands/debugdata.rs rust/rhg/src/commands/files.rs rust/rhg/src/error.rs
diffstat 4 files changed, 172 insertions(+), 166 deletions(-) [+]
line wrap: on
line diff
--- a/rust/rhg/src/commands/cat.rs	Tue Jan 26 20:05:37 2021 +0100
+++ b/rust/rhg/src/commands/cat.rs	Tue Jan 26 20:31:26 2021 +0100
@@ -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(),
-            ))
-        }
-    }
-}
--- a/rust/rhg/src/commands/debugdata.rs	Tue Jan 26 20:05:37 2021 +0100
+++ b/rust/rhg/src/commands/debugdata.rs	Tue Jan 26 20:31:26 2021 +0100
@@ -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(),
-            ))
-        }
-    }
-}
--- a/rust/rhg/src/commands/files.rs	Tue Jan 26 20:05:37 2021 +0100
+++ b/rust/rhg/src/commands/files.rs	Tue Jan 26 20:31:26 2021 +0100
@@ -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(),
-            ))
-        }
-    }
-}
--- a/rust/rhg/src/error.rs	Tue Jan 26 20:05:37 2021 +0100
+++ b/rust/rhg/src/error.rs	Tue Jan 26 20:31:26 2021 +0100
@@ -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(),
+                ))
+            }
+        }
+    }
+}