aboutsummaryrefslogtreecommitdiff
path: root/src/util.rs
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2018-06-07 03:03:29 +0200
committerRobin Krahl <robin.krahl@ireas.org>2018-06-07 03:03:29 +0200
commitf035857d9a9dc14c85e6bdf22cbe72528235657d (patch)
treeb6ec10e06a025a8b226e882c4ee39cc802c67665 /src/util.rs
parentbbe6740d0a0b3f4dd8c1593272a747cfbbb3006b (diff)
downloadnitrokey-rs-f035857d9a9dc14c85e6bdf22cbe72528235657d.tar.gz
nitrokey-rs-f035857d9a9dc14c85e6bdf22cbe72528235657d.tar.bz2
Use Result<(), CommandError> instead of CommandStatus
The Result enum is more idiomatic and easier to use than our custom CommandStatus enum with the same structure. This is especially true for the try operator ?.
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs37
1 files changed, 12 insertions, 25 deletions
diff --git a/src/util.rs b/src/util.rs
index 364b0de..1608952 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -39,15 +39,6 @@ pub enum CommandError {
RngError,
}
-/// Command execution status.
-#[derive(Debug, PartialEq)]
-pub enum CommandStatus {
- /// The command was successful.
- Success,
- /// An error occured during command execution.
- Error(CommandError),
-}
-
/// Log level for libnitrokey.
///
/// Setting the log level to a lower level enables all output from higher levels too. Currently,
@@ -90,17 +81,22 @@ pub fn result_from_string(ptr: *const c_char) -> Result<String, CommandError> {
}
}
-pub fn get_last_status() -> CommandStatus {
- unsafe {
- let status = nitrokey_sys::NK_get_last_command_status();
- return CommandStatus::from(status as c_int);
+pub fn get_command_result(value: c_int) -> Result<(), CommandError> {
+ match value {
+ 0 => Ok(()),
+ other => Err(CommandError::from(other)),
}
}
+pub fn get_last_result() -> Result<(), CommandError> {
+ let value = unsafe { nitrokey_sys::NK_get_last_command_status() } as c_int;
+ get_command_result(value)
+}
+
pub fn get_last_error() -> CommandError {
- return match get_last_status() {
- CommandStatus::Success => CommandError::Unknown,
- CommandStatus::Error(err) => err,
+ return match get_last_result() {
+ Ok(()) => CommandError::Unknown,
+ Err(err) => err,
};
}
@@ -133,15 +129,6 @@ impl From<c_int> for CommandError {
}
}
-impl From<c_int> for CommandStatus {
- fn from(value: c_int) -> Self {
- match value {
- 0 => CommandStatus::Success,
- other => CommandStatus::Error(CommandError::from(other)),
- }
- }
-}
-
impl Into<i32> for LogLevel {
fn into(self) -> i32 {
match self {