Skip to main content

prek_pty/
error.rs

1/// Error type for errors from this crate
2#[derive(Debug)]
3pub enum Error {
4    /// error came from `std::io::Error`
5    Io(std::io::Error),
6    /// error came from `nix::Error`
7    Rustix(rustix::io::Errno),
8    /// unsplit was called on halves of two different ptys
9    Unsplit(crate::pty::OwnedReadPty, crate::pty::OwnedWritePty),
10}
11
12impl std::fmt::Display for Error {
13    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14        match self {
15            Self::Io(e) => write!(f, "{e}"),
16            Self::Rustix(e) => write!(f, "{e}"),
17            Self::Unsplit(..) => {
18                write!(f, "unsplit called on halves of two different ptys")
19            }
20        }
21    }
22}
23
24impl From<std::io::Error> for Error {
25    fn from(e: std::io::Error) -> Self {
26        Self::Io(e)
27    }
28}
29
30impl From<rustix::io::Errno> for Error {
31    fn from(e: rustix::io::Errno) -> Self {
32        Self::Rustix(e)
33    }
34}
35
36impl std::error::Error for Error {
37    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
38        match self {
39            Self::Io(e) => Some(e),
40            Self::Rustix(e) => Some(e),
41            Self::Unsplit(..) => None,
42        }
43    }
44}
45
46/// Convenience wrapper for `Result`s using [`Error`](Error)
47pub type Result<T> = std::result::Result<T, Error>;