aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2018-12-30 00:04:23 +0100
committerRobin Krahl <robin.krahl@ireas.org>2018-12-30 00:04:23 +0100
commit3d951ab54daea9c362bdae2e0945701955c412e5 (patch)
treefeeee31249946184590ada8ec378aef2eb8a4382 /src
parente2d333574f54bf57a1bba970dbbfe5844ef8bf0d (diff)
downloadnitrokey-rs-3d951ab54daea9c362bdae2e0945701955c412e5.tar.gz
nitrokey-rs-3d951ab54daea9c362bdae2e0945701955c412e5.tar.bz2
Add function Device::get_model
This patch adds the function Device::get_model that returns the model of the connected Nitrokey stick.
Diffstat (limited to 'src')
-rw-r--r--src/device.rs54
-rw-r--r--src/lib.rs2
2 files changed, 50 insertions, 6 deletions
diff --git a/src/device.rs b/src/device.rs
index 1455a57..1f9ae3d 100644
--- a/src/device.rs
+++ b/src/device.rs
@@ -1,3 +1,5 @@
+use std::fmt;
+
use libc;
use nitrokey_sys;
@@ -8,14 +10,27 @@ use crate::pws::GetPasswordSafe;
use crate::util::{get_command_result, get_cstring, get_last_error, result_from_string, CommandError};
/// Available Nitrokey models.
-#[derive(Debug, PartialEq)]
-enum Model {
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub enum Model {
/// The Nitrokey Storage.
Storage,
/// The Nitrokey Pro.
Pro,
}
+impl fmt::Display for Model {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(
+ f,
+ "{}",
+ match *self {
+ Model::Pro => "Pro",
+ Model::Storage => "Storage",
+ }
+ )
+ }
+}
+
/// A wrapper for a Nitrokey device of unknown type.
///
/// Use the function [`connect`][] to obtain a wrapped instance. The wrapper implements all traits
@@ -211,6 +226,20 @@ pub struct StorageStatus {
/// This trait provides the commands that can be executed without authentication and that are
/// present on all supported Nitrokey devices.
pub trait Device: Authenticate + GetPasswordSafe + GenerateOtp {
+ /// Returns the model of the connected Nitrokey device.
+ ///
+ /// # Example
+ ///
+ /// ```no_run
+ /// # use nitrokey::CommandError;
+ ///
+ /// # fn try_main() -> Result<(), CommandError> {
+ /// let device = nitrokey::connect()?;
+ /// println!("Connected to a Nitrokey {}", device.get_model());
+ /// # Ok(())
+ /// # }
+ fn get_model(&self) -> Model;
+
/// Returns the serial number of the Nitrokey device. The serial number is the string
/// representation of a hex number.
///
@@ -563,7 +592,14 @@ impl GenerateOtp for DeviceWrapper {
}
}
-impl Device for DeviceWrapper {}
+impl Device for DeviceWrapper {
+ fn get_model(&self) -> Model {
+ match *self {
+ DeviceWrapper::Pro(_) => Model::Pro,
+ DeviceWrapper::Storage(_) => Model::Storage,
+ }
+ }
+}
impl Pro {
/// Connects to a Nitrokey Pro.
@@ -597,7 +633,11 @@ impl Drop for Pro {
}
}
-impl Device for Pro {}
+impl Device for Pro {
+ fn get_model(&self) -> Model {
+ Model::Pro
+ }
+}
impl GenerateOtp for Pro {}
@@ -743,7 +783,11 @@ impl Drop for Storage {
}
}
-impl Device for Storage {}
+impl Device for Storage {
+ fn get_model(&self) -> Model {
+ Model::Pro
+ }
+}
impl GenerateOtp for Storage {}
diff --git a/src/lib.rs b/src/lib.rs
index d0e4357..37f839a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -102,7 +102,7 @@ use nitrokey_sys;
pub use crate::auth::{Admin, Authenticate, User};
pub use crate::config::Config;
-pub use crate::device::{connect, Device, DeviceWrapper, Pro, Storage, StorageStatus, VolumeStatus};
+pub use crate::device::{connect, Device, DeviceWrapper, Model, Pro, Storage, StorageStatus, VolumeStatus};
pub use crate::otp::{ConfigureOtp, GenerateOtp, OtpMode, OtpSlotData};
pub use crate::pws::{GetPasswordSafe, PasswordSafe, SLOT_COUNT};
pub use crate::util::{CommandError, LogLevel};