diff options
| -rw-r--r-- | CHANGELOG.md | 1 | ||||
| -rw-r--r-- | src/device.rs | 12 | ||||
| -rw-r--r-- | src/lib.rs | 38 | 
3 files changed, 42 insertions, 9 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index e67fd81..a3a9afe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ SPDX-License-Identifier: MIT  - Refactor connection management:    - Add `ConcurrentAccessError` and `PoisonError` `Error` variants.    - Add the `Manager` struct that manages connections to Nitrokey devices. +  - Deprecate the `connect` 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 51551c2..653c5d1 100644 --- a/src/device.rs +++ b/src/device.rs @@ -647,15 +647,9 @@ pub trait Device: Authenticate + GetPasswordSafe + GenerateOtp + fmt::Debug {  /// ```  ///  /// [`NotConnected`]: enum.CommunicationError.html#variant.NotConnected +#[deprecated(since = "0.4.0", note = "use `nitrokey::Manager::connect` instead")]  pub fn connect() -> Result<DeviceWrapper, Error> { -    if unsafe { nitrokey_sys::NK_login_auto() } == 1 { -        match get_connected_device() { -            Some(wrapper) => Ok(wrapper), -            None => Err(CommunicationError::NotConnected.into()), -        } -    } else { -        Err(CommunicationError::NotConnected.into()) -    } +    crate::take()?.connect().map_err(Into::into)  }  /// Connects to a Nitrokey device of the given model. @@ -702,7 +696,7 @@ fn create_device_wrapper(model: Model) -> DeviceWrapper {      }  } -fn get_connected_device() -> Option<DeviceWrapper> { +pub(crate) fn get_connected_device() -> Option<DeviceWrapper> {      get_connected_model().map(create_device_wrapper)  } @@ -108,6 +108,7 @@ use nitrokey_sys;  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, @@ -176,6 +177,43 @@ impl Manager {              marker: marker::PhantomData,          }      } + +    /// Connects to a Nitrokey device. +    /// +    /// This method can be used to connect to any connected device, both a Nitrokey Pro and a +    /// Nitrokey Storage. +    /// +    /// # Errors +    /// +    /// - [`NotConnected`][] if no Nitrokey device is connected +    /// +    /// # Example +    /// +    /// ``` +    /// use nitrokey::DeviceWrapper; +    /// +    /// fn do_something(device: DeviceWrapper) {} +    /// +    /// # fn main() -> Result<(), nitrokey::Error> { +    /// match nitrokey::take()?.connect() { +    ///     Ok(device) => do_something(device), +    ///     Err(err) => println!("Could not connect to a Nitrokey: {}", err), +    /// } +    /// #     Ok(()) +    /// # } +    /// ``` +    /// +    /// [`NotConnected`]: enum.CommunicationError.html#variant.NotConnected +    pub fn connect(&mut self) -> Result<DeviceWrapper, Error> { +        if unsafe { nitrokey_sys::NK_login_auto() } == 1 { +            match device::get_connected_device() { +                Some(wrapper) => Ok(wrapper), +                None => Err(CommunicationError::NotConnected.into()), +            } +        } else { +            Err(CommunicationError::NotConnected.into()) +        } +    }  }  /// Take an instance of the connection manager, blocking until an instance is available.  | 
