Mercurial > public > mercurial-scm > hg
comparison rust/rhg/src/commands/root.rs @ 45361:47997afadf08
rhg: ask the error message from `CommandError`
Avoid repeating the display of the same error messages in different commands.
Differential Revision: https://phab.mercurial-scm.org/D8865
author | Antoine Cezar <antoine.cezar@octobus.net> |
---|---|
date | Mon, 20 Jul 2020 18:14:52 +0200 |
parents | 227281e76c22 |
children | 5dbf875b3275 |
comparison
equal
deleted
inserted
replaced
45360:227281e76c22 | 45361:47997afadf08 |
---|---|
1 use crate::commands::Command; | 1 use crate::commands::Command; |
2 use crate::error::{CommandError, CommandErrorKind}; | 2 use crate::error::{CommandError, CommandErrorKind}; |
3 use crate::ui::Ui; | 3 use crate::ui::Ui; |
4 use hg::operations::{FindRoot, FindRootError, FindRootErrorKind}; | 4 use hg::operations::{FindRoot, FindRootErrorKind}; |
5 use hg::utils::files::get_bytes_from_path; | 5 use hg::utils::files::get_bytes_from_path; |
6 use std::path::PathBuf; | |
7 | 6 |
8 pub const HELP_TEXT: &str = " | 7 pub const HELP_TEXT: &str = " |
9 Print the root directory of the current repository. | 8 Print the root directory of the current repository. |
10 | 9 |
11 Returns 0 on success. | 10 Returns 0 on success. |
12 "; | 11 "; |
13 | 12 |
14 pub struct RootCommand { | 13 pub struct RootCommand<'a> { |
15 ui: Ui, | 14 ui: &'a Ui, |
16 } | 15 } |
17 | 16 |
18 impl RootCommand { | 17 impl<'a> RootCommand<'a> { |
19 pub fn new() -> Self { | 18 pub fn new(ui: &'a Ui) -> Self { |
20 RootCommand { ui: Ui::new() } | 19 RootCommand { ui } |
21 } | 20 } |
21 } | |
22 | 22 |
23 fn display_found_path( | 23 impl<'a> Command<'a> for RootCommand<'a> { |
24 &self, | 24 fn run(&self) -> Result<(), CommandError> { |
25 path_buf: PathBuf, | 25 let path_buf = |
26 ) -> Result<(), CommandError> { | 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 | |
27 let bytes = get_bytes_from_path(path_buf); | 35 let bytes = get_bytes_from_path(path_buf); |
28 | 36 |
29 // TODO use formating macro | 37 // TODO use formating macro |
30 self.ui.write_stdout(&[bytes.as_slice(), b"\n"].concat())?; | 38 self.ui.write_stdout(&[bytes.as_slice(), b"\n"].concat())?; |
31 | 39 |
32 Ok(()) | 40 Ok(()) |
33 } | 41 } |
34 | |
35 fn display_error(&self, error: FindRootError) -> Result<(), CommandError> { | |
36 match error.kind { | |
37 FindRootErrorKind::RootNotFound(path) => { | |
38 let bytes = get_bytes_from_path(path); | |
39 | |
40 // TODO use formating macro | |
41 self.ui.write_stderr( | |
42 &[ | |
43 b"abort: no repository found in '", | |
44 bytes.as_slice(), | |
45 b"' (.hg not found)!\n", | |
46 ] | |
47 .concat(), | |
48 )?; | |
49 | |
50 Err(CommandErrorKind::RootNotFound.into()) | |
51 } | |
52 FindRootErrorKind::GetCurrentDirError(e) => { | |
53 // TODO use formating macro | |
54 self.ui.write_stderr( | |
55 &[ | |
56 b"abort: error getting current working directory: ", | |
57 e.to_string().as_bytes(), | |
58 b"\n", | |
59 ] | |
60 .concat(), | |
61 )?; | |
62 | |
63 Err(CommandErrorKind::CurrentDirNotFound.into()) | |
64 } | |
65 } | |
66 } | |
67 } | 42 } |
68 | |
69 impl Command for RootCommand { | |
70 fn run(&self) -> Result<(), CommandError> { | |
71 match FindRoot::new().run() { | |
72 Ok(path_buf) => self.display_found_path(path_buf), | |
73 Err(e) => self.display_error(e), | |
74 } | |
75 } | |
76 } |