changeset 50037:364e78389653

rust-ui: refactor ui code for printing narrow/sparse warnings This will be used elsewhere in the code, starting from the next commit.
author Raphaël Gomès <rgomes@octobus.net>
date Wed, 11 Jan 2023 16:42:29 +0100
parents 95ffa065204e
children 7faedeb24eb2
files rust/rhg/src/commands/status.rs rust/rhg/src/ui.rs
diffstat 2 files changed, 80 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/rust/rhg/src/commands/status.rs	Wed Jan 11 16:29:29 2023 +0100
+++ b/rust/rhg/src/commands/status.rs	Wed Jan 11 16:42:29 2023 +0100
@@ -6,7 +6,9 @@
 // GNU General Public License version 2 or any later version.
 
 use crate::error::CommandError;
-use crate::ui::Ui;
+use crate::ui::{
+    format_pattern_file_warning, print_narrow_sparse_warnings, Ui,
+};
 use crate::utils::path_utils::RelativizePaths;
 use clap::Arg;
 use format_bytes::format_bytes;
@@ -20,7 +22,6 @@
 use hg::matchers::{AlwaysMatcher, IntersectionMatcher};
 use hg::repo::Repo;
 use hg::utils::files::get_bytes_from_os_string;
-use hg::utils::files::get_bytes_from_path;
 use hg::utils::files::get_path_from_bytes;
 use hg::utils::hg_path::{hg_path_to_path_buf, HgPath};
 use hg::DirstateStatus;
@@ -269,7 +270,7 @@
     let after_status = |res: StatusResult| -> Result<_, CommandError> {
         let (mut ds_status, pattern_warnings) = res?;
         for warning in pattern_warnings {
-            ui.write_stderr(&print_pattern_file_warning(&warning, repo))?;
+            ui.write_stderr(&format_pattern_file_warning(&warning, repo))?;
         }
 
         for (path, error) in ds_status.bad {
@@ -385,31 +386,12 @@
         (false, false) => Box::new(AlwaysMatcher),
     };
 
-    for warning in narrow_warnings.into_iter().chain(sparse_warnings) {
-        match &warning {
-            sparse::SparseWarning::RootWarning { context, line } => {
-                let msg = format_bytes!(
-                    b"warning: {} profile cannot use paths \"
-                    starting with /, ignoring {}\n",
-                    context,
-                    line
-                );
-                ui.write_stderr(&msg)?;
-            }
-            sparse::SparseWarning::ProfileNotFound { profile, rev } => {
-                let msg = format_bytes!(
-                    b"warning: sparse profile '{}' not found \"
-                    in rev {} - ignoring it\n",
-                    profile,
-                    rev
-                );
-                ui.write_stderr(&msg)?;
-            }
-            sparse::SparseWarning::Pattern(e) => {
-                ui.write_stderr(&print_pattern_file_warning(e, repo))?;
-            }
-        }
-    }
+    print_narrow_sparse_warnings(
+        &narrow_warnings,
+        &sparse_warnings,
+        ui,
+        repo,
+    )?;
     let (fixup, mut dirstate_write_needed, filesystem_time_at_status_start) =
         dmap.with_status(
             matcher.as_ref(),
@@ -617,30 +599,3 @@
     };
     Ok(p1_contents != &*fs_contents)
 }
-
-fn print_pattern_file_warning(
-    warning: &PatternFileWarning,
-    repo: &Repo,
-) -> Vec<u8> {
-    match warning {
-        PatternFileWarning::InvalidSyntax(path, syntax) => format_bytes!(
-            b"{}: ignoring invalid syntax '{}'\n",
-            get_bytes_from_path(path),
-            &*syntax
-        ),
-        PatternFileWarning::NoSuchFile(path) => {
-            let path = if let Ok(relative) =
-                path.strip_prefix(repo.working_directory_path())
-            {
-                relative
-            } else {
-                &*path
-            };
-            format_bytes!(
-                b"skipping unreadable pattern file '{}': \
-                    No such file or directory\n",
-                get_bytes_from_path(path),
-            )
-        }
-    }
-}
--- a/rust/rhg/src/ui.rs	Wed Jan 11 16:29:29 2023 +0100
+++ b/rust/rhg/src/ui.rs	Wed Jan 11 16:42:29 2023 +0100
@@ -1,10 +1,15 @@
 use crate::color::ColorConfig;
 use crate::color::Effect;
+use crate::error::CommandError;
 use format_bytes::format_bytes;
 use format_bytes::write_bytes;
 use hg::config::Config;
 use hg::config::PlainInfo;
 use hg::errors::HgError;
+use hg::repo::Repo;
+use hg::sparse;
+use hg::utils::files::get_bytes_from_path;
+use hg::PatternFileWarning;
 use std::borrow::Cow;
 use std::io;
 use std::io::{ErrorKind, Write};
@@ -223,3 +228,68 @@
         atty::is(atty::Stream::Stdout)
     })
 }
+
+/// Return the formatted bytestring corresponding to a pattern file warning,
+/// as expected by the CLI.
+pub(crate) fn format_pattern_file_warning(
+    warning: &PatternFileWarning,
+    repo: &Repo,
+) -> Vec<u8> {
+    match warning {
+        PatternFileWarning::InvalidSyntax(path, syntax) => format_bytes!(
+            b"{}: ignoring invalid syntax '{}'\n",
+            get_bytes_from_path(path),
+            &*syntax
+        ),
+        PatternFileWarning::NoSuchFile(path) => {
+            let path = if let Ok(relative) =
+                path.strip_prefix(repo.working_directory_path())
+            {
+                relative
+            } else {
+                &*path
+            };
+            format_bytes!(
+                b"skipping unreadable pattern file '{}': \
+                    No such file or directory\n",
+                get_bytes_from_path(path),
+            )
+        }
+    }
+}
+
+/// Print with `Ui` the formatted bytestring corresponding to a
+/// sparse/narrow warning, as expected by the CLI.
+pub(crate) fn print_narrow_sparse_warnings(
+    narrow_warnings: &[sparse::SparseWarning],
+    sparse_warnings: &[sparse::SparseWarning],
+    ui: &Ui,
+    repo: &Repo,
+) -> Result<(), CommandError> {
+    for warning in narrow_warnings.iter().chain(sparse_warnings) {
+        match &warning {
+            sparse::SparseWarning::RootWarning { context, line } => {
+                let msg = format_bytes!(
+                    b"warning: {} profile cannot use paths \"
+                starting with /, ignoring {}\n",
+                    context,
+                    line
+                );
+                ui.write_stderr(&msg)?;
+            }
+            sparse::SparseWarning::ProfileNotFound { profile, rev } => {
+                let msg = format_bytes!(
+                    b"warning: sparse profile '{}' not found \"
+                in rev {} - ignoring it\n",
+                    profile,
+                    rev
+                );
+                ui.write_stderr(&msg)?;
+            }
+            sparse::SparseWarning::Pattern(e) => {
+                ui.write_stderr(&format_pattern_file_warning(e, repo))?;
+            }
+        }
+    }
+    Ok(())
+}