diff options
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 68 |
1 files changed, 36 insertions, 32 deletions
diff --git a/src/config.rs b/src/config.rs index c273792..bc935d7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,20 +1,22 @@ // Copyright (C) 2018-2019 Robin Krahl <robin.krahl@ireas.org> // SPDX-License-Identifier: MIT +use std::convert; + use crate::error::{Error, LibraryError}; /// The configuration for a Nitrokey. #[derive(Clone, Copy, Debug, PartialEq)] pub struct Config { - /// If set, the stick will generate a code from the HOTP slot with the given number if numlock - /// is pressed. The slot number must be 0, 1 or 2. - pub numlock: Option<u8>, - /// If set, the stick will generate a code from the HOTP slot with the given number if capslock + /// If set, the stick will generate a code from the HOTP slot with the given number if Num Lock /// is pressed. The slot number must be 0, 1 or 2. - pub capslock: Option<u8>, - /// If set, the stick will generate a code from the HOTP slot with the given number if - /// scrollock is pressed. The slot number must be 0, 1 or 2. - pub scrollock: Option<u8>, + pub num_lock: Option<u8>, + /// If set, the stick will generate a code from the HOTP slot with the given number if Caps + /// Lock is pressed. The slot number must be 0, 1 or 2. + pub caps_lock: Option<u8>, + /// If set, the stick will generate a code from the HOTP slot with the given number if Scroll + /// Lock is pressed. The slot number must be 0, 1 or 2. + pub scroll_lock: Option<u8>, /// If set, OTP generation using [`get_hotp_code`][] or [`get_totp_code`][] requires user /// authentication. Otherwise, OTPs can be generated without authentication. /// @@ -25,9 +27,9 @@ pub struct Config { #[derive(Debug)] pub struct RawConfig { - pub numlock: u8, - pub capslock: u8, - pub scrollock: u8, + pub num_lock: u8, + pub caps_lock: u8, + pub scroll_lock: u8, pub user_password: bool, } @@ -54,38 +56,40 @@ fn option_to_config_otp_slot(value: Option<u8>) -> Result<u8, Error> { impl Config { /// Constructs a new instance of this struct. pub fn new( - numlock: Option<u8>, - capslock: Option<u8>, - scrollock: Option<u8>, + num_lock: Option<u8>, + caps_lock: Option<u8>, + scroll_lock: Option<u8>, user_password: bool, ) -> Config { Config { - numlock, - capslock, - scrollock, + num_lock, + caps_lock, + scroll_lock, user_password, } } } -impl RawConfig { - pub fn try_from(config: Config) -> Result<RawConfig, Error> { +impl convert::TryFrom<Config> for RawConfig { + type Error = Error; + + fn try_from(config: Config) -> Result<RawConfig, Error> { Ok(RawConfig { - numlock: option_to_config_otp_slot(config.numlock)?, - capslock: option_to_config_otp_slot(config.capslock)?, - scrollock: option_to_config_otp_slot(config.scrollock)?, + num_lock: option_to_config_otp_slot(config.num_lock)?, + caps_lock: option_to_config_otp_slot(config.caps_lock)?, + scroll_lock: option_to_config_otp_slot(config.scroll_lock)?, user_password: config.user_password, }) } } -impl From<[u8; 5]> for RawConfig { - fn from(data: [u8; 5]) -> Self { - RawConfig { - numlock: data[0], - capslock: data[1], - scrollock: data[2], - user_password: data[3] != 0, +impl From<&nitrokey_sys::NK_status> for RawConfig { + fn from(status: &nitrokey_sys::NK_status) -> Self { + Self { + num_lock: status.config_numlock, + caps_lock: status.config_capslock, + scroll_lock: status.config_scrolllock, + user_password: status.otp_user_password, } } } @@ -93,9 +97,9 @@ impl From<[u8; 5]> for RawConfig { impl Into<Config> for RawConfig { fn into(self) -> Config { Config { - numlock: config_otp_slot_to_option(self.numlock), - capslock: config_otp_slot_to_option(self.capslock), - scrollock: config_otp_slot_to_option(self.scrollock), + num_lock: config_otp_slot_to_option(self.num_lock), + caps_lock: config_otp_slot_to_option(self.caps_lock), + scroll_lock: config_otp_slot_to_option(self.scroll_lock), user_password: self.user_password, } } |