diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2019-01-11 03:11:16 +0000 |
---|---|---|
committer | Robin Krahl <me@robin-krahl.de> | 2019-01-11 04:37:01 +0100 |
commit | fa2dfee36238fad8951217d258ce8b7360f3539f (patch) | |
tree | 1710fc749afab9e36e33a6e92f1d4222ca1812dd /src | |
parent | 5ede9c31db245cc3c3354d9c16200545fa5255db (diff) | |
download | nitrocli-otp-qr-fa2dfee36238fad8951217d258ce8b7360f3539f.tar.gz nitrocli-otp-qr-fa2dfee36238fad8951217d258ce8b7360f3539f.tar.bz2 |
Move the Error enum to the error module
Diffstat (limited to 'src')
-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 { |