diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2018-06-07 03:03:29 +0200 |
---|---|---|
committer | Robin Krahl <robin.krahl@ireas.org> | 2018-06-07 03:03:29 +0200 |
commit | f035857d9a9dc14c85e6bdf22cbe72528235657d (patch) | |
tree | b6ec10e06a025a8b226e882c4ee39cc802c67665 /src/pws.rs | |
parent | bbe6740d0a0b3f4dd8c1593272a747cfbbb3006b (diff) | |
download | nitrokey-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/pws.rs')
-rw-r--r-- | src/pws.rs | 33 |
1 files changed, 18 insertions, 15 deletions
@@ -2,7 +2,7 @@ use device::{Device, DeviceWrapper, Pro, Storage}; use libc; use nitrokey_sys; use std::ffi::CString; -use util::{get_last_error, result_from_string, CommandError, CommandStatus}; +use util::{get_command_result, get_last_error, result_from_string, CommandError}; /// The number of slots in a [`PasswordSafe`][]. /// @@ -111,15 +111,12 @@ fn get_password_safe<'a>( return Err(CommandError::InvalidString); } let user_pin_string = user_pin_string.unwrap(); - let status = unsafe { - CommandStatus::from(nitrokey_sys::NK_enable_password_safe( + let result = unsafe { + get_command_result(nitrokey_sys::NK_enable_password_safe( user_pin_string.as_ptr(), )) }; - match status { - CommandStatus::Success => Ok(PasswordSafe { _device: device }), - CommandStatus::Error(err) => Err(err), - } + result.map(|()| PasswordSafe { _device: device }) } impl<'a> PasswordSafe<'a> { @@ -283,19 +280,25 @@ impl<'a> PasswordSafe<'a> { /// /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString - pub fn write_slot(&self, slot: u8, name: &str, login: &str, password: &str) -> CommandStatus { + pub fn write_slot( + &self, + slot: u8, + name: &str, + login: &str, + password: &str, + ) -> Result<(), CommandError> { let name_string = CString::new(name); let login_string = CString::new(login); let password_string = CString::new(password); if name_string.is_err() || login_string.is_err() || password_string.is_err() { - return CommandStatus::Error(CommandError::InvalidString); + return Err(CommandError::InvalidString); } let name_string = name_string.unwrap(); let login_string = login_string.unwrap(); let password_string = password_string.unwrap(); unsafe { - CommandStatus::from(nitrokey_sys::NK_write_password_safe_slot( + get_command_result(nitrokey_sys::NK_write_password_safe_slot( slot, name_string.as_ptr(), login_string.as_ptr(), @@ -314,23 +317,23 @@ impl<'a> PasswordSafe<'a> { /// # Example /// /// ```no_run - /// use nitrokey::{CommandStatus, GetPasswordSafe}; + /// use nitrokey::GetPasswordSafe; /// # use nitrokey::CommandError; /// /// # fn try_main() -> Result<(), CommandError> { /// let device = nitrokey::connect()?; /// let pws = device.get_password_safe("123456")?; /// match pws.erase_slot(0) { - /// CommandStatus::Success => println!("Erased slot 0."), - /// CommandStatus::Error(err) => println!("Could not erase slot 0: {:?}", err), + /// Ok(()) => println!("Erased slot 0."), + /// Err(err) => println!("Could not erase slot 0: {:?}", err), /// }; /// # Ok(()) /// # } /// ``` /// /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot - pub fn erase_slot(&self, slot: u8) -> CommandStatus { - unsafe { CommandStatus::from(nitrokey_sys::NK_erase_password_safe_slot(slot)) } + pub fn erase_slot(&self, slot: u8) -> Result<(), CommandError> { + unsafe { get_command_result(nitrokey_sys::NK_erase_password_safe_slot(slot)) } } } |