aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2019-01-27 18:21:08 +0000
committerRobin Krahl <robin.krahl@ireas.org>2019-07-08 21:33:13 +0000
commit379bc798477a1de7ffda923c5d10ca63aebae25f (patch)
tree1cd4dcbc9ddcbf23b9f546d097f3d5ef2f454992 /src/lib.rs
parentbd7c7a5fdf0ae66a1ff2f00beb5ed4c2e6994ca1 (diff)
downloadnitrokey-rs-379bc798477a1de7ffda923c5d10ca63aebae25f.tar.gz
nitrokey-rs-379bc798477a1de7ffda923c5d10ca63aebae25f.tar.bz2
Move {Pro, Storage}::connect into Manager
As part of the connection refactoring, this patch moves the connect methods of the Pro and Storage structs into the Manager struct. To maintain compatibility with nitrokey-test, the old methods are not removed but marked as deprecated.
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 784dc6f..dc3432a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -246,6 +246,68 @@ impl Manager {
Err(CommunicationError::NotConnected.into())
}
}
+
+ /// Connects to a Nitrokey Pro.
+ ///
+ /// # Errors
+ ///
+ /// - [`NotConnected`][] if no Nitrokey device of the given model is connected
+ ///
+ /// # Example
+ ///
+ /// ```
+ /// use nitrokey::Pro;
+ ///
+ /// fn use_pro(device: Pro) {}
+ ///
+ /// # fn main() -> Result<(), nitrokey::Error> {
+ /// match nitrokey::take()?.connect_pro() {
+ /// Ok(device) => use_pro(device),
+ /// Err(err) => println!("Could not connect to the Nitrokey Pro: {}", err),
+ /// }
+ /// # Ok(())
+ /// # }
+ /// ```
+ ///
+ /// [`NotConnected`]: enum.CommunicationError.html#variant.NotConnected
+ pub fn connect_pro(&mut self) -> Result<Pro, Error> {
+ if device::connect_enum(device::Model::Pro) {
+ Ok(device::Pro::new())
+ } else {
+ Err(CommunicationError::NotConnected.into())
+ }
+ }
+
+ /// Connects to a Nitrokey Storage.
+ ///
+ /// # Errors
+ ///
+ /// - [`NotConnected`][] if no Nitrokey device of the given model is connected
+ ///
+ /// # Example
+ ///
+ /// ```
+ /// use nitrokey::Storage;
+ ///
+ /// fn use_storage(device: Storage) {}
+ ///
+ /// # fn main() -> Result<(), nitrokey::Error> {
+ /// match nitrokey::take()?.connect_storage() {
+ /// Ok(device) => use_storage(device),
+ /// Err(err) => println!("Could not connect to the Nitrokey Storage: {}", err),
+ /// }
+ /// # Ok(())
+ /// # }
+ /// ```
+ ///
+ /// [`NotConnected`]: enum.CommunicationError.html#variant.NotConnected
+ pub fn connect_storage(&mut self) -> Result<Storage, Error> {
+ if device::connect_enum(Model::Storage) {
+ Ok(Storage::new())
+ } else {
+ Err(CommunicationError::NotConnected.into())
+ }
+ }
}
/// Take an instance of the connection manager, blocking until an instance is available.