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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
use failure; use std::os::unix::io::RawFd; use std::{fmt, result}; use terminfo; pub type Result<T> = result::Result<T, Error>; #[derive(Debug)] pub struct Error { inner: failure::Context<ErrorKind>, } #[derive(Eq, PartialEq, Debug, Fail)] pub enum ErrorKind { #[fail(display = "failed to put the terminal in raw mode")] InitRawModeFailed, #[fail(display = "failed to take terminal out of raw mode")] ExitRawModeFailed, #[fail(display = "failed to get the next character")] GetCharFailed, #[fail(display = "failed to read the next keystroke")] ReadKeyFailed, #[fail(display = "Invalid number")] InvalidNumber, #[fail(display = "Invalid color")] InvalidColor, #[fail(display = "Failed to write OS Command escape code")] OscFailed, #[fail(display = "Failed to create terminal")] TermInitFailed, #[fail(display = "Failed to write Control Sequence")] CsiFailed, #[fail(display = "Expect fg:/bg: inside [+] block")] InvalidColorLocation, #[fail(display = "Expect fg/bg inside [-] block")] InvalidResetSpecifier, #[fail(display = "Unknown color \"{}\"", _0)] UnknownColorName(String), #[fail( display = "Failed to get the cursor position. The terminal did not return a valid escape sequence." )] InvalidCursorPosition, #[fail(display = "Failed to find the with of a tab character in this terminal")] FailedToGetTabWidth, #[fail(display = "Failed to read a line from standard in")] ReadLineFailed, #[fail(display = "Failed to write to standard out")] FailedWriteToStdout, #[fail(display = "Failed to align line right")] FailedToAlignRight, #[fail(display = "Failed to align line center")] FailedToAlignCenter, #[fail(display = "A terminfo field is missing!")] MissingTermInfoField(terminfo::StringField), #[fail(display = "Failed to execute a terminfo string")] FailedToRunTerminfo(terminfo::StringField), } impl Error { pub fn kind(&self) -> &ErrorKind { self.inner.get_context() } } impl failure::Fail for Error { fn cause(&self) -> Option<&failure::Fail> { self.inner.cause() } fn backtrace(&self) -> Option<&failure::Backtrace> { self.inner.backtrace() } } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.inner, f) } } impl From<ErrorKind> for Error { fn from(kind: ErrorKind) -> Error { Error { inner: failure::Context::new(kind), } } } impl From<failure::Context<ErrorKind>> for Error { fn from(inner: failure::Context<ErrorKind>) -> Error { Error { inner: inner } } }