aboutsummaryrefslogtreecommitdiff
path: root/src/util.rs
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2018-06-07 03:21:57 +0200
committerRobin Krahl <robin.krahl@ireas.org>2018-06-07 03:21:57 +0200
commitd66616a4fa71609231688119c40a3a0ec39a17ab (patch)
tree8bf35b826e5d4a1b78902310f6bff3dc0dfecc21 /src/util.rs
parentf035857d9a9dc14c85e6bdf22cbe72528235657d (diff)
downloadnitrokey-rs-d66616a4fa71609231688119c40a3a0ec39a17ab.tar.gz
nitrokey-rs-d66616a4fa71609231688119c40a3a0ec39a17ab.tar.bz2
Implement easier CString creation
The new get_cstring method in util returns a Result<CString, CommandError>, so mast callers can just use the ? operator to unwrap the result instead of cumbersome unwrapping code.
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs6
1 files changed, 5 insertions, 1 deletions
diff --git a/src/util.rs b/src/util.rs
index 1608952..75de799 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -2,7 +2,7 @@ use libc::{c_void, free};
use nitrokey_sys;
use rand::{OsRng, Rng};
use std;
-use std::ffi::CStr;
+use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_int};
/// Error types returned by Nitrokey device or by the library.
@@ -110,6 +110,10 @@ pub fn generate_password(length: usize) -> std::io::Result<Vec<u8>> {
return Ok(data);
}
+pub fn get_cstring<T: Into<Vec<u8>>>(s: T) -> Result<CString, CommandError> {
+ CString::new(s).or(Err(CommandError::InvalidString))
+}
+
impl From<c_int> for CommandError {
fn from(value: c_int) -> Self {
match value {