aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2019-01-27 18:07:59 +0000
committerRobin Krahl <robin.krahl@ireas.org>2019-07-08 21:31:40 +0000
commitbd7c7a5fdf0ae66a1ff2f00beb5ed4c2e6994ca1 (patch)
tree7e9a249db27c80a0e40fe16bcda2c1ca0cd386f3
parent54d23475aa3b712a539bad129fe37223173268f2 (diff)
downloadnitrokey-rs-bd7c7a5fdf0ae66a1ff2f00beb5ed4c2e6994ca1.tar.gz
nitrokey-rs-bd7c7a5fdf0ae66a1ff2f00beb5ed4c2e6994ca1.tar.bz2
Move the connect_model function into Manager
As part of the connection refactoring, this patch moves the connect_model function to the Manager struct. As the connect_model function is not used by nitrokey-test, it is removed.
-rw-r--r--CHANGELOG.md1
-rw-r--r--src/device.rs33
-rw-r--r--src/lib.rs36
-rw-r--r--tests/device.rs12
4 files changed, 45 insertions, 37 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a3a9afe..ffc52e6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -45,6 +45,7 @@ SPDX-License-Identifier: MIT
- Add `ConcurrentAccessError` and `PoisonError` `Error` variants.
- Add the `Manager` struct that manages connections to Nitrokey devices.
- Deprecate the `connect` function.
+ - Remove the `connect_model` function.
# v0.3.4 (2019-01-20)
- Fix authentication methods that assumed that `char` is signed.
diff --git a/src/device.rs b/src/device.rs
index 653c5d1..f28558d 100644
--- a/src/device.rs
+++ b/src/device.rs
@@ -652,35 +652,6 @@ pub fn connect() -> Result<DeviceWrapper, Error> {
crate::take()?.connect().map_err(Into::into)
}
-/// Connects to a Nitrokey device of the given model.
-///
-/// # Errors
-///
-/// - [`NotConnected`][] if no Nitrokey device of the given model is connected
-///
-/// # Example
-///
-/// ```
-/// use nitrokey::DeviceWrapper;
-/// use nitrokey::Model;
-///
-/// fn do_something(device: DeviceWrapper) {}
-///
-/// match nitrokey::connect_model(Model::Pro) {
-/// Ok(device) => do_something(device),
-/// Err(err) => eprintln!("Could not connect to a Nitrokey Pro: {}", err),
-/// }
-/// ```
-///
-/// [`NotConnected`]: enum.CommunicationError.html#variant.NotConnected
-pub fn connect_model(model: Model) -> Result<DeviceWrapper, Error> {
- if connect_enum(model) {
- Ok(create_device_wrapper(model))
- } else {
- Err(CommunicationError::NotConnected.into())
- }
-}
-
fn get_connected_model() -> Option<Model> {
match unsafe { nitrokey_sys::NK_get_device_model() } {
nitrokey_sys::NK_device_model_NK_PRO => Some(Model::Pro),
@@ -689,7 +660,7 @@ fn get_connected_model() -> Option<Model> {
}
}
-fn create_device_wrapper(model: Model) -> DeviceWrapper {
+pub(crate) fn create_device_wrapper(model: Model) -> DeviceWrapper {
match model {
Model::Pro => Pro::new().into(),
Model::Storage => Storage::new().into(),
@@ -700,7 +671,7 @@ pub(crate) fn get_connected_device() -> Option<DeviceWrapper> {
get_connected_model().map(create_device_wrapper)
}
-fn connect_enum(model: Model) -> bool {
+pub(crate) fn connect_enum(model: Model) -> bool {
let model = match model {
Model::Storage => nitrokey_sys::NK_device_model_NK_STORAGE,
Model::Pro => nitrokey_sys::NK_device_model_NK_PRO,
diff --git a/src/lib.rs b/src/lib.rs
index 9f88be3..784dc6f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -110,8 +110,8 @@ pub use crate::auth::{Admin, Authenticate, User};
pub use crate::config::Config;
#[allow(deprecated)]
pub use crate::device::{
- connect, connect_model, Device, DeviceWrapper, Model, Pro, SdCardData, Storage,
- StorageProductionInfo, StorageStatus, VolumeMode, VolumeStatus,
+ connect, Device, DeviceWrapper, Model, Pro, SdCardData, Storage, StorageProductionInfo,
+ StorageStatus, VolumeMode, VolumeStatus,
};
pub use crate::error::{CommandError, CommunicationError, Error, LibraryError};
pub use crate::otp::{ConfigureOtp, GenerateOtp, OtpMode, OtpSlotData};
@@ -214,6 +214,38 @@ impl Manager {
Err(CommunicationError::NotConnected.into())
}
}
+
+ /// Connects to a Nitrokey device of the given model.
+ ///
+ /// # Errors
+ ///
+ /// - [`NotConnected`][] if no Nitrokey device of the given model is connected
+ ///
+ /// # Example
+ ///
+ /// ```
+ /// use nitrokey::DeviceWrapper;
+ /// use nitrokey::Model;
+ ///
+ /// fn do_something(device: DeviceWrapper) {}
+ ///
+ /// # fn main() -> Result<(), nitrokey::Error> {
+ /// match nitrokey::take()?.connect_model(Model::Pro) {
+ /// Ok(device) => do_something(device),
+ /// Err(err) => println!("Could not connect to a Nitrokey Pro: {}", err),
+ /// }
+ /// # Ok(())
+ /// # }
+ /// ```
+ ///
+ /// [`NotConnected`]: enum.CommunicationError.html#variant.NotConnected
+ pub fn connect_model(&mut self, model: Model) -> Result<DeviceWrapper, Error> {
+ if device::connect_enum(model) {
+ Ok(device::create_device_wrapper(model))
+ } else {
+ Err(CommunicationError::NotConnected.into())
+ }
+ }
}
/// Take an instance of the connection manager, blocking until an instance is available.
diff --git a/tests/device.rs b/tests/device.rs
index 5c52024..527d6f2 100644
--- a/tests/device.rs
+++ b/tests/device.rs
@@ -36,11 +36,15 @@ fn connect_no_device() {
assert_cmu_err!(CommunicationError::NotConnected, nitrokey::connect());
assert_cmu_err!(
CommunicationError::NotConnected,
- nitrokey::connect_model(nitrokey::Model::Pro)
+ nitrokey::take()
+ .unwrap()
+ .connect_model(nitrokey::Model::Pro)
);
assert_cmu_err!(
CommunicationError::NotConnected,
- nitrokey::connect_model(nitrokey::Model::Storage)
+ nitrokey::take()
+ .unwrap()
+ .connect_model(nitrokey::Model::Storage)
);
assert_cmu_err!(CommunicationError::NotConnected, nitrokey::Pro::connect());
assert_cmu_err!(
@@ -55,7 +59,7 @@ fn connect_pro(device: Pro) {
drop(device);
assert_any_ok!(nitrokey::connect());
- assert_any_ok!(nitrokey::connect_model(nitrokey::Model::Pro));
+ assert_any_ok!(nitrokey::take().unwrap().connect_model(nitrokey::Model::Pro));
assert_any_ok!(nitrokey::Pro::connect());
}
@@ -65,7 +69,7 @@ fn connect_storage(device: Storage) {
drop(device);
assert_any_ok!(nitrokey::connect());
- assert_any_ok!(nitrokey::connect_model(nitrokey::Model::Storage));
+ assert_any_ok!(nitrokey::take().unwrap().connect_model(nitrokey::Model::Storage));
assert_any_ok!(nitrokey::Storage::connect());
}