aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/error.rs')
-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),
}
}
}