aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs61
1 files changed, 34 insertions, 27 deletions
diff --git a/src/error.rs b/src/error.rs
index dfe1680..c5a975e 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -13,6 +13,18 @@ pub enum Error {
CommunicationError(CommunicationError),
/// An error that occured during random number generation.
RandError(rand_core::Error),
+ /// An unknown error returned by libnitrokey.
+ Unknown(i64),
+}
+
+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 {
+ Error::Unknown(code.into())
+ }
+ }
}
impl From<CommandError> for Error {
@@ -33,6 +45,7 @@ impl error::Error for Error {
Error::CommandError(ref err) => Some(err),
Error::CommunicationError(_) => None,
Error::RandError(ref err) => Some(err),
+ Error::Unknown(_) => None,
}
}
}
@@ -43,6 +56,7 @@ impl fmt::Display for Error {
Error::CommandError(ref err) => write!(f, "Command error: {}", err),
Error::CommunicationError(_) => write!(f, "Placeholder"),
Error::RandError(ref err) => write!(f, "RNG error: {}", err),
+ Error::Unknown(ref err) => write!(f, "Unknown error: {}", err),
}
}
}
@@ -74,8 +88,6 @@ pub enum CommandError {
UnknownCommand,
/// AES decryption failed.
AesDecryptionFailed,
- /// An unknown error occurred.
- Unknown(i64),
/// An unspecified error occurred.
Undefined,
/// You passed a string containing a null byte.
@@ -98,6 +110,26 @@ pub enum CommunicationError {
}
impl CommandError {
+ fn try_from(value: raw::c_int) -> Option<Self> {
+ match value {
+ 1 => Some(CommandError::WrongCrc),
+ 2 => Some(CommandError::WrongSlot),
+ 3 => Some(CommandError::SlotNotProgrammed),
+ 4 => Some(CommandError::WrongPassword),
+ 5 => Some(CommandError::NotAuthorized),
+ 6 => Some(CommandError::Timestamp),
+ 7 => Some(CommandError::NoName),
+ 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,
+ }
+ }
+
fn as_str(&self) -> borrow::Cow<'static, str> {
match *self {
CommandError::WrongCrc => {
@@ -116,9 +148,6 @@ impl CommandError {
CommandError::NotSupported => "This command is not supported by this device".into(),
CommandError::UnknownCommand => "This command is unknown".into(),
CommandError::AesDecryptionFailed => "AES decryption failed".into(),
- CommandError::Unknown(x) => {
- borrow::Cow::from(format!("An unknown error occurred ({})", x))
- }
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(),
@@ -138,25 +167,3 @@ impl fmt::Display for CommandError {
write!(f, "{}", self.as_str())
}
}
-
-impl From<raw::c_int> for CommandError {
- fn from(value: raw::c_int) -> Self {
- match value {
- 1 => CommandError::WrongCrc,
- 2 => CommandError::WrongSlot,
- 3 => CommandError::SlotNotProgrammed,
- 4 => CommandError::WrongPassword,
- 5 => CommandError::NotAuthorized,
- 6 => CommandError::Timestamp,
- 7 => CommandError::NoName,
- 8 => CommandError::NotSupported,
- 9 => CommandError::UnknownCommand,
- 10 => CommandError::AesDecryptionFailed,
- 200 => CommandError::StringTooLong,
- 201 => CommandError::InvalidSlot,
- 202 => CommandError::InvalidHexString,
- 203 => CommandError::TargetBufferTooSmall,
- x => CommandError::Unknown(x.into()),
- }
- }
-}