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/otp.rs | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'src/otp.rs') diff --git a/src/otp.rs b/src/otp.rs index b89d2bf..1094520 100644 --- a/src/otp.rs +++ b/src/otp.rs @@ -1,6 +1,6 @@ use nitrokey_sys; use std::ffi::CString; -use util::{result_from_string, CommandError, CommandStatus}; +use util::{get_command_result, result_from_string, CommandError}; /// Modes for one-time password generation. #[derive(Debug, PartialEq)] @@ -25,7 +25,7 @@ pub trait ConfigureOtp { /// # Example /// /// ```no_run - /// use nitrokey::{Authenticate, CommandStatus, ConfigureOtp, OtpMode, OtpSlotData}; + /// use nitrokey::{Authenticate, ConfigureOtp, OtpMode, OtpSlotData}; /// # use nitrokey::CommandError; /// /// # fn try_main() -> Result<(), (CommandError)> { @@ -34,8 +34,8 @@ pub trait ConfigureOtp { /// match device.authenticate_admin("12345678") { /// Ok(admin) => { /// match admin.write_hotp_slot(slot_data, 0) { - /// CommandStatus::Success => println!("Successfully wrote slot."), - /// CommandStatus::Error(err) => println!("Could not write slot: {:?}", err), + /// Ok(()) => println!("Successfully wrote slot."), + /// Err(err) => println!("Could not write slot: {:?}", err), /// } /// }, /// Err((_, err)) => println!("Could not authenticate as admin: {:?}", err), @@ -47,7 +47,7 @@ pub trait ConfigureOtp { /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString /// [`NoName`]: enum.CommandError.html#variant.NoName - fn write_hotp_slot(&self, data: OtpSlotData, counter: u64) -> CommandStatus; + fn write_hotp_slot(&self, data: OtpSlotData, counter: u64) -> Result<(), CommandError>; /// Configure a TOTP slot with the given data and set the TOTP time window to the given value /// (default 30). @@ -61,7 +61,7 @@ pub trait ConfigureOtp { /// # Example /// /// ```no_run - /// use nitrokey::{Authenticate, CommandStatus, ConfigureOtp, OtpMode, OtpSlotData}; + /// use nitrokey::{Authenticate, ConfigureOtp, OtpMode, OtpSlotData}; /// # use nitrokey::CommandError; /// /// # fn try_main() -> Result<(), (CommandError)> { @@ -70,8 +70,8 @@ pub trait ConfigureOtp { /// match device.authenticate_admin("12345678") { /// Ok(admin) => { /// match admin.write_totp_slot(slot_data, 30) { - /// CommandStatus::Success => println!("Successfully wrote slot."), - /// CommandStatus::Error(err) => println!("Could not write slot: {:?}", err), + /// Ok(()) => println!("Successfully wrote slot."), + /// Err(err) => println!("Could not write slot: {:?}", err), /// } /// }, /// Err((_, err)) => println!("Could not authenticate as admin: {:?}", err), @@ -83,7 +83,7 @@ pub trait ConfigureOtp { /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString /// [`NoName`]: enum.CommandError.html#variant.NoName - fn write_totp_slot(&self, data: OtpSlotData, time_window: u16) -> CommandStatus; + fn write_totp_slot(&self, data: OtpSlotData, time_window: u16) -> Result<(), CommandError>; /// Erases an HOTP slot. /// @@ -94,7 +94,7 @@ pub trait ConfigureOtp { /// # Example /// /// ```no_run - /// use nitrokey::{Authenticate, CommandStatus, ConfigureOtp}; + /// use nitrokey::{Authenticate, ConfigureOtp}; /// # use nitrokey::CommandError; /// /// # fn try_main() -> Result<(), (CommandError)> { @@ -102,8 +102,8 @@ pub trait ConfigureOtp { /// match device.authenticate_admin("12345678") { /// Ok(admin) => { /// match admin.erase_hotp_slot(1) { - /// CommandStatus::Success => println!("Successfully erased slot."), - /// CommandStatus::Error(err) => println!("Could not erase slot: {:?}", err), + /// Ok(()) => println!("Successfully erased slot."), + /// Err(err) => println!("Could not erase slot: {:?}", err), /// } /// }, /// Err((_, err)) => println!("Could not authenticate as admin: {:?}", err), @@ -113,7 +113,7 @@ pub trait ConfigureOtp { /// ``` /// /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot - fn erase_hotp_slot(&self, slot: u8) -> CommandStatus; + fn erase_hotp_slot(&self, slot: u8) -> Result<(), CommandError>; /// Erases a TOTP slot. /// @@ -124,7 +124,7 @@ pub trait ConfigureOtp { /// # Example /// /// ```no_run - /// use nitrokey::{Authenticate, CommandStatus, ConfigureOtp}; + /// use nitrokey::{Authenticate, ConfigureOtp}; /// # use nitrokey::CommandError; /// /// # fn try_main() -> Result<(), (CommandError)> { @@ -132,8 +132,8 @@ pub trait ConfigureOtp { /// match device.authenticate_admin("12345678") { /// Ok(admin) => { /// match admin.erase_totp_slot(1) { - /// CommandStatus::Success => println!("Successfully erased slot."), - /// CommandStatus::Error(err) => println!("Could not erase slot: {:?}", err), + /// Ok(()) => println!("Successfully erased slot."), + /// Err(err) => println!("Could not erase slot: {:?}", err), /// } /// }, /// Err((_, err)) => println!("Could not authenticate as admin: {:?}", err), @@ -143,7 +143,7 @@ pub trait ConfigureOtp { /// ``` /// /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot - fn erase_totp_slot(&self, slot: u8) -> CommandStatus; + fn erase_totp_slot(&self, slot: u8) -> Result<(), CommandError>; } /// Provides methods to generate OTP codes and to query OTP slots on a Nitrokey @@ -181,8 +181,8 @@ pub trait GenerateOtp { /// /// [`get_totp_code`]: #method.get_totp_code /// [`Timestamp`]: enum.CommandError.html#variant.Timestamp - fn set_time(&self, time: u64) -> CommandStatus { - unsafe { CommandStatus::from(nitrokey_sys::NK_totp_set_time(time)) } + fn set_time(&self, time: u64) -> Result<(), CommandError> { + unsafe { get_command_result(nitrokey_sys::NK_totp_set_time(time)) } } /// Returns the name of the given HOTP slot. -- cgit v1.2.1