Skip to main content

ez_tui/types/
error.rs

1use crate::{PortError, ViewError};
2use displaydoc::Display;
3use std::fmt::Debug;
4use std::io;
5
6/// Error type for the library
7#[derive(Debug, Display, Eq, PartialEq, Clone, PartialOrd)]
8pub enum Error {
9    /// [VIEW] {0}
10    View(ViewError),
11    /// [PORT] {0}
12    Port(PortError),
13    /// [FATAL] {0}
14    Fatal(String),
15    /// [IO] error while {0}: '{1}'
16    Io(String, IoContext),
17}
18
19/// Some context associated with any IO error that may occur.
20#[derive(Debug, Display, Eq, PartialEq, Clone, PartialOrd)]
21pub enum IoContext {
22    /// resizing terminal
23    TerminalResize,
24    /// initializing terminal
25    TerminalInit,
26    /// polling for terminal events
27    TerminalPoll,
28    /// gracefully exiting
29    GracefulExit,
30    /// drawing
31    Drawing,
32}
33impl IoContext {
34    /// Create a new [`Error::Io`] from another lib error
35    #[must_use]
36    pub fn wrap_lib(self, err: &Error) -> Error {
37        self.raw(err.to_string())
38    }
39    /// Create a new [`Error::Io`] from an actual io error
40    #[must_use]
41    pub fn wrap_io(self, err: &io::Error) -> Error {
42        self.raw(err.to_string())
43    }
44
45    /// Create a new [`Error::Io`] from a string
46    #[must_use]
47    pub fn raw(self, err: String) -> Error {
48        Error::Io(err, self.clone())
49    }
50}
51
52/// Dummy type to use if your application does not handle any specific errors.
53#[derive(Debug, Eq, PartialEq, Copy, Clone, PartialOrd)]
54pub struct NoClientError {}