requestty_ui/
error.rs

1use std::{fmt, io};
2
3/// The `requestty` result type.
4pub type Result<T> = std::result::Result<T, ErrorKind>;
5
6/// The errors that can occur in `requestty`.
7#[derive(Debug)]
8pub enum ErrorKind {
9    /// A regular [`std::io::Error`].
10    IoError(io::Error),
11    /// This occurs when `Ctrl+C` is received in [`Input`](crate::Input).
12    Interrupted,
13    /// This occurs when `Null` is received in [`Input`](crate::Input).
14    Eof,
15    /// The user aborted the question with `Esc`
16    Aborted,
17}
18
19impl std::error::Error for ErrorKind {
20    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
21        match self {
22            ErrorKind::IoError(e) => Some(e),
23            ErrorKind::Interrupted | ErrorKind::Eof | ErrorKind::Aborted => None,
24        }
25    }
26}
27
28impl fmt::Display for ErrorKind {
29    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
30        match self {
31            ErrorKind::IoError(e) => write!(fmt, "IoError: {}", e),
32            ErrorKind::Interrupted => write!(fmt, "CTRL+C"),
33            ErrorKind::Aborted => write!(fmt, "ESC"),
34            ErrorKind::Eof => write!(fmt, "EOF"),
35        }
36    }
37}
38
39impl From<io::Error> for ErrorKind {
40    fn from(e: io::Error) -> Self {
41        Self::IoError(e)
42    }
43}