diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2019-01-08 04:13:23 +0000 |
---|---|---|
committer | Robin Krahl <robin.krahl@ireas.org> | 2019-01-08 05:13:45 +0100 |
commit | 4ffee6bacc3579c57687f4029d2fc73213592c39 (patch) | |
tree | 1daf3804dd42d70013a39e6ba62548547726fb38 /src/lib.rs | |
parent | fce7d2b9bf30e09b615dbd77b4dd7db94d623bfb (diff) | |
download | dialog-rs-4ffee6bacc3579c57687f4029d2fc73213592c39.tar.gz dialog-rs-4ffee6bacc3579c57687f4029d2fc73213592c39.tar.bz2 |
Refactor io::Error into custom Error enum
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 55 |
1 files changed, 54 insertions, 1 deletions
@@ -79,7 +79,60 @@ /// [`Backend`]: trait.Backend.html pub mod backends; -use std::io::Result; +use std::io; +use std::process; +use std::result; +use std::str; +use std::string; + +/// A result returned by `dialog`. +pub type Result<T> = result::Result<T, Error>; + +/// An error returned by `dialog`. +#[derive(Debug)] +pub enum Error { + /// A general error with an error message. + Error(String), + /// An input or output error. + IoError(io::Error), + /// An UTF-8 error. + Utf8Error(str::Utf8Error), +} + +impl From<&str> for Error { + fn from(string: &str) -> Error { + Error::Error(string.to_string()) + } +} + +impl From<io::Error> for Error { + fn from(error: io::Error) -> Error { + Error::IoError(error) + } +} + +impl From<str::Utf8Error> for Error { + fn from(error: str::Utf8Error) -> Error { + Error::Utf8Error(error) + } +} + +impl From<string::FromUtf8Error> for Error { + fn from(error: string::FromUtf8Error) -> Error { + Error::Utf8Error(error.utf8_error()) + } +} + +impl From<(&str, process::ExitStatus)> for Error { + fn from(data: (&str, process::ExitStatus)) -> Error { + let (command, status) = data; + let msg = match status.code() { + Some(code) => format!("Command {} failed with exit status {}", command, code), + None => format!("Command {} was terminated by a signal", command), + }; + Error::Error(msg) + } +} /// A dialog box that can be shown using a backend. /// |