aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2019-01-23 04:16:59 +0000
committerRobin Krahl <robin.krahl@ireas.org>2019-01-23 04:26:15 +0000
commitc79ddf8116659efd1aa7de42bb85337632f238dd (patch)
treeac435349263ac9c87478f6d3f5aa85f7c3bbf1b0 /src
parent5ef83ad507d1e5f51152b20628314936b4fb833c (diff)
downloadnitrokey-rs-c79ddf8116659efd1aa7de42bb85337632f238dd.tar.gz
nitrokey-rs-c79ddf8116659efd1aa7de42bb85337632f238dd.tar.bz2
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.
Diffstat (limited to 'src')
-rw-r--r--src/error.rs11
1 files changed, 11 insertions, 0 deletions
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<raw::c_int> for Error {
@@ -58,6 +61,12 @@ impl From<rand_core::Error> for Error {
}
}
+impl From<str::Utf8Error> 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),
}
}
}