diff options
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}; |