aboutsummaryrefslogtreecommitdiff
path: root/src/auth.rs
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2018-06-07 03:03:29 +0200
committerRobin Krahl <robin.krahl@ireas.org>2018-06-07 03:03:29 +0200
commitf035857d9a9dc14c85e6bdf22cbe72528235657d (patch)
treeb6ec10e06a025a8b226e882c4ee39cc802c67665 /src/auth.rs
parentbbe6740d0a0b3f4dd8c1593272a747cfbbb3006b (diff)
downloadnitrokey-rs-f035857d9a9dc14c85e6bdf22cbe72528235657d.tar.gz
nitrokey-rs-f035857d9a9dc14c85e6bdf22cbe72528235657d.tar.bz2
Use Result<(), CommandError> instead of CommandStatus
The Result enum is more idiomatic and easier to use than our custom CommandStatus enum with the same structure. This is especially true for the try operator ?.
Diffstat (limited to 'src/auth.rs')
-rw-r--r--src/auth.rs44
1 files changed, 18 insertions, 26 deletions
diff --git a/src/auth.rs b/src/auth.rs
index 6a0a71f..62b851f 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -5,7 +5,7 @@ use otp::{ConfigureOtp, GenerateOtp, OtpMode, OtpSlotData, RawOtpSlotData};
use std::ffi::CString;
use std::ops::Deref;
use std::os::raw::c_int;
-use util::{generate_password, result_from_string, CommandError, CommandStatus};
+use util::{generate_password, get_command_result, result_from_string, CommandError};
static TEMPORARY_PASSWORD_LENGTH: usize = 25;
@@ -287,41 +287,33 @@ impl<T: Device> Admin<T> {
/// ```
///
/// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
- pub fn write_config(&self, config: Config) -> CommandStatus {
- let raw_config = match RawConfig::try_from(config) {
- Ok(raw_config) => raw_config,
- Err(err) => return CommandStatus::Error(err),
- };
+ pub fn write_config(&self, config: Config) -> Result<(), CommandError> {
+ let raw_config = RawConfig::try_from(config)?;
unsafe {
- let rv = nitrokey_sys::NK_write_config(
+ get_command_result(nitrokey_sys::NK_write_config(
raw_config.numlock,
raw_config.capslock,
raw_config.scrollock,
raw_config.user_password,
false,
self.temp_password.as_ptr() as *const i8,
- );
- return CommandStatus::from(rv);
+ ))
}
}
- fn write_otp_slot<C>(&self, data: OtpSlotData, callback: C) -> CommandStatus
+ fn write_otp_slot<C>(&self, data: OtpSlotData, callback: C) -> Result<(), CommandError>
where
C: Fn(RawOtpSlotData, *const i8) -> c_int,
{
- let raw_data = match RawOtpSlotData::new(data) {
- Ok(raw_data) => raw_data,
- Err(err) => return CommandStatus::Error(err),
- };
+ let raw_data = RawOtpSlotData::new(data)?;
let temp_password_ptr = self.temp_password.as_ptr() as *const i8;
- let rv = callback(raw_data, temp_password_ptr);
- return CommandStatus::from(rv);
+ get_command_result(callback(raw_data, temp_password_ptr))
}
}
impl<T: Device> ConfigureOtp for Admin<T> {
- fn write_hotp_slot(&self, data: OtpSlotData, counter: u64) -> CommandStatus {
- return self.write_otp_slot(data, |raw_data: RawOtpSlotData, temp_password_ptr| unsafe {
+ fn write_hotp_slot(&self, data: OtpSlotData, counter: u64) -> Result<(), CommandError> {
+ self.write_otp_slot(data, |raw_data: RawOtpSlotData, temp_password_ptr| unsafe {
nitrokey_sys::NK_write_hotp_slot(
raw_data.number,
raw_data.name.as_ptr(),
@@ -333,11 +325,11 @@ impl<T: Device> ConfigureOtp for Admin<T> {
raw_data.token_id.as_ptr(),
temp_password_ptr,
)
- });
+ })
}
- fn write_totp_slot(&self, data: OtpSlotData, time_window: u16) -> CommandStatus {
- return self.write_otp_slot(data, |raw_data: RawOtpSlotData, temp_password_ptr| unsafe {
+ fn write_totp_slot(&self, data: OtpSlotData, time_window: u16) -> Result<(), CommandError> {
+ self.write_otp_slot(data, |raw_data: RawOtpSlotData, temp_password_ptr| unsafe {
nitrokey_sys::NK_write_totp_slot(
raw_data.number,
raw_data.name.as_ptr(),
@@ -349,17 +341,17 @@ impl<T: Device> ConfigureOtp for Admin<T> {
raw_data.token_id.as_ptr(),
temp_password_ptr,
)
- });
+ })
}
- fn erase_hotp_slot(&self, slot: u8) -> CommandStatus {
+ fn erase_hotp_slot(&self, slot: u8) -> Result<(), CommandError> {
let temp_password_ptr = self.temp_password.as_ptr() as *const i8;
- unsafe { CommandStatus::from(nitrokey_sys::NK_erase_hotp_slot(slot, temp_password_ptr)) }
+ unsafe { get_command_result(nitrokey_sys::NK_erase_hotp_slot(slot, temp_password_ptr)) }
}
- fn erase_totp_slot(&self, slot: u8) -> CommandStatus {
+ fn erase_totp_slot(&self, slot: u8) -> Result<(), CommandError> {
let temp_password_ptr = self.temp_password.as_ptr() as *const i8;
- unsafe { CommandStatus::from(nitrokey_sys::NK_erase_totp_slot(slot, temp_password_ptr)) }
+ unsafe { get_command_result(nitrokey_sys::NK_erase_totp_slot(slot, temp_password_ptr)) }
}
}