From c79ddf8116659efd1aa7de42bb85337632f238dd Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Wed, 23 Jan 2019 04:16:59 +0000 Subject: Add Error::Utf8Error variant Previously, we just ignored UTF-8 errors. This patch prepares the Utf8Error variant so that we are able to return UTF-8 errors. --- CHANGELOG.md | 1 + src/error.rs | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6305ebe..c800521 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - Prefer using the `Into` trait over numeric casting. - Add `Pro::new` and `Storage::new` functions. - Implement `From` and `From` for `DeviceWrapper`. +- Add `Error::Utf8Error` variant. # v0.3.4 (2019-01-20) - Fix authentication methods that assumed that `char` is signed. diff --git a/src/error.rs b/src/error.rs index cde9a34..4b82c6e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,6 +2,7 @@ use std::error; use std::fmt; use std::os::raw; use std::result; +use std::str; /// An error returned by the nitrokey crate. #[derive(Debug)] @@ -18,6 +19,8 @@ pub enum Error { UnexpectedError, /// An unknown error returned by libnitrokey. Unknown(i64), + /// An error occurred when interpreting a UTF-8 string. + Utf8Error(str::Utf8Error), } impl From for Error { @@ -58,6 +61,12 @@ impl From for Error { } } +impl From for Error { + fn from(error: str::Utf8Error) -> Self { + Error::Utf8Error(error) + } +} + impl error::Error for Error { fn source(&self) -> Option<&(dyn error::Error + 'static)> { match *self { @@ -67,6 +76,7 @@ impl error::Error for Error { Error::RandError(ref err) => Some(err), Error::UnexpectedError => None, Error::Unknown(_) => None, + Error::Utf8Error(ref err) => Some(err), } } } @@ -80,6 +90,7 @@ impl fmt::Display for Error { Error::RandError(ref err) => write!(f, "RNG error: {}", err), Error::UnexpectedError => write!(f, "An unexpected error occurred"), Error::Unknown(ref err) => write!(f, "Unknown error: {}", err), + Error::Utf8Error(ref err) => write!(f, "UTF-8 error: {}", err), } } } -- cgit v1.2.1