summaryrefslogtreecommitdiff
path: root/src/pws.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/pws.rs')
-rw-r--r--src/pws.rs33
1 files changed, 18 insertions, 15 deletions
diff --git a/src/pws.rs b/src/pws.rs
index 903ecef..bbc19aa 100644
--- a/src/pws.rs
+++ b/src/pws.rs
@@ -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)) }
}
}