diff options
Diffstat (limited to 'src/util.rs')
-rw-r--r-- | src/util.rs | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/src/util.rs b/src/util.rs index 88a381c..8855275 100644 --- a/src/util.rs +++ b/src/util.rs @@ -5,7 +5,7 @@ use libc::{c_void, free}; use rand_core::RngCore; use rand_os::OsRng; -use crate::error::CommandError; +use crate::error::{CommandError, Error}; /// Log level for libnitrokey. /// @@ -34,9 +34,9 @@ pub fn owned_str_from_ptr(ptr: *const c_char) -> String { } } -pub fn result_from_string(ptr: *const c_char) -> Result<String, CommandError> { +pub fn result_from_string(ptr: *const c_char) -> Result<String, Error> { if ptr.is_null() { - return Err(CommandError::Undefined); + return Err(CommandError::Undefined.into()); } unsafe { let s = owned_str_from_ptr(ptr); @@ -51,34 +51,34 @@ pub fn result_from_string(ptr: *const c_char) -> Result<String, CommandError> { } } -pub fn get_command_result(value: c_int) -> Result<(), CommandError> { +pub fn get_command_result(value: c_int) -> Result<(), Error> { match value { 0 => Ok(()), - other => Err(CommandError::from(other)), + other => Err(CommandError::from(other).into()), } } -pub fn get_last_result() -> Result<(), CommandError> { +pub fn get_last_result() -> Result<(), Error> { let value = unsafe { nitrokey_sys::NK_get_last_command_status() } as c_int; get_command_result(value) } -pub fn get_last_error() -> CommandError { +pub fn get_last_error() -> Error { return match get_last_result() { - Ok(()) => CommandError::Undefined, + Ok(()) => CommandError::Undefined.into(), Err(err) => err, }; } -pub fn generate_password(length: usize) -> Result<Vec<u8>, CommandError> { - let mut rng = OsRng::new()?; +pub fn generate_password(length: usize) -> Result<Vec<u8>, Error> { + let mut rng = OsRng::new().map_err(CommandError::from)?; let mut data = vec![0u8; length]; rng.fill_bytes(&mut data[..]); Ok(data) } -pub fn get_cstring<T: Into<Vec<u8>>>(s: T) -> Result<CString, CommandError> { - CString::new(s).or(Err(CommandError::InvalidString)) +pub fn get_cstring<T: Into<Vec<u8>>>(s: T) -> Result<CString, Error> { + CString::new(s).or(Err(CommandError::InvalidString.into())) } impl Into<i32> for LogLevel { |