# HG changeset patch # User Antoine Cezar # Date 2020-07-29 08:21:17 # Node ID 5fe25f8ef5d9b7f62292981c6add81b70f1ead5d # Parent 5dbf875b32751ea383b7ec8a14449efdd05bb420 rhg: add a `Files` `Command` to prepare the `rhg files` subcommand Differential Revision: https://phab.mercurial-scm.org/D8868 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 @@ -57,6 +57,7 @@ pub type FastHashMap = HashMap { + ui: &'a Ui, +} + +impl<'a> FilesCommand<'a> { + pub fn new(ui: &'a Ui) -> Self { + FilesCommand { ui } + } +} + +impl<'a> Command<'a> for FilesCommand<'a> { + fn run(&self) -> Result<(), CommandError> { + let operation_builder = ListTrackedFiles::new()?; + let operation = operation_builder.load().map_err(|err| { + CommandErrorKind::Abort(Some( + [b"abort: ", err.to_string().as_bytes(), b"\n"] + .concat() + .to_vec(), + )) + })?; + let files = operation.run().map_err(|err| match err.kind { + ListTrackedFilesErrorKind::ParseError(_) => { + CommandErrorKind::Abort(Some( + // TODO find a better error message + b"abort: parse error\n".to_vec(), + )) + } + })?; + + let mut stdout = self.ui.stdout_buffer(); + for file in files { + stdout.write_all(file.as_bytes())?; + stdout.write_all(b"\n")?; + } + stdout.flush()?; + + Ok(()) + } +} 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 @@ -16,6 +16,8 @@ pub enum CommandErrorKind { StdoutError, /// The standard error stream cannot be written to StderrError, + /// The command aborted + Abort(Option>), } impl CommandErrorKind { @@ -25,6 +27,7 @@ impl CommandErrorKind { CommandErrorKind::CurrentDirNotFound(_) => exitcode::ABORT, CommandErrorKind::StdoutError => exitcode::ABORT, CommandErrorKind::StderrError => exitcode::ABORT, + CommandErrorKind::Abort(_) => exitcode::ABORT, } } @@ -52,6 +55,7 @@ impl CommandErrorKind { ] .concat(), ), + CommandErrorKind::Abort(message) => message.to_owned(), _ => None, } }