diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2019-01-10 23:43:15 +0000 |
---|---|---|
committer | Robin Krahl <robin.krahl@ireas.org> | 2019-01-11 00:49:22 +0100 |
commit | 3e7464c0f8cbadb8c6335c53579ec9570962dc3f (patch) | |
tree | a8293a3cbec9488a1acb4214fb27e0654edf3648 /src/error.rs | |
parent | f0f659944c6d89a6205fd98a2434ed61c0e7313b (diff) | |
download | dialog-rs-3e7464c0f8cbadb8c6335c53579ec9570962dc3f.tar.gz dialog-rs-3e7464c0f8cbadb8c6335c53579ec9570962dc3f.tar.bz2 |
Move Error enum to the error module
The trait implementations for the Error enum contain a lot of
boilerplate code and are not very insightful. Therefore this patch
moves the Error enum, its trait implementations and the related Result
type definition from the main module to the error module. The Error and
Result types are re-exported from the main module so that the public
interface does not change.
Diffstat (limited to 'src/error.rs')
-rw-r--r-- | src/error.rs | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..6e32155 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,57 @@ +// Copyright (C) 2019 Robin Krahl <robin.krahl@ireas.org> +// SPDX-License-Identifier: MIT + +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) + } +} |