rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand
authorAntoine Cezar <antoine.cezar@octobus.net>
Wed, 09 Sep 2020 12:07:05 +0200
changeset 45528 66756b34c06e
parent 45527 b56df13a0450
child 45529 f17caf8f3fef
rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand Differential Revision: https://phab.mercurial-scm.org/D8960
rust/hg-core/src/operations/mod.rs
rust/rhg/src/commands.rs
rust/rhg/src/commands/debugdata.rs
rust/rhg/src/ui.rs
--- a/rust/hg-core/src/operations/mod.rs	Thu Aug 13 16:22:15 2020 +0200
+++ b/rust/hg-core/src/operations/mod.rs	Wed Sep 09 12:07:05 2020 +0200
@@ -6,6 +6,9 @@
 mod dirstate_status;
 mod find_root;
 mod list_tracked_files;
+pub use debugdata::{
+    DebugData, DebugDataError, DebugDataErrorKind, DebugDataKind,
+};
 pub use find_root::{FindRoot, FindRootError, FindRootErrorKind};
 pub use list_tracked_files::{
     ListTrackedFiles, ListTrackedFilesError, ListTrackedFilesErrorKind,
--- a/rust/rhg/src/commands.rs	Thu Aug 13 16:22:15 2020 +0200
+++ b/rust/rhg/src/commands.rs	Wed Sep 09 12:07:05 2020 +0200
@@ -1,3 +1,4 @@
+pub mod debugdata;
 pub mod files;
 pub mod root;
 use crate::error::CommandError;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rust/rhg/src/commands/debugdata.rs	Wed Sep 09 12:07:05 2020 +0200
@@ -0,0 +1,80 @@
+use crate::commands::Command;
+use crate::error::{CommandError, CommandErrorKind};
+use crate::ui::utf8_to_local;
+use crate::ui::Ui;
+use hg::operations::{
+    DebugData, DebugDataError, DebugDataErrorKind, DebugDataKind,
+};
+
+pub const HELP_TEXT: &str = "
+Dump the contents of a data file revision
+";
+
+pub struct DebugDataCommand<'a> {
+    rev: &'a str,
+    kind: DebugDataKind,
+}
+
+impl<'a> DebugDataCommand<'a> {
+    pub fn new(rev: &'a str, kind: DebugDataKind) -> Self {
+        DebugDataCommand { rev, kind }
+    }
+}
+
+impl<'a> Command for DebugDataCommand<'a> {
+    fn run(&self, ui: &Ui) -> Result<(), CommandError> {
+        let mut operation = DebugData::new(self.rev, self.kind);
+        let data =
+            operation.run().map_err(|e| to_command_error(self.rev, e))?;
+
+        let mut stdout = ui.stdout_buffer();
+        stdout.write_all(&data)?;
+        stdout.flush()?;
+
+        Ok(())
+    }
+}
+
+/// Convert operation errors to command errors
+fn to_command_error(rev: &str, err: DebugDataError) -> CommandError {
+    match err.kind {
+        DebugDataErrorKind::FindRootError(err) => CommandError::from(err),
+        DebugDataErrorKind::IoError(err) => CommandError {
+            kind: CommandErrorKind::Abort(Some(
+                utf8_to_local(&format!("abort: {}\n", err)).into(),
+            )),
+        },
+        DebugDataErrorKind::InvalidRevision => CommandError {
+            kind: CommandErrorKind::Abort(Some(
+                utf8_to_local(&format!(
+                    "abort: invalid revision identifier{}\n",
+                    rev
+                ))
+                .into(),
+            )),
+        },
+        DebugDataErrorKind::UnsuportedRevlogVersion(version) => CommandError {
+            kind: CommandErrorKind::Abort(Some(
+                utf8_to_local(&format!(
+                    "abort: unsupported revlog version {}\n",
+                    version
+                ))
+                .into(),
+            )),
+        },
+        DebugDataErrorKind::CorruptedRevlog => CommandError {
+            kind: CommandErrorKind::Abort(Some(
+                "abort: corrupted revlog\n".into(),
+            )),
+        },
+        DebugDataErrorKind::UnknowRevlogDataFormat(format) => CommandError {
+            kind: CommandErrorKind::Abort(Some(
+                utf8_to_local(&format!(
+                    "abort: unknow revlog dataformat {:?}\n",
+                    format
+                ))
+                .into(),
+            )),
+        },
+    }
+}
--- a/rust/rhg/src/ui.rs	Thu Aug 13 16:22:15 2020 +0200
+++ b/rust/rhg/src/ui.rs	Wed Sep 09 12:07:05 2020 +0200
@@ -1,3 +1,4 @@
+use std::borrow::Cow;
 use std::io;
 use std::io::{ErrorKind, Write};
 
@@ -103,3 +104,10 @@
     }
     Err(UiError::StdoutError(error))
 }
+
+/// Encode rust strings according to the user system.
+pub fn utf8_to_local(s: &str) -> Cow<[u8]> {
+    // TODO encode for the user's system //
+    let bytes = s.as_bytes();
+    Cow::Borrowed(bytes)
+}