From 3e7464c0f8cbadb8c6335c53579ec9570962dc3f Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Thu, 10 Jan 2019 23:43:15 +0000 Subject: 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. --- src/error.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 57 +++------------------------------------------------------ 2 files changed, 60 insertions(+), 54 deletions(-) create mode 100644 src/error.rs 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 +// 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 = result::Result; + +/// 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 for Error { + fn from(error: io::Error) -> Error { + Error::IoError(error) + } +} + +impl From for Error { + fn from(error: str::Utf8Error) -> Error { + Error::Utf8Error(error) + } +} + +impl From 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) + } +} diff --git a/src/lib.rs b/src/lib.rs index dd255d9..f2babce 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -72,6 +72,8 @@ //! [`show`]: trait.DialogBox.html#method.show //! [`show_with`]: trait.DialogBox.html#method.show_with +mod error; + /// Backends that display dialog boxes. /// /// All backends implement the [`Backend`][] trait. Some backends might provide additional @@ -81,60 +83,7 @@ /// [`Backend`]: trait.Backend.html pub mod backends; -use std::io; -use std::process; -use std::result; -use std::str; -use std::string; - -/// A result returned by `dialog`. -pub type Result = result::Result; - -/// 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 for Error { - fn from(error: io::Error) -> Error { - Error::IoError(error) - } -} - -impl From for Error { - fn from(error: str::Utf8Error) -> Error { - Error::Utf8Error(error) - } -} - -impl From 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) - } -} +pub use crate::error::{Error, Result}; /// A dialog box that can be shown using a backend. /// -- cgit v1.2.1