aboutsummaryrefslogtreecommitdiff
path: root/src/device.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/device.rs')
-rw-r--r--src/device.rs72
1 files changed, 35 insertions, 37 deletions
diff --git a/src/device.rs b/src/device.rs
index f901306..c4e83a9 100644
--- a/src/device.rs
+++ b/src/device.rs
@@ -5,7 +5,7 @@ use nitrokey_sys;
use otp::GenerateOtp;
use pws::GetPasswordSafe;
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};
/// Available Nitrokey models.
#[derive(Debug, PartialEq)]
@@ -296,14 +296,14 @@ pub trait Device: Authenticate + GetPasswordSafe + GenerateOtp {
/// # Example
///
/// ```no_run
- /// use nitrokey::{CommandStatus, Device};
+ /// use nitrokey::Device;
/// # use nitrokey::CommandError;
///
/// # fn try_main() -> Result<(), CommandError> {
/// let device = nitrokey::connect()?;
/// match device.change_admin_pin("12345678", "12345679") {
- /// CommandStatus::Success => println!("Updated admin PIN."),
- /// CommandStatus::Error(err) => println!("Failed to update admin PIN: {:?}", err),
+ /// Ok(()) => println!("Updated admin PIN."),
+ /// Err(err) => println!("Failed to update admin PIN: {:?}", err),
/// };
/// # Ok(())
/// # }
@@ -311,16 +311,16 @@ pub trait Device: Authenticate + GetPasswordSafe + GenerateOtp {
///
/// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
- fn change_admin_pin(&self, current: &str, new: &str) -> CommandStatus {
+ fn change_admin_pin(&self, current: &str, new: &str) -> Result<(), CommandError> {
let current_string = CString::new(current);
let new_string = CString::new(new);
if current_string.is_err() || new_string.is_err() {
- return CommandStatus::Error(CommandError::InvalidString);
+ return Err(CommandError::InvalidString);
}
let current_string = current_string.unwrap();
let new_string = new_string.unwrap();
unsafe {
- CommandStatus::from(nitrokey_sys::NK_change_admin_PIN(
+ get_command_result(nitrokey_sys::NK_change_admin_PIN(
current_string.as_ptr(),
new_string.as_ptr(),
))
@@ -337,14 +337,14 @@ pub trait Device: Authenticate + GetPasswordSafe + GenerateOtp {
/// # Example
///
/// ```no_run
- /// use nitrokey::{CommandStatus, Device};
+ /// use nitrokey::Device;
/// # use nitrokey::CommandError;
///
/// # fn try_main() -> Result<(), CommandError> {
/// let device = nitrokey::connect()?;
/// match device.change_user_pin("123456", "123457") {
- /// CommandStatus::Success => println!("Updated admin PIN."),
- /// CommandStatus::Error(err) => println!("Failed to update admin PIN: {:?}", err),
+ /// Ok(()) => println!("Updated admin PIN."),
+ /// Err(err) => println!("Failed to update admin PIN: {:?}", err),
/// };
/// # Ok(())
/// # }
@@ -352,16 +352,16 @@ pub trait Device: Authenticate + GetPasswordSafe + GenerateOtp {
///
/// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
- fn change_user_pin(&self, current: &str, new: &str) -> CommandStatus {
+ fn change_user_pin(&self, current: &str, new: &str) -> Result<(), CommandError> {
let current_string = CString::new(current);
let new_string = CString::new(new);
if current_string.is_err() || new_string.is_err() {
- return CommandStatus::Error(CommandError::InvalidString);
+ return Err(CommandError::InvalidString);
}
let current_string = current_string.unwrap();
let new_string = new_string.unwrap();
unsafe {
- CommandStatus::from(nitrokey_sys::NK_change_user_PIN(
+ get_command_result(nitrokey_sys::NK_change_user_PIN(
current_string.as_ptr(),
new_string.as_ptr(),
))
@@ -378,14 +378,14 @@ pub trait Device: Authenticate + GetPasswordSafe + GenerateOtp {
/// # Example
///
/// ```no_run
- /// use nitrokey::{CommandStatus, Device};
+ /// use nitrokey::Device;
/// # use nitrokey::CommandError;
///
/// # fn try_main() -> Result<(), CommandError> {
/// let device = nitrokey::connect()?;
/// match device.unlock_user_pin("12345678", "123456") {
- /// CommandStatus::Success => println!("Unlocked user PIN."),
- /// CommandStatus::Error(err) => println!("Failed to unlock user PIN: {:?}", err),
+ /// Ok(()) => println!("Unlocked user PIN."),
+ /// Err(err) => println!("Failed to unlock user PIN: {:?}", err),
/// };
/// # Ok(())
/// # }
@@ -393,16 +393,16 @@ pub trait Device: Authenticate + GetPasswordSafe + GenerateOtp {
///
/// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
- fn unlock_user_pin(&self, admin_pin: &str, user_pin: &str) -> CommandStatus {
+ fn unlock_user_pin(&self, admin_pin: &str, user_pin: &str) -> Result<(), CommandError> {
let admin_pin_string = CString::new(admin_pin);
let user_pin_string = CString::new(user_pin);
if admin_pin_string.is_err() || user_pin_string.is_err() {
- return CommandStatus::Error(CommandError::InvalidString);
+ return Err(CommandError::InvalidString);
}
let admin_pin_string = admin_pin_string.unwrap();
let user_pin_string = user_pin_string.unwrap();
unsafe {
- CommandStatus::from(nitrokey_sys::NK_unlock_user_password(
+ get_command_result(nitrokey_sys::NK_unlock_user_password(
admin_pin_string.as_ptr(),
user_pin_string.as_ptr(),
))
@@ -417,20 +417,20 @@ pub trait Device: Authenticate + GetPasswordSafe + GenerateOtp {
/// # Example
///
/// ```no_run
- /// use nitrokey::{CommandStatus, Device};
+ /// use nitrokey::Device;
/// # use nitrokey::CommandError;
///
/// # fn try_main() -> Result<(), CommandError> {
/// let device = nitrokey::connect()?;
/// match device.lock() {
- /// CommandStatus::Success => println!("Locked the Nitrokey device."),
- /// CommandStatus::Error(err) => println!("Could not lock the Nitrokey device: {:?}", err),
+ /// Ok(()) => println!("Locked the Nitrokey device."),
+ /// Err(err) => println!("Could not lock the Nitrokey device: {:?}", err),
/// };
/// # Ok(())
/// # }
/// ```
- fn lock(&self) -> CommandStatus {
- unsafe { CommandStatus::from(nitrokey_sys::NK_lock_device()) }
+ fn lock(&self) -> Result<(), CommandError> {
+ unsafe { get_command_result(nitrokey_sys::NK_lock_device()) }
}
}
@@ -547,14 +547,13 @@ impl Storage {
/// # Example
///
/// ```no_run
- /// use nitrokey::{CommandStatus};
/// # use nitrokey::CommandError;
///
/// # fn try_main() -> Result<(), CommandError> {
/// let device = nitrokey::Storage::connect()?;
/// match device.enable_encrypted_volume("123456") {
- /// CommandStatus::Success => println!("Enabled the encrypted volume."),
- /// CommandStatus::Error(err) => println!("Could not enable the encrypted volume: {:?}", err),
+ /// Ok(()) => println!("Enabled the encrypted volume."),
+ /// Err(err) => println!("Could not enable the encrypted volume: {:?}", err),
/// };
/// # Ok(())
/// # }
@@ -562,13 +561,13 @@ impl Storage {
///
/// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
- pub fn enable_encrypted_volume(&self, user_pin: &str) -> CommandStatus {
+ pub fn enable_encrypted_volume(&self, user_pin: &str) -> Result<(), CommandError> {
let user_pin = CString::new(user_pin);
if user_pin.is_err() {
- return CommandStatus::Error(CommandError::InvalidString);
+ return Err(CommandError::InvalidString);
}
let user_pin = user_pin.unwrap();
- unsafe { CommandStatus::from(nitrokey_sys::NK_unlock_encrypted_volume(user_pin.as_ptr())) }
+ unsafe { get_command_result(nitrokey_sys::NK_unlock_encrypted_volume(user_pin.as_ptr())) }
}
/// Disables the encrypted storage volume.
@@ -579,7 +578,6 @@ impl Storage {
/// # Example
///
/// ```no_run
- /// use nitrokey::{CommandStatus};
/// # use nitrokey::CommandError;
///
/// fn use_volume() {}
@@ -587,17 +585,17 @@ impl Storage {
/// # fn try_main() -> Result<(), CommandError> {
/// let device = nitrokey::Storage::connect()?;
/// match device.enable_encrypted_volume("123456") {
- /// CommandStatus::Success => {
+ /// Ok(()) => {
/// println!("Enabled the encrypted volume.");
/// use_volume();
/// match device.disable_encrypted_volume() {
- /// CommandStatus::Success => println!("Disabled the encrypted volume."),
- /// CommandStatus::Err(err) => {
+ /// Ok(()) => println!("Disabled the encrypted volume."),
+ /// Err(err) => {
/// println!("Could not disable the encrypted volume: {:?}", err);
/// },
/// };
/// },
- /// CommandStatus::Error(err) => println!("Could not enable the encrypted volume: {:?}", err),
+ /// Err(err) => println!("Could not enable the encrypted volume: {:?}", err),
/// };
/// # Ok(())
/// # }
@@ -605,8 +603,8 @@ impl Storage {
///
/// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
- pub fn disable_encrypted_volume(&self) -> CommandStatus {
- unsafe { CommandStatus::from(nitrokey_sys::NK_lock_encrypted_volume()) }
+ pub fn disable_encrypted_volume(&self) -> Result<(), CommandError> {
+ unsafe { get_command_result(nitrokey_sys::NK_lock_encrypted_volume()) }
}
}