D10238: rhg: Add more conversions between error types

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

D10238: rhg: Add more conversions between error types

marmoute (Pierre-Yves David)
SimonSapin created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  This allows using the `?` operator in more places, as the next commit does.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  rust/hg-core/src/config/config.rs
  rust/hg-core/src/errors.rs
  rust/hg-core/src/lib.rs
  rust/rhg/src/error.rs

CHANGE DETAILS

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
@@ -2,11 +2,12 @@
 use crate::ui::UiError;
 use crate::NoRepoInCwdError;
 use format_bytes::format_bytes;
-use hg::config::{ConfigError, ConfigParseError};
+use hg::config::{ConfigError, ConfigParseError, ConfigValueParseError};
 use hg::errors::HgError;
 use hg::repo::RepoError;
 use hg::revlog::revlog::RevlogError;
 use hg::utils::files::get_bytes_from_path;
+use hg::{DirstateError, DirstateMapError, StatusError};
 use std::convert::From;
 
 /// The kind of command error
@@ -61,6 +62,12 @@
     }
 }
 
+impl From<ConfigValueParseError> for CommandError {
+    fn from(error: ConfigValueParseError) -> Self {
+        CommandError::abort(error.to_string())
+    }
+}
+
 impl From<UiError> for CommandError {
     fn from(_error: UiError) -> Self {
         // If we already failed writing to stdout or stderr,
@@ -144,3 +151,24 @@
         }
     }
 }
+
+impl From<StatusError> for CommandError {
+    fn from(error: StatusError) -> Self {
+        CommandError::abort(format!("{}", error))
+    }
+}
+
+impl From<DirstateMapError> for CommandError {
+    fn from(error: DirstateMapError) -> Self {
+        CommandError::abort(format!("{}", error))
+    }
+}
+
+impl From<DirstateError> for CommandError {
+    fn from(error: DirstateError) -> Self {
+        match error {
+            DirstateError::Common(error) => error.into(),
+            DirstateError::Map(error) => error.into(),
+        }
+    }
+}
diff --git a/rust/hg-core/src/lib.rs b/rust/hg-core/src/lib.rs
--- a/rust/hg-core/src/lib.rs
+++ b/rust/hg-core/src/lib.rs
@@ -17,7 +17,8 @@
     dirstate_map::DirstateMap,
     parsers::{pack_dirstate, parse_dirstate, PARENT_SIZE},
     status::{
-        status, BadMatch, BadType, DirstateStatus, StatusError, StatusOptions,
+        status, BadMatch, BadType, DirstateStatus, HgPathCow, StatusError,
+        StatusOptions,
     },
     CopyMap, CopyMapIter, DirstateEntry, DirstateParents, EntryState,
     StateMap, StateMapIter,
diff --git a/rust/hg-core/src/errors.rs b/rust/hg-core/src/errors.rs
--- a/rust/hg-core/src/errors.rs
+++ b/rust/hg-core/src/errors.rs
@@ -88,25 +88,7 @@
             HgError::UnsupportedFeature(explanation) => {
                 write!(f, "unsupported feature: {}", explanation)
             }
-            HgError::ConfigValueParseError(ConfigValueParseError {
-                origin: _,
-                line: _,
-                section,
-                item,
-                value,
-                expected_type,
-            }) => {
-                // TODO: add origin and line number information, here and in
-                // corresponding python code
-                write!(
-                    f,
-                    "config error: {}.{} is not a {} ('{}')",
-                    String::from_utf8_lossy(section),
-                    String::from_utf8_lossy(item),
-                    expected_type,
-                    String::from_utf8_lossy(value)
-                )
-            }
+            HgError::ConfigValueParseError(error) => error.fmt(f),
         }
     }
 }
diff --git a/rust/hg-core/src/config/config.rs b/rust/hg-core/src/config/config.rs
--- a/rust/hg-core/src/config/config.rs
+++ b/rust/hg-core/src/config/config.rs
@@ -17,6 +17,7 @@
 use format_bytes::{write_bytes, DisplayBytes};
 use std::collections::HashSet;
 use std::env;
+use std::fmt;
 use std::path::{Path, PathBuf};
 use std::str;
 
@@ -68,6 +69,21 @@
     pub expected_type: &'static str,
 }
 
+impl fmt::Display for ConfigValueParseError {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        // TODO: add origin and line number information, here and in
+        // corresponding python code
+        write!(
+            f,
+            "config error: {}.{} is not a {} ('{}')",
+            String::from_utf8_lossy(&self.section),
+            String::from_utf8_lossy(&self.item),
+            self.expected_type,
+            String::from_utf8_lossy(&self.value)
+        )
+    }
+}
+
 impl Config {
     /// Load system and user configuration from various files.
     ///



To: SimonSapin, #hg-reviewers
Cc: mercurial-patches, mercurial-devel
_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel