Patchwork D12164: rhg: Pass a &Config to Ui::new

login
register
mail settings
Submitter phabricator
Date Feb. 10, 2022, 7:19 p.m.
Message ID <differential-rev-PHID-DREV-xkvk64bbfyhejnkx7ziv-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/50500/
State New
Headers show

Comments

phabricator - Feb. 10, 2022, 7:19 p.m.
SimonSapin created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  When a Ui object is needed to print errors about configuration-loading errors,
  an empty (default) configuration is used.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  rust/hg-core/src/config/config.rs
  rust/rhg/src/main.rs
  rust/rhg/src/ui.rs

CHANGE DETAILS




To: SimonSapin, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

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,4 +1,5 @@ 
 use format_bytes::format_bytes;
+use hg::config::Config;
 use hg::utils::files::get_bytes_from_os_string;
 use std::borrow::Cow;
 use std::env;
@@ -21,7 +22,7 @@ 
 
 /// The commandline user interface
 impl Ui {
-    pub fn new() -> Self {
+    pub fn new(_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
@@ -137,7 +137,6 @@ 
     let process_start_time = blackbox::ProcessStartTime::now();
 
     env_logger::init();
-    let ui = ui::Ui::new();
 
     let early_args = EarlyArgs::parse(std::env::args_os());
 
@@ -151,7 +150,7 @@ 
             .unwrap_or_else(|error| {
                 exit(
                     &None,
-                    &ui,
+                    &Ui::new(&Config::empty()),
                     OnUnsupported::Abort,
                     Err(CommandError::abort(format!(
                         "abort: {}: '{}'",
@@ -172,7 +171,7 @@ 
 
             exit(
                 &initial_current_dir,
-                &ui,
+                &Ui::new(&Config::empty()),
                 on_unsupported,
                 Err(error.into()),
                 false,
@@ -184,7 +183,7 @@ 
         .unwrap_or_else(|error| {
             exit(
                 &initial_current_dir,
-                &ui,
+                &Ui::new(&non_repo_config),
                 OnUnsupported::from_config(&non_repo_config),
                 Err(error.into()),
                 non_repo_config
@@ -202,7 +201,7 @@ 
         if SCHEME_RE.is_match(&repo_path_bytes) {
             exit(
                 &initial_current_dir,
-                &ui,
+                &Ui::new(&non_repo_config),
                 OnUnsupported::from_config(&non_repo_config),
                 Err(CommandError::UnsupportedFeature {
                     message: format_bytes!(
@@ -292,7 +291,7 @@ 
         }
         Err(error) => exit(
             &initial_current_dir,
-            &ui,
+            &Ui::new(&non_repo_config),
             OnUnsupported::from_config(&non_repo_config),
             Err(error.into()),
             // TODO: show a warning or combine with original error if
@@ -308,6 +307,7 @@ 
     } else {
         &non_repo_config
     };
+    let ui = Ui::new(&config);
     let on_unsupported = OnUnsupported::from_config(config);
 
     let result = main_with_result(
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
@@ -84,6 +84,11 @@ 
 }
 
 impl Config {
+    /// The configuration to use when printing configuration-loading errors
+    pub fn empty() -> Self {
+        Self { layers: Vec::new() }
+    }
+
     /// Load system and user configuration from various files.
     ///
     /// This is also affected by some environment variables.