summaryrefslogtreecommitdiff
path: root/src/pws.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/pws.rs')
-rw-r--r--src/pws.rs62
1 files changed, 31 insertions, 31 deletions
diff --git a/src/pws.rs b/src/pws.rs
index 615e47c..e974737 100644
--- a/src/pws.rs
+++ b/src/pws.rs
@@ -2,7 +2,7 @@ use libc;
use nitrokey_sys;
use crate::device::{Device, DeviceWrapper, Pro, Storage};
-use crate::error::CommandError;
+use crate::error::{CommandError, Error};
use crate::util::{get_command_result, get_cstring, get_last_error, result_from_string};
/// The number of slots in a [`PasswordSafe`][].
@@ -29,9 +29,9 @@ pub const SLOT_COUNT: u8 = 16;
///
/// ```no_run
/// use nitrokey::{Device, GetPasswordSafe, PasswordSafe};
-/// # use nitrokey::CommandError;
+/// # use nitrokey::Error;
///
-/// fn use_password_safe(pws: &PasswordSafe) -> Result<(), CommandError> {
+/// fn use_password_safe(pws: &PasswordSafe) -> Result<(), Error> {
/// let name = pws.get_slot_name(0)?;
/// let login = pws.get_slot_login(0)?;
/// let password = pws.get_slot_login(0)?;
@@ -39,7 +39,7 @@ pub const SLOT_COUNT: u8 = 16;
/// Ok(())
/// }
///
-/// # fn try_main() -> Result<(), CommandError> {
+/// # fn try_main() -> Result<(), Error> {
/// let device = nitrokey::connect()?;
/// let pws = device.get_password_safe("123456")?;
/// use_password_safe(&pws);
@@ -88,11 +88,11 @@ pub trait GetPasswordSafe {
///
/// ```no_run
/// use nitrokey::{Device, GetPasswordSafe, PasswordSafe};
- /// # use nitrokey::CommandError;
+ /// # use nitrokey::Error;
///
/// fn use_password_safe(pws: &PasswordSafe) {}
///
- /// # fn try_main() -> Result<(), CommandError> {
+ /// # fn try_main() -> Result<(), Error> {
/// let device = nitrokey::connect()?;
/// match device.get_password_safe("123456") {
/// Ok(pws) => {
@@ -112,13 +112,13 @@ pub trait GetPasswordSafe {
/// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
/// [`Unknown`]: enum.CommandError.html#variant.Unknown
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
- fn get_password_safe(&self, user_pin: &str) -> Result<PasswordSafe<'_>, CommandError>;
+ fn get_password_safe(&self, user_pin: &str) -> Result<PasswordSafe<'_>, Error>;
}
fn get_password_safe<'a>(
device: &'a dyn Device,
user_pin: &str,
-) -> Result<PasswordSafe<'a>, CommandError> {
+) -> Result<PasswordSafe<'a>, Error> {
let user_pin_string = get_cstring(user_pin)?;
let result = unsafe {
get_command_result(nitrokey_sys::NK_enable_password_safe(
@@ -128,9 +128,9 @@ fn get_password_safe<'a>(
result.map(|()| PasswordSafe { _device: device })
}
-fn get_pws_result(s: String) -> Result<String, CommandError> {
+fn get_pws_result(s: String) -> Result<String, Error> {
if s.is_empty() {
- Err(CommandError::SlotNotProgrammed)
+ Err(CommandError::SlotNotProgrammed.into())
} else {
Ok(s)
}
@@ -145,9 +145,9 @@ impl<'a> PasswordSafe<'a> {
///
/// ```no_run
/// use nitrokey::{GetPasswordSafe, SLOT_COUNT};
- /// # use nitrokey::CommandError;
+ /// # use nitrokey::Error;
///
- /// # fn try_main() -> Result<(), CommandError> {
+ /// # fn try_main() -> Result<(), Error> {
/// let device = nitrokey::connect()?;
/// let pws = device.get_password_safe("123456")?;
/// pws.get_slot_status()?.iter().enumerate().for_each(|(slot, programmed)| {
@@ -160,7 +160,7 @@ impl<'a> PasswordSafe<'a> {
/// # Ok(())
/// # }
/// ```
- pub fn get_slot_status(&self) -> Result<[bool; SLOT_COUNT as usize], CommandError> {
+ pub fn get_slot_status(&self) -> Result<[bool; SLOT_COUNT as usize], Error> {
let status_ptr = unsafe { nitrokey_sys::NK_get_password_safe_slot_status() };
if status_ptr.is_null() {
return Err(get_last_error());
@@ -190,9 +190,9 @@ impl<'a> PasswordSafe<'a> {
///
/// ```no_run
/// use nitrokey::GetPasswordSafe;
- /// # use nitrokey::CommandError;
+ /// # use nitrokey::Error;
///
- /// # fn try_main() -> Result<(), CommandError> {
+ /// # fn try_main() -> Result<(), Error> {
/// let device = nitrokey::connect()?;
/// match device.get_password_safe("123456") {
/// Ok(pws) => {
@@ -209,7 +209,7 @@ impl<'a> PasswordSafe<'a> {
///
/// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
/// [`SlotNotProgrammed`]: enum.CommandError.html#variant.SlotNotProgrammed
- pub fn get_slot_name(&self, slot: u8) -> Result<String, CommandError> {
+ pub fn get_slot_name(&self, slot: u8) -> Result<String, Error> {
unsafe { result_from_string(nitrokey_sys::NK_get_password_safe_slot_name(slot)) }
.and_then(get_pws_result)
}
@@ -227,9 +227,9 @@ impl<'a> PasswordSafe<'a> {
///
/// ```no_run
/// use nitrokey::GetPasswordSafe;
- /// # use nitrokey::CommandError;
+ /// # use nitrokey::Error;
///
- /// # fn try_main() -> Result<(), CommandError> {
+ /// # fn try_main() -> Result<(), Error> {
/// let device = nitrokey::connect()?;
/// let pws = device.get_password_safe("123456")?;
/// let name = pws.get_slot_name(0)?;
@@ -242,7 +242,7 @@ impl<'a> PasswordSafe<'a> {
///
/// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
/// [`SlotNotProgrammed`]: enum.CommandError.html#variant.SlotNotProgrammed
- pub fn get_slot_login(&self, slot: u8) -> Result<String, CommandError> {
+ pub fn get_slot_login(&self, slot: u8) -> Result<String, Error> {
unsafe { result_from_string(nitrokey_sys::NK_get_password_safe_slot_login(slot)) }
.and_then(get_pws_result)
}
@@ -260,9 +260,9 @@ impl<'a> PasswordSafe<'a> {
///
/// ```no_run
/// use nitrokey::GetPasswordSafe;
- /// # use nitrokey::CommandError;
+ /// # use nitrokey::Error;
///
- /// # fn try_main() -> Result<(), CommandError> {
+ /// # fn try_main() -> Result<(), Error> {
/// let device = nitrokey::connect()?;
/// let pws = device.get_password_safe("123456")?;
/// let name = pws.get_slot_name(0)?;
@@ -275,7 +275,7 @@ impl<'a> PasswordSafe<'a> {
///
/// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
/// [`SlotNotProgrammed`]: enum.CommandError.html#variant.SlotNotProgrammed
- pub fn get_slot_password(&self, slot: u8) -> Result<String, CommandError> {
+ pub fn get_slot_password(&self, slot: u8) -> Result<String, Error> {
unsafe { result_from_string(nitrokey_sys::NK_get_password_safe_slot_password(slot)) }
.and_then(get_pws_result)
}
@@ -291,9 +291,9 @@ impl<'a> PasswordSafe<'a> {
///
/// ```no_run
/// use nitrokey::GetPasswordSafe;
- /// # use nitrokey::CommandError;
+ /// # use nitrokey::Error;
///
- /// # fn try_main() -> Result<(), CommandError> {
+ /// # fn try_main() -> Result<(), Error> {
/// let device = nitrokey::connect()?;
/// let pws = device.get_password_safe("123456")?;
/// let name = pws.get_slot_name(0)?;
@@ -312,7 +312,7 @@ impl<'a> PasswordSafe<'a> {
name: &str,
login: &str,
password: &str,
- ) -> Result<(), CommandError> {
+ ) -> Result<(), Error> {
let name_string = get_cstring(name)?;
let login_string = get_cstring(login)?;
let password_string = get_cstring(password)?;
@@ -337,9 +337,9 @@ impl<'a> PasswordSafe<'a> {
///
/// ```no_run
/// use nitrokey::GetPasswordSafe;
- /// # use nitrokey::CommandError;
+ /// # use nitrokey::Error;
///
- /// # fn try_main() -> Result<(), CommandError> {
+ /// # fn try_main() -> Result<(), Error> {
/// let device = nitrokey::connect()?;
/// let pws = device.get_password_safe("123456")?;
/// match pws.erase_slot(0) {
@@ -351,7 +351,7 @@ impl<'a> PasswordSafe<'a> {
/// ```
///
/// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
- pub fn erase_slot(&self, slot: u8) -> Result<(), CommandError> {
+ pub fn erase_slot(&self, slot: u8) -> Result<(), Error> {
unsafe { get_command_result(nitrokey_sys::NK_erase_password_safe_slot(slot)) }
}
}
@@ -364,19 +364,19 @@ impl<'a> Drop for PasswordSafe<'a> {
}
impl GetPasswordSafe for Pro {
- fn get_password_safe(&self, user_pin: &str) -> Result<PasswordSafe<'_>, CommandError> {
+ fn get_password_safe(&self, user_pin: &str) -> Result<PasswordSafe<'_>, Error> {
get_password_safe(self, user_pin)
}
}
impl GetPasswordSafe for Storage {
- fn get_password_safe(&self, user_pin: &str) -> Result<PasswordSafe<'_>, CommandError> {
+ fn get_password_safe(&self, user_pin: &str) -> Result<PasswordSafe<'_>, Error> {
get_password_safe(self, user_pin)
}
}
impl GetPasswordSafe for DeviceWrapper {
- fn get_password_safe(&self, user_pin: &str) -> Result<PasswordSafe<'_>, CommandError> {
+ fn get_password_safe(&self, user_pin: &str) -> Result<PasswordSafe<'_>, Error> {
get_password_safe(self, user_pin)
}
}