diff options
-rw-r--r-- | src/error.rs | 69 | ||||
-rw-r--r-- | src/main.rs | 66 |
2 files changed, 72 insertions, 63 deletions
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 <robin.krahl@ireas.org> +// 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<dialog::Error> for Error { + fn from(error: dialog::Error) -> Error { + Error::DialogError(error) + } +} + +impl From<io::Error> 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<url::ParseError> for Error { + fn from(error: url::ParseError) -> Error { + Error::UrlParseError(error) + } +} + +impl From<str::Utf8Error> 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<dialog::Error> for Error { - fn from(error: dialog::Error) -> Error { - Error::DialogError(error) - } -} - -impl From<io::Error> 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<url::ParseError> for Error { - fn from(error: url::ParseError) -> Error { - Error::UrlParseError(error) - } -} - -impl From<str::Utf8Error> for Error { - fn from(error: str::Utf8Error) -> Error { - Error::Utf8Error(error) - } -} +use crate::error::Error; #[derive(Debug, PartialEq)] struct UrlData { |