kiro_editor/
error.rs

1use std::fmt;
2use std::io;
3use std::time::SystemTimeError;
4
5// Deriving Debug is necessary to use .expect() method
6#[derive(Debug)]
7pub enum Error {
8    IoError(io::Error),
9    SystemTimeError(SystemTimeError),
10    TooSmallWindow(usize, usize),
11    UnknownWindowSize,
12    NotUtf8Input(Vec<u8>),
13    ControlCharInText(char),
14}
15
16impl fmt::Display for Error {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        use Error::*;
19        match self {
20            IoError(err) => write!(f, "{}", err),
21            SystemTimeError(err) => write!(f, "{}", err),
22            TooSmallWindow(w, h) => write!(
23                f,
24                "Screen {}x{} is too small. At least 1x3 is necessary in width x height",
25                w, h
26            ),
27            UnknownWindowSize => write!(f, "Could not detect terminal window size"),
28            NotUtf8Input(seq) => {
29                write!(f, "Cannot handle non-UTF8 multi-byte input sequence: ")?;
30                for byte in seq.iter() {
31                    write!(f, "\\x{:x}", byte)?;
32                }
33                Ok(())
34            }
35            ControlCharInText(c) => write!(f, "Invalid character for text is included: {:?}", c),
36        }
37    }
38}
39
40impl From<io::Error> for Error {
41    fn from(err: io::Error) -> Error {
42        Error::IoError(err)
43    }
44}
45
46impl From<SystemTimeError> for Error {
47    fn from(err: SystemTimeError) -> Error {
48        Error::SystemTimeError(err)
49    }
50}
51
52pub type Result<T> = std::result::Result<T, Error>;