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).
--- 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,
+ }
+ }
+}
--- a/rust/rhg/src/error.rs Tue Nov 26 15:45:11 2024 +0000
+++ b/rust/rhg/src/error.rs Tue Nov 26 16:21:00 2024 +0000
@@ -206,20 +206,8 @@
impl From<RevlogError> for CommandError {
fn from(err: RevlogError) -> CommandError {
- match err {
- RevlogError::WDirUnsupported => CommandError::abort(
- "abort: working directory revision cannot be specified",
- ),
- RevlogError::InvalidRevision(r) => CommandError::abort(format!(
- "abort: invalid revision identifier: {}",
- r
- )),
- RevlogError::AmbiguousPrefix(r) => CommandError::abort(format!(
- "abort: ambiguous revision identifier: {}",
- r
- )),
- RevlogError::Other(error) => error.into(),
- }
+ let err: HgError = err.into();
+ err.into()
}
}