aboutsummaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs49
1 files changed, 28 insertions, 21 deletions
diff --git a/src/config.rs b/src/config.rs
index 2ce6f77..120a51b 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,4 +1,9 @@
-use crate::util::CommandError;
+// 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)]
@@ -30,21 +35,21 @@ pub struct RawConfig {
fn config_otp_slot_to_option(value: u8) -> Option<u8> {
if value < 3 {
- return Some(value);
+ Some(value)
+ } else {
+ None
}
- None
}
-fn option_to_config_otp_slot(value: Option<u8>) -> Result<u8, CommandError> {
- match value {
- Some(value) => {
- if value < 3 {
- Ok(value)
- } else {
- Err(CommandError::InvalidSlot)
- }
+fn option_to_config_otp_slot(value: Option<u8>) -> Result<u8, Error> {
+ if let Some(value) = value {
+ if value < 3 {
+ Ok(value)
+ } else {
+ Err(LibraryError::InvalidSlot.into())
}
- None => Ok(255),
+ } else {
+ Ok(255)
}
}
@@ -65,8 +70,10 @@ impl Config {
}
}
-impl RawConfig {
- pub fn try_from(config: Config) -> Result<RawConfig, CommandError> {
+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)?,
@@ -76,13 +83,13 @@ impl RawConfig {
}
}
-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 {
+ numlock: status.config_numlock,
+ capslock: status.config_capslock,
+ scrollock: status.config_scrolllock,
+ user_password: status.otp_user_password,
}
}
}