Patchwork D8954: rhg: pass ui to Command run

login
register
mail settings
Submitter phabricator
Date Aug. 26, 2020, 1:24 p.m.
Message ID <differential-rev-PHID-DREV-7tzsw5r6e3o7zfy3bwbg-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/47056/
State Superseded
Headers show

Comments

phabricator - Aug. 26, 2020, 1:24 p.m.
acezar created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  Allow implementation of `From<clap::ArgMatches> for Command`

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  rust/rhg/src/commands.rs
  rust/rhg/src/commands/files.rs
  rust/rhg/src/commands/root.rs
  rust/rhg/src/main.rs

CHANGE DETAILS




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

Patch

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
@@ -29,8 +29,8 @@ 
 
     let command_result = match matches.subcommand_name() {
         Some(name) => match name {
-            "root" => commands::root::RootCommand::new(&ui).run(),
-            "files" => commands::files::FilesCommand::new(&ui).run(),
+            "root" => commands::root::RootCommand::new().run(&ui),
+            "files" => commands::files::FilesCommand::new().run(&ui),
             _ => std::process::exit(exitcode::UNIMPLEMENTED_COMMAND),
         },
         _ => {
diff --git a/rust/rhg/src/commands/root.rs b/rust/rhg/src/commands/root.rs
--- a/rust/rhg/src/commands/root.rs
+++ b/rust/rhg/src/commands/root.rs
@@ -10,24 +10,22 @@ 
 Returns 0 on success.
 ";
 
-pub struct RootCommand<'a> {
-    ui: &'a Ui,
-}
+pub struct RootCommand {}
 
-impl<'a> RootCommand<'a> {
-    pub fn new(ui: &'a Ui) -> Self {
-        RootCommand { ui }
+impl RootCommand {
+    pub fn new() -> Self {
+        RootCommand {}
     }
 }
 
-impl<'a> Command<'a> for RootCommand<'a> {
-    fn run(&self) -> Result<(), CommandError> {
+impl Command for RootCommand {
+    fn run(&self, ui: &Ui) -> Result<(), CommandError> {
         let path_buf = FindRoot::new().run()?;
 
         let bytes = get_bytes_from_path(path_buf);
 
         // TODO use formating macro
-        self.ui.write_stdout(&[bytes.as_slice(), b"\n"].concat())?;
+        ui.write_stdout(&[bytes.as_slice(), b"\n"].concat())?;
 
         Ok(())
     }
diff --git a/rust/rhg/src/commands/files.rs b/rust/rhg/src/commands/files.rs
--- a/rust/rhg/src/commands/files.rs
+++ b/rust/rhg/src/commands/files.rs
@@ -11,18 +11,16 @@ 
 Returns 0 on success.
 ";
 
-pub struct FilesCommand<'a> {
-    ui: &'a Ui,
-}
+pub struct FilesCommand {}
 
-impl<'a> FilesCommand<'a> {
-    pub fn new(ui: &'a Ui) -> Self {
-        FilesCommand { ui }
+impl FilesCommand {
+    pub fn new() -> Self {
+        FilesCommand {}
     }
 }
 
-impl<'a> Command<'a> for FilesCommand<'a> {
-    fn run(&self) -> Result<(), CommandError> {
+impl Command for FilesCommand {
+    fn run(&self, ui: &Ui) -> Result<(), CommandError> {
         let operation_builder = ListTrackedFiles::new()?;
         let operation = operation_builder.load().map_err(|err| {
             CommandErrorKind::Abort(Some(
@@ -47,7 +45,7 @@ 
             .expect("cwd was already checked within the repository");
         let rooted_cwd = HgPathBuf::from(get_bytes_from_path(rooted_cwd));
 
-        let mut stdout = self.ui.stdout_buffer();
+        let mut stdout = ui.stdout_buffer();
 
         for file in files {
             stdout.write_all(relativize_path(file, &rooted_cwd).as_ref())?;
diff --git a/rust/rhg/src/commands.rs b/rust/rhg/src/commands.rs
--- a/rust/rhg/src/commands.rs
+++ b/rust/rhg/src/commands.rs
@@ -1,10 +1,11 @@ 
 pub mod files;
 pub mod root;
 use crate::error::CommandError;
+use crate::ui::Ui;
 
 /// The common trait for rhg commands
 ///
 /// Normalize the interface of the commands provided by rhg
-pub trait Command<'a> {
-    fn run(&self) -> Result<(), CommandError>;
+pub trait Command {
+    fn run(&self, ui: &Ui) -> Result<(), CommandError>;
 }