aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2019-01-17 12:47:52 +0000
committerRobin Krahl <robin.krahl@ireas.org>2019-01-20 21:08:50 +0000
commit5e258d26b55af6bed7c316b1c7ac12e20946702d (patch)
treefee2be71586bef2f1978eb1cf40ef84c8ac6492b /src/error.rs
parent944e1fa0d51e547dde2a9368d2b8431b109f63c4 (diff)
downloadnitrokey-rs-5e258d26b55af6bed7c316b1c7ac12e20946702d.tar.gz
nitrokey-rs-5e258d26b55af6bed7c316b1c7ac12e20946702d.tar.bz2
Refactor library errors into LibraryError enum
Previously, library errors were part of the CommandError enum. As command errors and library errors are two different error types, they should be split into two enums.
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs74
1 files changed, 53 insertions, 21 deletions
diff --git a/src/error.rs b/src/error.rs
index c5a975e..f40d07f 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -11,6 +11,8 @@ pub enum Error {
CommandError(CommandError),
/// Placeholder for testing.
CommunicationError(CommunicationError),
+ /// A library usage error.
+ LibraryError(LibraryError),
/// An error that occured during random number generation.
RandError(rand_core::Error),
/// An unknown error returned by libnitrokey.
@@ -21,6 +23,8 @@ impl From<raw::c_int> for Error {
fn from(code: raw::c_int) -> Self {
if let Some(err) = CommandError::try_from(code) {
Error::CommandError(err)
+ } else if let Some(err) = LibraryError::try_from(code) {
+ Error::LibraryError(err)
} else {
Error::Unknown(code.into())
}
@@ -33,6 +37,12 @@ impl From<CommandError> for Error {
}
}
+impl From<LibraryError> for Error {
+ fn from(err: LibraryError) -> Self {
+ Error::LibraryError(err)
+ }
+}
+
impl From<rand_core::Error> for Error {
fn from(error: rand_core::Error) -> Self {
Error::RandError(error)
@@ -44,6 +54,7 @@ impl error::Error for Error {
match *self {
Error::CommandError(ref err) => Some(err),
Error::CommunicationError(_) => None,
+ Error::LibraryError(ref err) => Some(err),
Error::RandError(ref err) => Some(err),
Error::Unknown(_) => None,
}
@@ -55,6 +66,7 @@ impl fmt::Display for Error {
match *self {
Error::CommandError(ref err) => write!(f, "Command error: {}", err),
Error::CommunicationError(_) => write!(f, "Placeholder"),
+ Error::LibraryError(ref err) => write!(f, "Library error: {}", err),
Error::RandError(ref err) => write!(f, "RNG error: {}", err),
Error::Unknown(ref err) => write!(f, "Unknown error: {}", err),
}
@@ -90,16 +102,6 @@ pub enum CommandError {
AesDecryptionFailed,
/// An unspecified error occurred.
Undefined,
- /// You passed a string containing a null byte.
- InvalidString,
- /// A supplied string exceeded a length limit.
- StringTooLong,
- /// You passed an invalid slot.
- InvalidSlot,
- /// The supplied string was not in hexadecimal format.
- InvalidHexString,
- /// The target buffer was smaller than the source.
- TargetBufferTooSmall,
}
/// Placeholder for testing.
@@ -122,10 +124,6 @@ impl CommandError {
8 => Some(CommandError::NotSupported),
9 => Some(CommandError::UnknownCommand),
10 => Some(CommandError::AesDecryptionFailed),
- 200 => Some(CommandError::StringTooLong),
- 201 => Some(CommandError::InvalidSlot),
- 202 => Some(CommandError::InvalidHexString),
- 203 => Some(CommandError::TargetBufferTooSmall),
_ => None,
}
}
@@ -149,13 +147,6 @@ impl CommandError {
CommandError::UnknownCommand => "This command is unknown".into(),
CommandError::AesDecryptionFailed => "AES decryption failed".into(),
CommandError::Undefined => "An unspecified error occurred".into(),
- CommandError::InvalidString => "You passed a string containing a null byte".into(),
- CommandError::StringTooLong => "The supplied string is too long".into(),
- CommandError::InvalidSlot => "The given slot is invalid".into(),
- CommandError::InvalidHexString => {
- "The supplied string is not in hexadecimal format".into()
- }
- CommandError::TargetBufferTooSmall => "The target buffer is too small".into(),
}
}
}
@@ -167,3 +158,44 @@ impl fmt::Display for CommandError {
write!(f, "{}", self.as_str())
}
}
+
+/// A library usage error.
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub enum LibraryError {
+ /// A supplied string exceeded a length limit.
+ StringTooLong,
+ /// You passed an invalid slot.
+ InvalidSlot,
+ /// The supplied string was not in hexadecimal format.
+ InvalidHexString,
+ /// The target buffer was smaller than the source.
+ TargetBufferTooSmall,
+ /// You passed a string containing a null byte.
+ InvalidString,
+}
+
+impl LibraryError {
+ fn try_from(value: raw::c_int) -> Option<Self> {
+ match value {
+ 200 => Some(LibraryError::StringTooLong),
+ 201 => Some(LibraryError::InvalidSlot),
+ 202 => Some(LibraryError::InvalidHexString),
+ 203 => Some(LibraryError::TargetBufferTooSmall),
+ _ => None,
+ }
+ }
+}
+
+impl error::Error for LibraryError {}
+
+impl fmt::Display for LibraryError {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.write_str(match *self {
+ LibraryError::StringTooLong => "The supplied string is too long",
+ LibraryError::InvalidSlot => "The given slot is invalid",
+ LibraryError::InvalidHexString => "The supplied string is not in hexadecimal format",
+ LibraryError::TargetBufferTooSmall => "The target buffer is too small",
+ LibraryError::InvalidString => "You passed a string containing a null byte",
+ })
+ }
+}