##// END OF EJS Templates
rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand...
Antoine Cezar -
r46099:66756b34 default
parent child Browse files
Show More
@@ -0,0 +1,80 b''
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 }
@@ -1,18 +1,21 b''
1 1 //! A distinction is made between operations and commands.
2 2 //! An operation is what can be done whereas a command is what is exposed by
3 3 //! the cli. A single command can use several operations to achieve its goal.
4 4
5 5 mod debugdata;
6 6 mod dirstate_status;
7 7 mod find_root;
8 8 mod list_tracked_files;
9 pub use debugdata::{
10 DebugData, DebugDataError, DebugDataErrorKind, DebugDataKind,
11 };
9 12 pub use find_root::{FindRoot, FindRootError, FindRootErrorKind};
10 13 pub use list_tracked_files::{
11 14 ListTrackedFiles, ListTrackedFilesError, ListTrackedFilesErrorKind,
12 15 };
13 16
14 17 // TODO add an `Operation` trait when GAT have landed (rust #44265):
15 18 // there is no way to currently define a trait which can both return
16 19 // references to `self` and to passed data, which is what we would need.
17 20 // Generic Associated Types may fix this and allow us to have a unified
18 21 // interface.
@@ -1,11 +1,12 b''
1 pub mod debugdata;
1 2 pub mod files;
2 3 pub mod root;
3 4 use crate::error::CommandError;
4 5 use crate::ui::Ui;
5 6
6 7 /// The common trait for rhg commands
7 8 ///
8 9 /// Normalize the interface of the commands provided by rhg
9 10 pub trait Command {
10 11 fn run(&self, ui: &Ui) -> Result<(), CommandError>;
11 12 }
@@ -1,105 +1,113 b''
1 use std::borrow::Cow;
1 2 use std::io;
2 3 use std::io::{ErrorKind, Write};
3 4
4 5 #[derive(Debug)]
5 6 pub struct Ui {
6 7 stdout: std::io::Stdout,
7 8 stderr: std::io::Stderr,
8 9 }
9 10
10 11 /// The kind of user interface error
11 12 pub enum UiError {
12 13 /// The standard output stream cannot be written to
13 14 StdoutError(io::Error),
14 15 /// The standard error stream cannot be written to
15 16 StderrError(io::Error),
16 17 }
17 18
18 19 /// The commandline user interface
19 20 impl Ui {
20 21 pub fn new() -> Self {
21 22 Ui {
22 23 stdout: std::io::stdout(),
23 24 stderr: std::io::stderr(),
24 25 }
25 26 }
26 27
27 28 /// Returns a buffered handle on stdout for faster batch printing
28 29 /// operations.
29 30 pub fn stdout_buffer(&self) -> StdoutBuffer<std::io::StdoutLock> {
30 31 StdoutBuffer::new(self.stdout.lock())
31 32 }
32 33
33 34 /// Write bytes to stdout
34 35 pub fn write_stdout(&self, bytes: &[u8]) -> Result<(), UiError> {
35 36 let mut stdout = self.stdout.lock();
36 37
37 38 stdout.write_all(bytes).or_else(handle_stdout_error)?;
38 39
39 40 stdout.flush().or_else(handle_stdout_error)
40 41 }
41 42
42 43 /// Write bytes to stderr
43 44 pub fn write_stderr(&self, bytes: &[u8]) -> Result<(), UiError> {
44 45 let mut stderr = self.stderr.lock();
45 46
46 47 stderr.write_all(bytes).or_else(handle_stderr_error)?;
47 48
48 49 stderr.flush().or_else(handle_stderr_error)
49 50 }
50 51
51 52 /// Write string line to stderr
52 53 pub fn writeln_stderr_str(&self, s: &str) -> Result<(), UiError> {
53 54 self.write_stderr(&format!("{}\n", s).as_bytes())
54 55 }
55 56 }
56 57
57 58 /// A buffered stdout writer for faster batch printing operations.
58 59 pub struct StdoutBuffer<W: Write> {
59 60 buf: io::BufWriter<W>,
60 61 }
61 62
62 63 impl<W: Write> StdoutBuffer<W> {
63 64 pub fn new(writer: W) -> Self {
64 65 let buf = io::BufWriter::new(writer);
65 66 Self { buf }
66 67 }
67 68
68 69 /// Write bytes to stdout buffer
69 70 pub fn write_all(&mut self, bytes: &[u8]) -> Result<(), UiError> {
70 71 self.buf.write_all(bytes).or_else(handle_stdout_error)
71 72 }
72 73
73 74 /// Flush bytes to stdout
74 75 pub fn flush(&mut self) -> Result<(), UiError> {
75 76 self.buf.flush().or_else(handle_stdout_error)
76 77 }
77 78 }
78 79
79 80 /// Sometimes writing to stdout is not possible, try writing to stderr to
80 81 /// signal that failure, otherwise just bail.
81 82 fn handle_stdout_error(error: io::Error) -> Result<(), UiError> {
82 83 if let ErrorKind::BrokenPipe = error.kind() {
83 84 // This makes `| head` work for example
84 85 return Ok(());
85 86 }
86 87 let mut stderr = io::stderr();
87 88
88 89 stderr
89 90 .write_all(&[b"abort: ", error.to_string().as_bytes(), b"\n"].concat())
90 91 .map_err(UiError::StderrError)?;
91 92
92 93 stderr.flush().map_err(UiError::StderrError)?;
93 94
94 95 Err(UiError::StdoutError(error))
95 96 }
96 97
97 98 /// Sometimes writing to stderr is not possible.
98 99 fn handle_stderr_error(error: io::Error) -> Result<(), UiError> {
99 100 // A broken pipe should not result in a error
100 101 // like with `| head` for example
101 102 if let ErrorKind::BrokenPipe = error.kind() {
102 103 return Ok(());
103 104 }
104 105 Err(UiError::StdoutError(error))
105 106 }
107
108 /// Encode rust strings according to the user system.
109 pub fn utf8_to_local(s: &str) -> Cow<[u8]> {
110 // TODO encode for the user's system //
111 let bytes = s.as_bytes();
112 Cow::Borrowed(bytes)
113 }
General Comments 0
You need to be logged in to leave comments. Login now