From f035857d9a9dc14c85e6bdf22cbe72528235657d Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Thu, 7 Jun 2018 03:03:29 +0200 Subject: 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 ?. --- src/auth.rs | 44 ++++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 26 deletions(-) (limited to 'src/auth.rs') 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 Admin { /// ``` /// /// [`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(&self, data: OtpSlotData, callback: C) -> CommandStatus + fn write_otp_slot(&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 ConfigureOtp for Admin { - 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 ConfigureOtp for Admin { 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 ConfigureOtp for Admin { 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)) } } } -- cgit v1.2.1