[Updated] [+- ] D12165: rhg: Make Ui::new falliable, add Ui::new_infallible

SimonSapin phabricator at mercurial-scm.org
Fri Feb 11 11:43:12 UTC 2022


SimonSapin updated this revision to Diff 32144.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D12165?vs=32124&id=32144

BRANCH
  default

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D12165/new/

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

AFFECTED FILES
  rust/rhg/src/main.rs
  rust/rhg/src/ui.rs

CHANGE DETAILS

diff --git a/rust/rhg/src/ui.rs b/rust/rhg/src/ui.rs
--- a/rust/rhg/src/ui.rs
+++ b/rust/rhg/src/ui.rs
@@ -1,5 +1,6 @@
 use format_bytes::format_bytes;
 use hg::config::Config;
+use hg::errors::HgError;
 use hg::utils::files::get_bytes_from_os_string;
 use std::borrow::Cow;
 use std::env;
@@ -22,7 +23,17 @@
 
 /// The commandline user interface
 impl Ui {
-    pub fn new(_config: &Config) -> Self {
+    pub fn new(_config: &Config) -> Result<Self, HgError> {
+        Ok(Ui {
+            stdout: std::io::stdout(),
+            stderr: std::io::stderr(),
+        })
+    }
+
+    /// Default to no color if color configuration errors.
+    ///
+    /// Useful when we’re already handling another error.
+    pub fn new_infallible(_config: &Config) -> Self {
         Ui {
             stdout: std::io::stdout(),
             stderr: std::io::stderr(),
diff --git a/rust/rhg/src/main.rs b/rust/rhg/src/main.rs
--- a/rust/rhg/src/main.rs
+++ b/rust/rhg/src/main.rs
@@ -150,7 +150,7 @@
             .unwrap_or_else(|error| {
                 exit(
                     &None,
-                    &Ui::new(&Config::empty()),
+                    &Ui::new_infallible(&Config::empty()),
                     OnUnsupported::Abort,
                     Err(CommandError::abort(format!(
                         "abort: {}: '{}'",
@@ -171,7 +171,7 @@
 
             exit(
                 &initial_current_dir,
-                &Ui::new(&Config::empty()),
+                &Ui::new_infallible(&Config::empty()),
                 on_unsupported,
                 Err(error.into()),
                 false,
@@ -183,7 +183,7 @@
         .unwrap_or_else(|error| {
             exit(
                 &initial_current_dir,
-                &Ui::new(&non_repo_config),
+                &Ui::new_infallible(&non_repo_config),
                 OnUnsupported::from_config(&non_repo_config),
                 Err(error.into()),
                 non_repo_config
@@ -201,7 +201,7 @@
         if SCHEME_RE.is_match(&repo_path_bytes) {
             exit(
                 &initial_current_dir,
-                &Ui::new(&non_repo_config),
+                &Ui::new_infallible(&non_repo_config),
                 OnUnsupported::from_config(&non_repo_config),
                 Err(CommandError::UnsupportedFeature {
                     message: format_bytes!(
@@ -291,7 +291,7 @@
         }
         Err(error) => exit(
             &initial_current_dir,
-            &Ui::new(&non_repo_config),
+            &Ui::new_infallible(&non_repo_config),
             OnUnsupported::from_config(&non_repo_config),
             Err(error.into()),
             // TODO: show a warning or combine with original error if
@@ -307,7 +307,17 @@
     } else {
         &non_repo_config
     };
-    let ui = Ui::new(&config);
+    let ui = Ui::new(&config).unwrap_or_else(|error| {
+        exit(
+            &initial_current_dir,
+            &Ui::new_infallible(&config),
+            OnUnsupported::from_config(&config),
+            Err(error.into()),
+            config
+                .get_bool(b"ui", b"detailed-exit-code")
+                .unwrap_or(false),
+        )
+    });
     let on_unsupported = OnUnsupported::from_config(config);
 
     let result = main_with_result(



To: SimonSapin, #hg-reviewers
Cc: mercurial-patches
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurial-scm.org/pipermail/mercurial-patches/attachments/20220211/df3a896d/attachment-0002.html>


More information about the Mercurial-patches mailing list