From 38070766d15c44c3d731aa5b231e3b2ad698ac05 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 14 Dec 2021 18:11:04 -0500 Subject: simplify error handling a bunch --- src/error.rs | 83 ++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 33 deletions(-) (limited to 'src/error.rs') diff --git a/src/error.rs b/src/error.rs index 597d692..41ad4a2 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,52 +1,58 @@ -/// Error type for this crate +/// Source for errors in this crate #[derive(Debug)] -pub enum Error { - /// error making pty async - AsyncPty(std::io::Error), +pub enum Source { + /// error came from std::io::Error + Io(std::io::Error), + /// error came from nix::Error + Nix(nix::Error), +} - /// error making pty async - AsyncPtyNix(nix::Error), +impl std::fmt::Display for Source { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Io(e) => write!(f, "{}", e), + Self::Nix(e) => write!(f, "{}", e), + } + } +} - /// error creating pty - CreatePty(nix::Error), +impl std::convert::From for Source { + fn from(e: std::io::Error) -> Self { + Self::Io(e) + } +} - /// error opening pts at \ - OpenPts(std::io::Error, std::path::PathBuf), +impl std::convert::From for Source { + fn from(e: nix::Error) -> Self { + Self::Nix(e) + } +} - /// error setting terminal size - SetTermSize(nix::Error), +/// Error type for this crate +#[derive(Debug)] +pub enum Error { + /// error creating pty + CreatePty(Source), - /// error spawning subprocess - Spawn(std::io::Error), + /// error setting terminal size + SetTermSize(Source), /// error spawning subprocess - SpawnNix(nix::Error), + Spawn(Source), } impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Self::AsyncPty(e) => { - write!(f, "error making pty async: {}", e) - } - Self::AsyncPtyNix(e) => { - write!(f, "error making pty async: {}", e) - } Self::CreatePty(e) => { write!(f, "error creating pty: {}", e) } - Self::OpenPts(e, path) => { - write!(f, "error opening pts at {}: {}", path.display(), e) - } Self::SetTermSize(e) => { write!(f, "error setting terminal size: {}", e) } Self::Spawn(e) => { write!(f, "error spawning subprocess: {}", e) } - Self::SpawnNix(e) => { - write!(f, "error spawning subprocess: {}", e) - } } } } @@ -54,16 +60,27 @@ impl std::fmt::Display for Error { impl std::error::Error for Error { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { match self { - Self::AsyncPty(e) | Self::Spawn(e) | Self::OpenPts(e, _) => { - Some(e) - } - Self::AsyncPtyNix(e) | Self::SpawnNix(e) | Self::CreatePty(e) => { - Some(e) + Self::CreatePty(e) | Self::SetTermSize(e) | Self::Spawn(e) => { + match e { + Source::Io(e) => Some(e), + Source::Nix(e) => Some(e), + } } - Self::SetTermSize(e) => Some(e), } } } /// Convenience wrapper for `Result`s using [`Error`](Error) pub type Result = std::result::Result; + +pub fn create_pty>(source: S) -> Error { + Error::CreatePty(source.into()) +} + +pub fn set_term_size>(source: S) -> Error { + Error::SetTermSize(source.into()) +} + +pub fn spawn>(source: S) -> Error { + Error::Spawn(source.into()) +} -- cgit v1.2.3-54-g00ecf