comparison rust/rhg/src/commands/debugdata.rs @ 45528:66756b34c06e

rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand Differential Revision: https://phab.mercurial-scm.org/D8960
author Antoine Cezar <antoine.cezar@octobus.net>
date Wed, 09 Sep 2020 12:07:05 +0200
parents
children b1cea0dc9db0
comparison
equal deleted inserted replaced
45527:b56df13a0450 45528:66756b34c06e
1 use crate::commands::Command;
2 use crate::error::{CommandError, CommandErrorKind};
3 use crate::ui::utf8_to_local;
4 use crate::ui::Ui;
5 use hg::operations::{
6 DebugData, DebugDataError, DebugDataErrorKind, DebugDataKind,
7 };
8
9 pub const HELP_TEXT: &str = "
10 Dump the contents of a data file revision
11 ";
12
13 pub struct DebugDataCommand<'a> {
14 rev: &'a str,
15 kind: DebugDataKind,
16 }
17
18 impl<'a> DebugDataCommand<'a> {
19 pub fn new(rev: &'a str, kind: DebugDataKind) -> Self {
20 DebugDataCommand { rev, kind }
21 }
22 }
23
24 impl<'a> Command for DebugDataCommand<'a> {
25 fn run(&self, ui: &Ui) -> Result<(), CommandError> {
26 let mut operation = DebugData::new(self.rev, self.kind);
27 let data =
28 operation.run().map_err(|e| to_command_error(self.rev, e))?;
29
30 let mut stdout = ui.stdout_buffer();
31 stdout.write_all(&data)?;
32 stdout.flush()?;
33
34 Ok(())
35 }
36 }
37
38 /// Convert operation errors to command errors
39 fn to_command_error(rev: &str, err: DebugDataError) -> CommandError {
40 match err.kind {
41 DebugDataErrorKind::FindRootError(err) => CommandError::from(err),
42 DebugDataErrorKind::IoError(err) => CommandError {
43 kind: CommandErrorKind::Abort(Some(
44 utf8_to_local(&format!("abort: {}\n", err)).into(),
45 )),
46 },
47 DebugDataErrorKind::InvalidRevision => CommandError {
48 kind: CommandErrorKind::Abort(Some(
49 utf8_to_local(&format!(
50 "abort: invalid revision identifier{}\n",
51 rev
52 ))
53 .into(),
54 )),
55 },
56 DebugDataErrorKind::UnsuportedRevlogVersion(version) => CommandError {
57 kind: CommandErrorKind::Abort(Some(
58 utf8_to_local(&format!(
59 "abort: unsupported revlog version {}\n",
60 version
61 ))
62 .into(),
63 )),
64 },
65 DebugDataErrorKind::CorruptedRevlog => CommandError {
66 kind: CommandErrorKind::Abort(Some(
67 "abort: corrupted revlog\n".into(),
68 )),
69 },
70 DebugDataErrorKind::UnknowRevlogDataFormat(format) => CommandError {
71 kind: CommandErrorKind::Abort(Some(
72 utf8_to_local(&format!(
73 "abort: unknow revlog dataformat {:?}\n",
74 format
75 ))
76 .into(),
77 )),
78 },
79 }
80 }