Show More
@@ -1,42 +1,34 b'' | |||
|
1 | 1 | use crate::commands::Command; |
|
2 |
use crate::error:: |
|
|
2 | use crate::error::CommandError; | |
|
3 | 3 | use crate::ui::Ui; |
|
4 |
use hg::operations:: |
|
|
4 | use hg::operations::FindRoot; | |
|
5 | 5 | use hg::utils::files::get_bytes_from_path; |
|
6 | 6 | |
|
7 | 7 | pub const HELP_TEXT: &str = " |
|
8 | 8 | Print the root directory of the current repository. |
|
9 | 9 | |
|
10 | 10 | Returns 0 on success. |
|
11 | 11 | "; |
|
12 | 12 | |
|
13 | 13 | pub struct RootCommand<'a> { |
|
14 | 14 | ui: &'a Ui, |
|
15 | 15 | } |
|
16 | 16 | |
|
17 | 17 | impl<'a> RootCommand<'a> { |
|
18 | 18 | pub fn new(ui: &'a Ui) -> Self { |
|
19 | 19 | RootCommand { ui } |
|
20 | 20 | } |
|
21 | 21 | } |
|
22 | 22 | |
|
23 | 23 | impl<'a> Command<'a> for RootCommand<'a> { |
|
24 | 24 | fn run(&self) -> Result<(), CommandError> { |
|
25 | let path_buf = | |
|
26 | FindRoot::new().run().map_err(|err| match err.kind { | |
|
27 | FindRootErrorKind::RootNotFound(path) => { | |
|
28 | CommandErrorKind::RootNotFound(path) | |
|
29 | } | |
|
30 | FindRootErrorKind::GetCurrentDirError(e) => { | |
|
31 | CommandErrorKind::CurrentDirNotFound(e) | |
|
32 | } | |
|
33 | })?; | |
|
25 | let path_buf = FindRoot::new().run()?; | |
|
34 | 26 | |
|
35 | 27 | let bytes = get_bytes_from_path(path_buf); |
|
36 | 28 | |
|
37 | 29 | // TODO use formating macro |
|
38 | 30 | self.ui.write_stdout(&[bytes.as_slice(), b"\n"].concat())?; |
|
39 | 31 | |
|
40 | 32 | Ok(()) |
|
41 | 33 | } |
|
42 | 34 | } |
@@ -1,92 +1,106 b'' | |||
|
1 | 1 | use crate::exitcode; |
|
2 | 2 | use crate::ui::UiError; |
|
3 | use hg::operations::{FindRootError, FindRootErrorKind}; | |
|
3 | 4 | use hg::utils::files::get_bytes_from_path; |
|
4 | 5 | use std::convert::From; |
|
5 | 6 | use std::path::PathBuf; |
|
6 | 7 | |
|
7 | 8 | /// The kind of command error |
|
8 | 9 | #[derive(Debug)] |
|
9 | 10 | pub enum CommandErrorKind { |
|
10 | 11 | /// The root of the repository cannot be found |
|
11 | 12 | RootNotFound(PathBuf), |
|
12 | 13 | /// The current directory cannot be found |
|
13 | 14 | CurrentDirNotFound(std::io::Error), |
|
14 | 15 | /// The standard output stream cannot be written to |
|
15 | 16 | StdoutError, |
|
16 | 17 | /// The standard error stream cannot be written to |
|
17 | 18 | StderrError, |
|
18 | 19 | } |
|
19 | 20 | |
|
20 | 21 | impl CommandErrorKind { |
|
21 | 22 | pub fn get_exit_code(&self) -> exitcode::ExitCode { |
|
22 | 23 | match self { |
|
23 | 24 | CommandErrorKind::RootNotFound(_) => exitcode::ABORT, |
|
24 | 25 | CommandErrorKind::CurrentDirNotFound(_) => exitcode::ABORT, |
|
25 | 26 | CommandErrorKind::StdoutError => exitcode::ABORT, |
|
26 | 27 | CommandErrorKind::StderrError => exitcode::ABORT, |
|
27 | 28 | } |
|
28 | 29 | } |
|
29 | 30 | |
|
30 | 31 | /// Return the message corresponding to the error kind if any |
|
31 | 32 | pub fn get_error_message_bytes(&self) -> Option<Vec<u8>> { |
|
32 | 33 | match self { |
|
33 | 34 | // TODO use formating macro |
|
34 | 35 | CommandErrorKind::RootNotFound(path) => { |
|
35 | 36 | let bytes = get_bytes_from_path(path); |
|
36 | 37 | Some( |
|
37 | 38 | [ |
|
38 | 39 | b"abort: no repository found in '", |
|
39 | 40 | bytes.as_slice(), |
|
40 | 41 | b"' (.hg not found)!\n", |
|
41 | 42 | ] |
|
42 | 43 | .concat(), |
|
43 | 44 | ) |
|
44 | 45 | } |
|
45 | 46 | // TODO use formating macro |
|
46 | 47 | CommandErrorKind::CurrentDirNotFound(e) => Some( |
|
47 | 48 | [ |
|
48 | 49 | b"abort: error getting current working directory: ", |
|
49 | 50 | e.to_string().as_bytes(), |
|
50 | 51 | b"\n", |
|
51 | 52 | ] |
|
52 | 53 | .concat(), |
|
53 | 54 | ), |
|
54 | 55 | _ => None, |
|
55 | 56 | } |
|
56 | 57 | } |
|
57 | 58 | } |
|
58 | 59 | |
|
59 | 60 | /// The error type for the Command trait |
|
60 | 61 | #[derive(Debug)] |
|
61 | 62 | pub struct CommandError { |
|
62 | 63 | pub kind: CommandErrorKind, |
|
63 | 64 | } |
|
64 | 65 | |
|
65 | 66 | impl CommandError { |
|
66 | 67 | /// Exist the process with the corresponding exit code. |
|
67 | 68 | pub fn exit(&self) -> () { |
|
68 | 69 | std::process::exit(self.kind.get_exit_code()) |
|
69 | 70 | } |
|
70 | 71 | |
|
71 | 72 | /// Return the message corresponding to the command error if any |
|
72 | 73 | pub fn get_error_message_bytes(&self) -> Option<Vec<u8>> { |
|
73 | 74 | self.kind.get_error_message_bytes() |
|
74 | 75 | } |
|
75 | 76 | } |
|
76 | 77 | |
|
77 | 78 | impl From<CommandErrorKind> for CommandError { |
|
78 | 79 | fn from(kind: CommandErrorKind) -> Self { |
|
79 | 80 | CommandError { kind } |
|
80 | 81 | } |
|
81 | 82 | } |
|
82 | 83 | |
|
83 | 84 | impl From<UiError> for CommandError { |
|
84 | 85 | fn from(error: UiError) -> Self { |
|
85 | 86 | CommandError { |
|
86 | 87 | kind: match error { |
|
87 | 88 | UiError::StdoutError(_) => CommandErrorKind::StdoutError, |
|
88 | 89 | UiError::StderrError(_) => CommandErrorKind::StderrError, |
|
89 | 90 | }, |
|
90 | 91 | } |
|
91 | 92 | } |
|
92 | 93 | } |
|
94 | ||
|
95 | impl From<FindRootError> for CommandError { | |
|
96 | fn from(err: FindRootError) -> Self { | |
|
97 | match err.kind { | |
|
98 | FindRootErrorKind::RootNotFound(path) => CommandError { | |
|
99 | kind: CommandErrorKind::RootNotFound(path), | |
|
100 | }, | |
|
101 | FindRootErrorKind::GetCurrentDirError(e) => CommandError { | |
|
102 | kind: CommandErrorKind::CurrentDirNotFound(e), | |
|
103 | }, | |
|
104 | } | |
|
105 | } | |
|
106 | } |
General Comments 0
You need to be logged in to leave comments.
Login now