diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2019-01-06 23:27:06 +0000 |
---|---|---|
committer | Robin Krahl <robin.krahl@ireas.org> | 2019-01-07 00:29:52 +0100 |
commit | 3fab663891d42cfe317125650394c9560639b60c (patch) | |
tree | 36d72acd96d49cc1d8da85b920a30a42acc721cb /src | |
parent | d44f8be7c038bc499b36aa502f4939f41ebebfbf (diff) | |
download | nitrokey-rs-3fab663891d42cfe317125650394c9560639b60c.tar.gz nitrokey-rs-3fab663891d42cfe317125650394c9560639b60c.tar.bz2 |
Add the connect_model function
This patch adds the global connect_model function that can be used to
connect to a Nitrokey device of a given model. Contrary to Pro::connect
and Storage::connect, the model does not have to be set at compile time.
Diffstat (limited to 'src')
-rw-r--r-- | src/device.rs | 29 | ||||
-rw-r--r-- | src/lib.rs | 2 |
2 files changed, 30 insertions, 1 deletions
diff --git a/src/device.rs b/src/device.rs index 88be5b5..2eee08e 100644 --- a/src/device.rs +++ b/src/device.rs @@ -613,6 +613,35 @@ pub fn connect() -> Result<DeviceWrapper, CommandError> { } } +/// Connects to a Nitrokey device of the given model. +/// +/// # Errors +/// +/// - [`Undefined`][] 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) => println!("Could not connect to a Nitrokey Pro: {}", err), +/// } +/// ``` +/// +/// [`Undefined`]: enum.CommandError.html#variant.Undefined +pub fn connect_model(model: Model) -> Result<DeviceWrapper, CommandError> { + if connect_enum(model) { + Ok(create_device_wrapper(model)) + } else { + Err(CommandError::Undefined) + } +} + fn get_connected_model() -> Option<Model> { unsafe { match nitrokey_sys::NK_get_device_model() { @@ -98,7 +98,7 @@ use nitrokey_sys; pub use crate::auth::{Admin, Authenticate, User}; pub use crate::config::Config; pub use crate::device::{ - connect, Device, DeviceWrapper, Model, Pro, Storage, StorageStatus, VolumeStatus, + connect, connect_model, Device, DeviceWrapper, Model, Pro, Storage, StorageStatus, VolumeStatus, }; pub use crate::otp::{ConfigureOtp, GenerateOtp, OtpMode, OtpSlotData}; pub use crate::pws::{GetPasswordSafe, PasswordSafe, SLOT_COUNT}; |