diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2018-05-28 20:32:10 +0000 |
---|---|---|
committer | Robin Krahl <robin.krahl@ireas.org> | 2018-05-28 22:33:57 +0200 |
commit | 8f7eb1679fa988ad97b0e6dbe0ef5d1edef18351 (patch) | |
tree | dc1735d256383e194189fc586d10ebe5750eaee6 /src | |
parent | 15706f8708af4175a5376026cdd7c6f821945a5d (diff) | |
download | nitrokey-rs-8f7eb1679fa988ad97b0e6dbe0ef5d1edef18351.tar.gz nitrokey-rs-8f7eb1679fa988ad97b0e6dbe0ef5d1edef18351.tar.bz2 |
Restructure trait implementations for wrappers
For DeviceWrapper, the traits GenerateOtp and Device are now directly
implemented instead of complicated template implementations. For User
and Admin, the wrapper implementations are replaced with an
implementation of the Deref trait for easier access.
Diffstat (limited to 'src')
-rw-r--r-- | src/device.rs | 64 | ||||
-rw-r--r-- | src/tests/pro.rs | 5 |
2 files changed, 30 insertions, 39 deletions
diff --git a/src/device.rs b/src/device.rs index 25e4596..8f7dc00 100644 --- a/src/device.rs +++ b/src/device.rs @@ -2,6 +2,7 @@ use config::{Config, RawConfig}; use libc; use nitrokey_sys; use std::ffi::CString; +use std::ops::Deref; use std::os::raw::c_int; use misc::Authenticate; use otp::{ConfigureOtp, GenerateOtp, OtpMode, OtpSlotData, RawOtpSlotData}; @@ -403,43 +404,34 @@ fn connect_model(model: Model) -> bool { unsafe { nitrokey_sys::NK_login_enum(model) == 1 } } -impl<T: AsRef<GenerateOtp>> GenerateOtp for T { +impl DeviceWrapper { + fn device(&self) -> &Device { + match *self { + DeviceWrapper::Storage(_) => panic!("..."), + DeviceWrapper::Pro(ref pro) => pro, + } + } +} + +impl GenerateOtp for DeviceWrapper { fn get_hotp_slot_name(&self, slot: u8) -> Result<String, CommandError> { - self.as_ref().get_hotp_slot_name(slot) + self.device().get_hotp_slot_name(slot) } fn get_totp_slot_name(&self, slot: u8) -> Result<String, CommandError> { - self.as_ref().get_totp_slot_name(slot) + self.device().get_totp_slot_name(slot) } fn get_hotp_code(&self, slot: u8) -> Result<String, CommandError> { - self.as_ref().get_hotp_code(slot) + self.device().get_hotp_code(slot) } fn get_totp_code(&self, slot: u8) -> Result<String, CommandError> { - self.as_ref().get_totp_code(slot) + self.device().get_totp_code(slot) } } -impl<T: AsRef<Device> + GenerateOtp> Device for T {} - -impl AsRef<GenerateOtp> for DeviceWrapper { - fn as_ref(&self) -> &(GenerateOtp + 'static) { - match *self { - DeviceWrapper::Storage(_) => panic!("..."), - DeviceWrapper::Pro(ref pro) => pro, - } - } -} - -impl AsRef<Device> for DeviceWrapper { - fn as_ref(&self) -> &(Device + 'static) { - match *self { - DeviceWrapper::Storage(_) => panic!("..."), - DeviceWrapper::Pro(ref pro) => pro, - } - } -} +impl Device for DeviceWrapper {} impl Authenticate for DeviceWrapper { fn authenticate_user(self, password: &str) -> Result<User<Self>, (Self, CommandError)> { @@ -534,12 +526,6 @@ impl Device for Pro {} impl GenerateOtp for Pro {} -impl<T: Device + 'static> AsRef<Device> for User<T> { - fn as_ref(&self) -> &(Device + 'static) { - &self.device - } -} - impl<T: Device> User<T> { /// Forgets the user authentication and returns an unauthenticated device. This method /// consumes the authenticated device. It does not perform any actual commands on the @@ -549,6 +535,14 @@ impl<T: Device> User<T> { } } +impl<T: Device> Deref for User<T> { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.device + } +} + impl<T: Device> GenerateOtp for User<T> { fn get_hotp_code(&self, slot: u8) -> Result<String, CommandError> { unsafe { @@ -580,14 +574,10 @@ impl<T: Device> AuthenticatedDevice<T> for User<T> { } } -impl<T: Device + 'static> AsRef<GenerateOtp> for Admin<T> { - fn as_ref(&self) -> &(GenerateOtp + 'static) { - &self.device - } -} +impl<T: Device> Deref for Admin<T> { + type Target = T; -impl<T: Device + 'static> AsRef<Device> for Admin<T> { - fn as_ref(&self) -> &(Device + 'static) { + fn deref(&self) -> &Self::Target { &self.device } } diff --git a/src/tests/pro.rs b/src/tests/pro.rs index e52c287..5415ff9 100644 --- a/src/tests/pro.rs +++ b/src/tests/pro.rs @@ -1,4 +1,5 @@ use std::ffi::CStr; +use std::ops::Deref; use {Admin, Authenticate, CommandError, CommandStatus, Config, ConfigureOtp, Device, GenerateOtp, OtpMode, OtpSlotData, Pro}; @@ -101,7 +102,7 @@ fn hotp() { assert_eq!(CommandStatus::Success, admin.write_config(config)); configure_hotp(&admin); - check_hotp_codes(&admin); + check_hotp_codes(admin.deref()); configure_hotp(&admin); check_hotp_codes(&admin.device()); @@ -203,7 +204,7 @@ fn totp() { assert_eq!(CommandStatus::Success, admin.write_config(config)); configure_totp(&admin); - check_totp_codes(&admin); + check_totp_codes(admin.deref()); configure_totp(&admin); check_totp_codes(&admin.device()); |