diff rust/hg-core/src/errors.rs @ 52344:09a36de53b60

rust: implement conversion of RevlogError into HgError The conversion already exists in rhg, where we need to convert to CommandError. This commit moves it to hg core. This makes it easier to code some middleware where we need to carry around a type that represents any type of hg error (HgError).
author Arseniy Alekseyev <aalekseyev@janestreet.com>
date Tue, 26 Nov 2024 16:21:00 +0000
parents 65d516db7309
children
line wrap: on
line diff
--- a/rust/hg-core/src/errors.rs	Tue Nov 26 15:45:11 2024 +0000
+++ b/rust/hg-core/src/errors.rs	Tue Nov 26 16:21:00 2024 +0000
@@ -1,5 +1,6 @@
 use crate::config::ConfigValueParseError;
 use crate::exit_codes;
+use crate::revlog::RevlogError;
 use crate::utils::hg_path::HgPathError;
 use std::fmt;
 
@@ -103,6 +104,14 @@
             hint,
         }
     }
+
+    pub fn abort_simple(explanation: impl Into<String>) -> Self {
+        HgError::Abort {
+            message: explanation.into(),
+            detailed_exit_code: exit_codes::ABORT,
+            hint: None,
+        }
+    }
 }
 
 // TODO: use `DisplayBytes` instead to show non-Unicode filenames losslessly?
@@ -235,3 +244,22 @@
         }
     }
 }
+
+impl From<RevlogError> for HgError {
+    fn from(err: RevlogError) -> HgError {
+        match err {
+            RevlogError::WDirUnsupported => HgError::abort_simple(
+                "abort: working directory revision cannot be specified",
+            ),
+            RevlogError::InvalidRevision(r) => HgError::abort_simple(format!(
+                "abort: invalid revision identifier: {}",
+                r
+            )),
+            RevlogError::AmbiguousPrefix(r) => HgError::abort_simple(format!(
+                "abort: ambiguous revision identifier: {}",
+                r
+            )),
+            RevlogError::Other(error) => error,
+        }
+    }
+}