From f035857d9a9dc14c85e6bdf22cbe72528235657d Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Thu, 7 Jun 2018 03:03:29 +0200 Subject: 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 ?. --- src/util.rs | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-) (limited to 'src/util.rs') 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 { } } -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 for CommandError { } } -impl From for CommandStatus { - fn from(value: c_int) -> Self { - match value { - 0 => CommandStatus::Success, - other => CommandStatus::Error(CommandError::from(other)), - } - } -} - impl Into for LogLevel { fn into(self) -> i32 { match self { -- cgit v1.2.1