diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2019-01-17 02:05:52 +0000 |
---|---|---|
committer | Robin Krahl <robin.krahl@ireas.org> | 2019-01-20 20:57:07 +0000 |
commit | db198936be1a80f1735731d9e95eb6f4c48a5329 (patch) | |
tree | 3d157472a3e88aea458b16cb87c58d6f5c42ac11 /src | |
parent | 591c55ff294ee12812e4be1b90f03a093f83a4f5 (diff) | |
download | nitrokey-rs-db198936be1a80f1735731d9e95eb6f4c48a5329.tar.gz nitrokey-rs-db198936be1a80f1735731d9e95eb6f4c48a5329.tar.bz2 |
Add the Error enum and the Result<T> typedef
The Error enum is a wrapper for the possible error types (currently only
CommandError). Result<T> is defined as Result<T, Error>.
Diffstat (limited to 'src')
-rw-r--r-- | src/error.rs | 35 | ||||
-rw-r--r-- | src/lib.rs | 2 |
2 files changed, 35 insertions, 2 deletions
diff --git a/src/error.rs b/src/error.rs index dcaa4d2..89c4c82 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,8 +2,41 @@ use std::borrow; use std::error; use std::fmt; use std::os::raw; +use std::result; -/// Error types returned by Nitrokey device or by the library. +/// An error returned by the nitrokey crate. +#[derive(Debug)] +pub enum Error { + /// An error reported by the Nitrokey device in the response packet. + CommandError(CommandError), +} + +impl From<CommandError> for Error { + fn from(err: CommandError) -> Self { + Error::CommandError(err) + } +} + +impl error::Error for Error { + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + match *self { + Error::CommandError(ref err) => Some(err), + } + } +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + Error::CommandError(ref err) => write!(f, "Command error: {}", err), + } + } +} + +/// A result returned by the nitrokey crate. +pub type Result<T> = result::Result<T, Error>; + +/// An error reported by the Nitrokey device in the response packet. #[derive(Clone, Copy, Debug, PartialEq)] pub enum CommandError { /// A packet with a wrong checksum has been sent or received. @@ -104,7 +104,7 @@ pub use crate::device::{ connect, connect_model, Device, DeviceWrapper, Model, Pro, SdCardData, Storage, StorageProductionInfo, StorageStatus, VolumeMode, VolumeStatus, }; -pub use crate::error::CommandError; +pub use crate::error::{CommandError, Error, Result}; pub use crate::otp::{ConfigureOtp, GenerateOtp, OtpMode, OtpSlotData}; pub use crate::pws::{GetPasswordSafe, PasswordSafe, SLOT_COUNT}; pub use crate::util::LogLevel; |