diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2019-01-27 15:43:32 +0000 |
---|---|---|
committer | Robin Krahl <robin.krahl@ireas.org> | 2019-07-08 21:27:11 +0000 |
commit | 588066f415e956fdcd2c6f6216c52b25911a3b1d (patch) | |
tree | b16014285812bf8286a6f76800c978f8da864486 /src/error.rs | |
parent | a52676d9577f587e0f4d8e47ddc71ba34f0b31ca (diff) | |
download | nitrokey-rs-588066f415e956fdcd2c6f6216c52b25911a3b1d.tar.gz nitrokey-rs-588066f415e956fdcd2c6f6216c52b25911a3b1d.tar.bz2 |
Add Manager struct to manage Nitrokey connections
As part of the connection refactoring, we introduce the Manager struct
that deals with connection management. To make sure there can be only
once instance of the manager, we add a global static Mutex that holds
the single Manager instance. We use the struct to ensure that the user
can only connect to one device at a time.
This also changes the Error::PoisonError variant to store the
sync::PoisonError. This allows the user to call into_inner on the
PoisonError to retrieve the MutexGuard and to ignore the error (for
example useful during testing).
Diffstat (limited to 'src/error.rs')
-rw-r--r-- | src/error.rs | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/src/error.rs b/src/error.rs index c6b19db..b84f5eb 100644 --- a/src/error.rs +++ b/src/error.rs @@ -21,7 +21,7 @@ pub enum Error { /// A library usage error. LibraryError(LibraryError), /// An error that occurred due to a poisoned lock. - PoisonError, + PoisonError(sync::PoisonError<sync::MutexGuard<'static, crate::Manager>>), /// An error that occurred during random number generation. RandError(Box<dyn error::Error>), /// An error that is caused by an unexpected value returned by libnitrokey. @@ -70,14 +70,14 @@ impl From<str::Utf8Error> for Error { } } -impl<T> From<sync::PoisonError<T>> for Error { - fn from(_error: sync::PoisonError<T>) -> Self { - Error::PoisonError +impl From<sync::PoisonError<sync::MutexGuard<'static, crate::Manager>>> for Error { + fn from(error: sync::PoisonError<sync::MutexGuard<'static, crate::Manager>>) -> Self { + Error::PoisonError(error) } } -impl<T> From<sync::TryLockError<T>> for Error { - fn from(error: sync::TryLockError<T>) -> Self { +impl From<sync::TryLockError<sync::MutexGuard<'static, crate::Manager>>> for Error { + fn from(error: sync::TryLockError<sync::MutexGuard<'static, crate::Manager>>) -> Self { match error { sync::TryLockError::Poisoned(err) => err.into(), sync::TryLockError::WouldBlock => Error::ConcurrentAccessError, @@ -98,7 +98,7 @@ impl error::Error for Error { Error::CommunicationError(ref err) => Some(err), Error::ConcurrentAccessError => None, Error::LibraryError(ref err) => Some(err), - Error::PoisonError => None, + Error::PoisonError(ref err) => Some(err), Error::RandError(ref err) => Some(err.as_ref()), Error::UnexpectedError => None, Error::UnknownError(_) => None, @@ -114,7 +114,7 @@ impl fmt::Display for Error { Error::CommunicationError(ref err) => write!(f, "Communication error: {}", err), Error::ConcurrentAccessError => write!(f, "Internal error: concurrent access"), Error::LibraryError(ref err) => write!(f, "Library error: {}", err), - Error::PoisonError => write!(f, "Internal error: poisoned lock"), + Error::PoisonError(_) => write!(f, "Internal error: poisoned lock"), Error::RandError(ref err) => write!(f, "RNG error: {}", err), Error::UnexpectedError => write!(f, "An unexpected error occurred"), Error::UnknownError(ref err) => write!(f, "Unknown error: {}", err), |