summary refs log tree commit diff
path: root/src/error.rs
blob: 795f97372850ff9da8a0c60eb83c5ef4df5caf3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#![forbid(unsafe_code)]

pub type Result<T> = std::result::Result<T, TerminalError>;


#[derive(Clone,Debug,Eq,Hash,Ord,PartialEq,PartialOrd)]
pub enum TerminalError {
  Input(String),
  ModeSetting(String),
  Internal(String),
}

impl std::error::Error for TerminalError { }

impl std::fmt::Display for TerminalError {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      TerminalError::Input(s) =>
        f.write_fmt(format_args!(
            "Can't read terminal input: {}", s)),
      TerminalError::ModeSetting(s) =>
        f.write_fmt(format_args!(
            "Can't set terminal mode: {}", s)),
      TerminalError::Internal(s) =>
        f.write_fmt(format_args!(
            "Internal error regarding the terminal: {}", s)),
    }
  }
}


pub fn input(e: impl std::error::Error) -> TerminalError {
  TerminalError::ModeSetting(format!("{}", e))
}


pub fn mode_setting(e: impl std::error::Error) -> TerminalError {
  TerminalError::ModeSetting(format!("{}", e))
}


pub fn internal(e: impl std::error::Error) -> TerminalError {
  TerminalError::Internal(format!("{}", e))
}