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