rust/rhg/src/error.rs
changeset 45049 513b3ef277a3
parent 44982 bacf6c7ef01b
child 45360 227281e76c22
equal deleted inserted replaced
45046:dd3050227a84 45049:513b3ef277a3
       
     1 use crate::exitcode;
       
     2 use crate::ui::UiError;
       
     3 use std::convert::From;
       
     4 
       
     5 /// The kind of command error
       
     6 #[derive(Debug, PartialEq)]
       
     7 pub enum CommandErrorKind {
       
     8     /// The command finished without error
       
     9     Ok,
       
    10     /// The root of the repository cannot be found
       
    11     RootNotFound,
       
    12     /// The current directory cannot be found
       
    13     CurrentDirNotFound,
       
    14     /// The standard output stream cannot be written to
       
    15     StdoutError,
       
    16     /// The standard error stream cannot be written to
       
    17     StderrError,
       
    18 }
       
    19 
       
    20 impl CommandErrorKind {
       
    21     pub fn get_exit_code(&self) -> exitcode::ExitCode {
       
    22         match self {
       
    23             CommandErrorKind::Ok => exitcode::OK,
       
    24             CommandErrorKind::RootNotFound => exitcode::ABORT,
       
    25             CommandErrorKind::CurrentDirNotFound => exitcode::ABORT,
       
    26             CommandErrorKind::StdoutError => exitcode::ABORT,
       
    27             CommandErrorKind::StderrError => exitcode::ABORT,
       
    28         }
       
    29     }
       
    30 }
       
    31 
     1 /// The error type for the Command trait
    32 /// The error type for the Command trait
     2 #[derive(Debug, PartialEq)]
    33 #[derive(Debug, PartialEq)]
     3 pub struct CommandError {}
    34 pub struct CommandError {
       
    35     pub kind: CommandErrorKind,
       
    36 }
       
    37 
       
    38 impl CommandError {
       
    39     /// Exist the process with the corresponding exit code.
       
    40     pub fn exit(&self) -> () {
       
    41         std::process::exit(self.kind.get_exit_code())
       
    42     }
       
    43 }
       
    44 
       
    45 impl From<CommandErrorKind> for CommandError {
       
    46     fn from(kind: CommandErrorKind) -> Self {
       
    47         CommandError { kind }
       
    48     }
       
    49 }
       
    50 
       
    51 impl From<UiError> for CommandError {
       
    52     fn from(error: UiError) -> Self {
       
    53         CommandError {
       
    54             kind: match error {
       
    55                 UiError::StdoutError(_) => CommandErrorKind::StdoutError,
       
    56                 UiError::StderrError(_) => CommandErrorKind::StderrError,
       
    57             },
       
    58         }
       
    59     }
       
    60 }