aboutsummaryrefslogtreecommitdiff
path: root/src/util.rs
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2019-01-20 20:58:18 +0000
committerRobin Krahl <robin.krahl@ireas.org>2019-01-20 21:08:20 +0000
commit94390aadc8a3997d379bf5e4c0bc00c2a9669a34 (patch)
treea22cb82e8d6450ad034230d77e2db6605e54df8b /src/util.rs
parentdb198936be1a80f1735731d9e95eb6f4c48a5329 (diff)
downloadnitrokey-rs-94390aadc8a3997d379bf5e4c0bc00c2a9669a34.tar.gz
nitrokey-rs-94390aadc8a3997d379bf5e4c0bc00c2a9669a34.tar.bz2
Return Error instead of CommandError
This patch changes all public functions to return the Error enum instead of the CommandError enum. This breaks the tests which will be fixed with the next patch. This patch also adds a placeholder variant Error::CommandError and a placeholder enum CommandError to make the transition to a new nitrokey-test version easier.
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs24
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 {