From fa2dfee36238fad8951217d258ce8b7360f3539f Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Fri, 11 Jan 2019 03:11:16 +0000 Subject: Move the Error enum to the error module --- src/error.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 66 +++------------------------------------------------------ 2 files changed, 72 insertions(+), 63 deletions(-) create mode 100644 src/error.rs diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..cc3b235 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,69 @@ +// Copyright (C) 2019 Robin Krahl +// SPDX-License-Identifier: MIT + +use std::fmt; +use std::io; +use std::process; +use std::str; + +#[derive(Debug)] +pub enum Error { + DialogError(dialog::Error), + IoError(io::Error), + Error(String), + UrlParseError(url::ParseError), + Utf8Error(str::Utf8Error), +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + Error::DialogError(ref err) => write!(f, "Dialog error: {:?}", err), + Error::IoError(ref err) => write!(f, "IO error: {}", err), + Error::Error(ref string) => write!(f, "Error: {}", string), + Error::UrlParseError(ref err) => write!(f, "URL parse error: {}", err), + Error::Utf8Error(ref err) => write!(f, "UTF-8 error: {}", err), + } + } +} + +impl From<&str> for Error { + fn from(string: &str) -> Error { + Error::Error(string.to_string()) + } +} + +impl From for Error { + fn from(error: dialog::Error) -> Error { + Error::DialogError(error) + } +} + +impl From for Error { + fn from(error: io::Error) -> Error { + Error::IoError(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!("{} failed with error code {}", command, code), + None => format!("{} was terminated by a signal", command), + }; + Error::Error(msg) + } +} + +impl From for Error { + fn from(error: url::ParseError) -> Error { + Error::UrlParseError(error) + } +} + +impl From for Error { + fn from(error: str::Utf8Error) -> Error { + Error::Utf8Error(error) + } +} diff --git a/src/main.rs b/src/main.rs index 331e22f..ba5298d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,77 +5,17 @@ //! Reads OTP configuration from a QR code and writes it to an OTP slot on a Nitrokey device. +mod error; + use std::borrow; -use std::fmt; use std::fs; -use std::io; use std::path; use std::process; use std::str; use dialog::DialogBox; -#[derive(Debug)] -enum Error { - DialogError(dialog::Error), - IoError(io::Error), - Error(String), - UrlParseError(url::ParseError), - Utf8Error(str::Utf8Error), -} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match *self { - Error::DialogError(ref err) => write!(f, "Dialog error: {:?}", err), - Error::IoError(ref err) => write!(f, "IO error: {}", err), - Error::Error(ref string) => write!(f, "Error: {}", string), - Error::UrlParseError(ref err) => write!(f, "URL parse error: {}", err), - Error::Utf8Error(ref err) => write!(f, "UTF-8 error: {}", err), - } - } -} - -impl From<&str> for Error { - fn from(string: &str) -> Error { - Error::Error(string.to_string()) - } -} - -impl From for Error { - fn from(error: dialog::Error) -> Error { - Error::DialogError(error) - } -} - -impl From for Error { - fn from(error: io::Error) -> Error { - Error::IoError(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!("{} failed with error code {}", command, code), - None => format!("{} was terminated by a signal", command), - }; - Error::Error(msg) - } -} - -impl From for Error { - fn from(error: url::ParseError) -> Error { - Error::UrlParseError(error) - } -} - -impl From for Error { - fn from(error: str::Utf8Error) -> Error { - Error::Utf8Error(error) - } -} +use crate::error::Error; #[derive(Debug, PartialEq)] struct UrlData { -- cgit v1.2.3