Mercurial > hg
diff rust/rhg/src/error.rs @ 45049:513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
The println macro is not used to avoid string usage.
Dealing only with bytes allows us to be compatible with any encoding
and not just UTF8.
Later on, format macro will be made to have more readable code.
Differential Revision: https://phab.mercurial-scm.org/D8612
author | Antoine Cezar <antoine.cezar@octobus.net> |
---|---|
date | Fri, 05 Jun 2020 09:01:35 +0200 |
parents | bacf6c7ef01b |
children | 227281e76c22 |
line wrap: on
line diff
--- a/rust/rhg/src/error.rs Mon Jul 06 22:02:50 2020 -0400 +++ b/rust/rhg/src/error.rs Fri Jun 05 09:01:35 2020 +0200 @@ -1,3 +1,60 @@ +use crate::exitcode; +use crate::ui::UiError; +use std::convert::From; + +/// The kind of command error +#[derive(Debug, PartialEq)] +pub enum CommandErrorKind { + /// The command finished without error + Ok, + /// The root of the repository cannot be found + RootNotFound, + /// The current directory cannot be found + CurrentDirNotFound, + /// The standard output stream cannot be written to + StdoutError, + /// The standard error stream cannot be written to + StderrError, +} + +impl CommandErrorKind { + pub fn get_exit_code(&self) -> exitcode::ExitCode { + match self { + CommandErrorKind::Ok => exitcode::OK, + CommandErrorKind::RootNotFound => exitcode::ABORT, + CommandErrorKind::CurrentDirNotFound => exitcode::ABORT, + CommandErrorKind::StdoutError => exitcode::ABORT, + CommandErrorKind::StderrError => exitcode::ABORT, + } + } +} + /// The error type for the Command trait #[derive(Debug, PartialEq)] -pub struct CommandError {} +pub struct CommandError { + pub kind: CommandErrorKind, +} + +impl CommandError { + /// Exist the process with the corresponding exit code. + pub fn exit(&self) -> () { + std::process::exit(self.kind.get_exit_code()) + } +} + +impl From<CommandErrorKind> for CommandError { + fn from(kind: CommandErrorKind) -> Self { + CommandError { kind } + } +} + +impl From<UiError> for CommandError { + fn from(error: UiError) -> Self { + CommandError { + kind: match error { + UiError::StdoutError(_) => CommandErrorKind::StdoutError, + UiError::StderrError(_) => CommandErrorKind::StderrError, + }, + } + } +}