diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2019-01-11 01:30:24 +0000 |
---|---|---|
committer | Daniel Mueller <deso@posteo.net> | 2019-01-13 19:43:38 -0800 |
commit | 5bb115f0b2021b0f7a58e064d66e0a6ad67c5654 (patch) | |
tree | 7830af30855ccbaf3a14c44b78b7343d172ec717 | |
parent | d64d379e97a600c08b1e145f6f986aaf95ddcc8f (diff) | |
download | nitrocli-5bb115f0b2021b0f7a58e064d66e0a6ad67c5654.tar.gz nitrocli-5bb115f0b2021b0f7a58e064d66e0a6ad67c5654.tar.bz2 |
Implement From<&str> for Error
This patch implements From<&str> for Error so that we can use
Error::from(s) as a shorthand for Error::Error(s.to_string()). It also
replaces Error::Error with Error::from where possible.
-rw-r--r-- | nitrocli/src/args.rs | 4 | ||||
-rw-r--r-- | nitrocli/src/commands.rs | 34 | ||||
-rw-r--r-- | nitrocli/src/error.rs | 6 | ||||
-rw-r--r-- | nitrocli/src/pinentry.rs | 2 |
4 files changed, 21 insertions, 25 deletions
diff --git a/nitrocli/src/args.rs b/nitrocli/src/args.rs index 246490e..f4e0758 100644 --- a/nitrocli/src/args.rs +++ b/nitrocli/src/args.rs @@ -539,8 +539,8 @@ pub fn otp_set(ctx: &mut ExecCtx<'_>, args: Vec<String>) -> Result<()> { if ascii { if secret_format.is_some() { - return Err(Error::Error( - "The --format and the --ascii option cannot be used at the same time".to_string(), + return Err(Error::from( + "The --format and the --ascii option cannot be used at the same time", )); } diff --git a/nitrocli/src/commands.rs b/nitrocli/src/commands.rs index b37f9c5..209fb8f 100644 --- a/nitrocli/src/commands.rs +++ b/nitrocli/src/commands.rs @@ -62,7 +62,7 @@ fn get_device(ctx: &mut args::ExecCtx<'_>) -> Result<nitrokey::DeviceWrapper> { Some(model) => nitrokey::connect_model(model.into()), None => nitrokey::connect(), } - .map_err(|_| Error::Error("Nitrokey device not found".to_string())) + .map_err(|_| Error::from("Nitrokey device not found")) } /// Connect to a Nitrokey Storage device and return it. @@ -71,17 +71,13 @@ fn get_storage_device(ctx: &mut args::ExecCtx<'_>) -> Result<nitrokey::Storage> if let Some(model) = ctx.model { if model != args::DeviceModel::Storage { - return Err(Error::Error( - "This command is only available on the Nitrokey Storage".to_string(), + return Err(Error::from( + "This command is only available on the Nitrokey Storage", )); } } - nitrokey::Storage::connect().or_else(|_| { - Err(Error::Error( - "Nitrokey Storage device not found".to_string(), - )) - }) + nitrokey::Storage::connect().or_else(|_| Err(Error::from("Nitrokey Storage device not found"))) } /// Open the password safe on the given device. @@ -425,11 +421,7 @@ fn get_otp<T: GenerateOtp>(slot: u8, algorithm: args::OtpAlgorithm, device: &T) fn get_unix_timestamp() -> Result<u64> { time::SystemTime::now() .duration_since(time::UNIX_EPOCH) - .or_else(|_| { - Err(Error::Error( - "Current system time is before the Unix epoch".to_string(), - )) - }) + .or_else(|_| Err(Error::from("Current system time is before the Unix epoch"))) .map(|duration| duration.as_secs()) } @@ -483,8 +475,8 @@ fn prepare_ascii_secret(secret: &str) -> Result<String> { if secret.is_ascii() { Ok(format_bytes(&secret.as_bytes())) } else { - Err(Error::Error( - "The given secret is not an ASCII string despite --format ascii being set".to_string(), + Err(Error::from( + "The given secret is not an ASCII string despite --format ascii being set", )) } } @@ -493,7 +485,7 @@ fn prepare_ascii_secret(secret: &str) -> Result<String> { fn prepare_base32_secret(secret: &str) -> Result<String> { base32::decode(base32::Alphabet::RFC4648 { padding: false }, secret) .map(|vec| format_bytes(&vec)) - .ok_or_else(|| Error::Error("Could not parse base32 secret".to_string())) + .ok_or_else(|| Error::from("Could not parse base32 secret")) } /// Configure a one-time password slot on the Nitrokey device. @@ -552,9 +544,7 @@ fn print_otp_status( slot = match slot.checked_add(1) { Some(slot) => slot, None => { - return Err(Error::Error( - "Integer overflow when iterating OTP slots".to_string(), - )) + return Err(Error::from("Integer overflow when iterating OTP slots")); } }; let name = match result { @@ -614,7 +604,7 @@ fn choose_pin_with_pinentry(pin_type: pinentry::PinType) -> Result<String> { pinentry::clear_pin(pin_type)?; if new_pin != confirm_pin { - Err(Error::Error("Entered PINs do not match".to_string())) + Err(Error::from("Entered PINs do not match")) } else { Ok(new_pin) } @@ -649,7 +639,7 @@ fn choose_pin( if let Some(new_pin) = new_pin { new_pin .to_str() - .ok_or_else(|| Error::Error("Failed to read PIN: invalid Unicode data found".into())) + .ok_or_else(|| Error::from("Failed to read PIN: invalid Unicode data found")) .map(ToOwned::to_owned) } else { choose_pin_with_pinentry(pin_type) @@ -753,7 +743,7 @@ fn print_pws_slot( programmed: bool, ) -> Result<()> { if slot > u8::MAX as usize { - return Err(Error::Error("Invalid PWS slot number".to_string())); + return Err(Error::from("Invalid PWS slot number")); } let slot = slot as u8; let name = if programmed { diff --git a/nitrocli/src/error.rs b/nitrocli/src/error.rs index 78b8148..1346526 100644 --- a/nitrocli/src/error.rs +++ b/nitrocli/src/error.rs @@ -31,6 +31,12 @@ pub enum Error { Error(String), } +impl From<&str> for Error { + fn from(s: &str) -> Error { + Error::Error(s.to_string()) + } +} + impl From<nitrokey::CommandError> for Error { fn from(e: nitrokey::CommandError) -> Error { Error::CommandError(None, e) diff --git a/nitrocli/src/pinentry.rs b/nitrocli/src/pinentry.rs index 8ea4298..232bff9 100644 --- a/nitrocli/src/pinentry.rs +++ b/nitrocli/src/pinentry.rs @@ -134,7 +134,7 @@ where // specially. if !lines.is_empty() && lines[0].starts_with("ERR ") { let (_, error) = lines[0].split_at(4); - return Err(Error::Error(error.to_string())); + return Err(Error::from(error)); } Err(Error::Error(format!("Unexpected response: {}", string))) } |