author | Simon Sapin <simon.sapin@octobus.net> |
Tue, 26 Jan 2021 19:07:24 +0100 | |
changeset 46434 | 3e2d539d0d1a |
parent 45984 | fada33872b5b |
child 46435 | 2e2033081274 |
permissions | -rw-r--r-- |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
1 |
use crate::exitcode; |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
2 |
use crate::ui::UiError; |
45984
fada33872b5b
rhg: use `format_bytes!` for error messages
Raphaël Gomès <rgomes@octobus.net>
parents:
45937
diff
changeset
|
3 |
use format_bytes::format_bytes; |
46434
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
4 |
use hg::operations::FindRootError; |
45924
a2eda1ff22aa
requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
45923
diff
changeset
|
5 |
use hg::requirements::RequirementsError; |
45361
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45360
diff
changeset
|
6 |
use hg::utils::files::get_bytes_from_path; |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
7 |
use std::convert::From; |
45361
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45360
diff
changeset
|
8 |
use std::path::PathBuf; |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
9 |
|
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
10 |
/// The kind of command error |
45361
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45360
diff
changeset
|
11 |
#[derive(Debug)] |
46434
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
12 |
pub enum CommandError { |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
13 |
/// The root of the repository cannot be found |
45361
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45360
diff
changeset
|
14 |
RootNotFound(PathBuf), |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
15 |
/// The current directory cannot be found |
45361
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45360
diff
changeset
|
16 |
CurrentDirNotFound(std::io::Error), |
45924
a2eda1ff22aa
requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
45923
diff
changeset
|
17 |
/// `.hg/requires` |
a2eda1ff22aa
requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
45923
diff
changeset
|
18 |
RequirementsError(RequirementsError), |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
19 |
/// The standard output stream cannot be written to |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
20 |
StdoutError, |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
21 |
/// The standard error stream cannot be written to |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
22 |
StderrError, |
45364
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45363
diff
changeset
|
23 |
/// The command aborted |
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45363
diff
changeset
|
24 |
Abort(Option<Vec<u8>>), |
45542
33ded2d3f4fc
rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45439
diff
changeset
|
25 |
/// A mercurial capability as not been implemented. |
33ded2d3f4fc
rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45439
diff
changeset
|
26 |
Unimplemented, |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
27 |
} |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
28 |
|
46434
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
29 |
impl CommandError { |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
30 |
pub fn get_exit_code(&self) -> exitcode::ExitCode { |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
31 |
match self { |
46434
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
32 |
CommandError::RootNotFound(_) => exitcode::ABORT, |
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
33 |
CommandError::CurrentDirNotFound(_) => exitcode::ABORT, |
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
34 |
CommandError::RequirementsError( |
45937
2ad2745e0be9
rhg: exit with relevant code for unsupported requirements
Simon Sapin <simon-commits@exyr.org>
parents:
45924
diff
changeset
|
35 |
RequirementsError::Unsupported { .. }, |
2ad2745e0be9
rhg: exit with relevant code for unsupported requirements
Simon Sapin <simon-commits@exyr.org>
parents:
45924
diff
changeset
|
36 |
) => exitcode::UNIMPLEMENTED_COMMAND, |
46434
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
37 |
CommandError::RequirementsError(_) => exitcode::ABORT, |
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
38 |
CommandError::StdoutError => exitcode::ABORT, |
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
39 |
CommandError::StderrError => exitcode::ABORT, |
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
40 |
CommandError::Abort(_) => exitcode::ABORT, |
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
41 |
CommandError::Unimplemented => exitcode::UNIMPLEMENTED_COMMAND, |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
42 |
} |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
43 |
} |
45361
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45360
diff
changeset
|
44 |
|
46434
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
45 |
/// Return the message corresponding to the error if any |
45361
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45360
diff
changeset
|
46 |
pub fn get_error_message_bytes(&self) -> Option<Vec<u8>> { |
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45360
diff
changeset
|
47 |
match self { |
46434
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
48 |
CommandError::RootNotFound(path) => { |
45361
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45360
diff
changeset
|
49 |
let bytes = get_bytes_from_path(path); |
45984
fada33872b5b
rhg: use `format_bytes!` for error messages
Raphaël Gomès <rgomes@octobus.net>
parents:
45937
diff
changeset
|
50 |
Some(format_bytes!( |
fada33872b5b
rhg: use `format_bytes!` for error messages
Raphaël Gomès <rgomes@octobus.net>
parents:
45937
diff
changeset
|
51 |
b"abort: no repository found in '{}' (.hg not found)!\n", |
fada33872b5b
rhg: use `format_bytes!` for error messages
Raphaël Gomès <rgomes@octobus.net>
parents:
45937
diff
changeset
|
52 |
bytes.as_slice() |
fada33872b5b
rhg: use `format_bytes!` for error messages
Raphaël Gomès <rgomes@octobus.net>
parents:
45937
diff
changeset
|
53 |
)) |
45361
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45360
diff
changeset
|
54 |
} |
46434
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
55 |
CommandError::CurrentDirNotFound(e) => Some(format_bytes!( |
45984
fada33872b5b
rhg: use `format_bytes!` for error messages
Raphaël Gomès <rgomes@octobus.net>
parents:
45937
diff
changeset
|
56 |
b"abort: error getting current working directory: {}\n", |
fada33872b5b
rhg: use `format_bytes!` for error messages
Raphaël Gomès <rgomes@octobus.net>
parents:
45937
diff
changeset
|
57 |
e.to_string().as_bytes(), |
fada33872b5b
rhg: use `format_bytes!` for error messages
Raphaël Gomès <rgomes@octobus.net>
parents:
45937
diff
changeset
|
58 |
)), |
46434
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
59 |
CommandError::RequirementsError(RequirementsError::Corrupted) => { |
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
60 |
Some( |
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
61 |
"abort: .hg/requires is corrupted\n".as_bytes().to_owned(), |
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
62 |
) |
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
63 |
} |
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
64 |
CommandError::Abort(message) => message.to_owned(), |
45361
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45360
diff
changeset
|
65 |
_ => None, |
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45360
diff
changeset
|
66 |
} |
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45360
diff
changeset
|
67 |
} |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
68 |
|
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
69 |
/// Exist the process with the corresponding exit code. |
45439
fbc373b7cbc3
rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45364
diff
changeset
|
70 |
pub fn exit(&self) { |
46434
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
71 |
std::process::exit(self.get_exit_code()) |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
72 |
} |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
73 |
} |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
74 |
|
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
75 |
impl From<UiError> for CommandError { |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
76 |
fn from(error: UiError) -> Self { |
46434
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
77 |
match error { |
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
78 |
UiError::StdoutError(_) => CommandError::StdoutError, |
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
79 |
UiError::StderrError(_) => CommandError::StderrError, |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
80 |
} |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
81 |
} |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
82 |
} |
45363
5dbf875b3275
rhg: simplify `FindRootError` handling
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45361
diff
changeset
|
83 |
|
5dbf875b3275
rhg: simplify `FindRootError` handling
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45361
diff
changeset
|
84 |
impl From<FindRootError> for CommandError { |
5dbf875b3275
rhg: simplify `FindRootError` handling
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45361
diff
changeset
|
85 |
fn from(err: FindRootError) -> Self { |
46434
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
86 |
match err { |
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
87 |
FindRootError::RootNotFound(path) => { |
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
88 |
CommandError::RootNotFound(path) |
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
89 |
} |
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
90 |
FindRootError::GetCurrentDirError(e) => { |
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
91 |
CommandError::CurrentDirNotFound(e) |
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
92 |
} |
45363
5dbf875b3275
rhg: simplify `FindRootError` handling
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45361
diff
changeset
|
93 |
} |
5dbf875b3275
rhg: simplify `FindRootError` handling
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45361
diff
changeset
|
94 |
} |
5dbf875b3275
rhg: simplify `FindRootError` handling
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45361
diff
changeset
|
95 |
} |
45924
a2eda1ff22aa
requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
45923
diff
changeset
|
96 |
|
a2eda1ff22aa
requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
45923
diff
changeset
|
97 |
impl From<RequirementsError> for CommandError { |
a2eda1ff22aa
requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
45923
diff
changeset
|
98 |
fn from(err: RequirementsError) -> Self { |
46434
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
45984
diff
changeset
|
99 |
CommandError::RequirementsError(err) |
45924
a2eda1ff22aa
requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
45923
diff
changeset
|
100 |
} |
a2eda1ff22aa
requirements: move loading to hg-core and add parsing
Simon Sapin <simon-commits@exyr.org>
parents:
45923
diff
changeset
|
101 |
} |