From 01fe4700d29d676f18b0948b242806343dc4af85 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 15 Jan 2024 01:02:20 +0800 Subject: [PATCH] another difference between --check and fmt runs --- apps/vault/libraries/backup/src/lib.rs | 77 +++++++++----------------- 1 file changed, 27 insertions(+), 50 deletions(-) diff --git a/apps/vault/libraries/backup/src/lib.rs b/apps/vault/libraries/backup/src/lib.rs index 47b387292..0a2df5b0d 100644 --- a/apps/vault/libraries/backup/src/lib.rs +++ b/apps/vault/libraries/backup/src/lib.rs @@ -1,8 +1,9 @@ -use cbor::{self, cbor_array_vec, cbor_int, cbor_map, cbor_unsigned, destructure_cbor_map}; -use serde::{Deserialize, Serialize}; use std::fmt::Display; -pub const CONTINUE_RESPONSE: &[u8] = &[42,43,44,45]; +use cbor::{self, cbor_array_vec, cbor_int, cbor_map, cbor_unsigned, destructure_cbor_map}; +use serde::{Deserialize, Serialize}; + +pub const CONTINUE_RESPONSE: &[u8] = &[42, 43, 44, 45]; pub const OKAY_CANARY: &[u8] = &[0xca, 0xfe, 0xba, 0xbe]; pub const ERROR_VENDOR_HANDLING: u8 = 0x35; pub const VENDOR_SESSION_ERROR: u8 = 0x36; @@ -62,6 +63,8 @@ impl From for cbor::Value { } impl TryFrom for HashAlgorithms { + type Error = CborConversionError; + fn try_from(value: cbor::Value) -> Result { let v = extract_unsigned(value)?; match v { @@ -71,11 +74,11 @@ impl TryFrom for HashAlgorithms { _ => Err(CborConversionError::UnknownAlgorithm(v)), } } - - type Error = CborConversionError; } impl std::str::FromStr for HashAlgorithms { + type Err = HashFromStrError; + fn from_str(input: &str) -> Result { match input { "SHA1" => Ok(HashAlgorithms::SHA1), @@ -84,8 +87,6 @@ impl std::str::FromStr for HashAlgorithms { _ => Err(HashFromStrError::UnknownHash), } } - - type Err = HashFromStrError; } #[derive(Debug, Clone, Serialize, Deserialize, Default)] @@ -112,6 +113,8 @@ impl From for cbor::Value { } impl TryFrom for TotpEntry { + type Error = CborConversionError; + fn try_from(value: cbor::Value) -> Result { let rawmap = match value { cbor::Value::Map(m) => m, @@ -134,33 +137,23 @@ impl TryFrom for TotpEntry { let digit_count = extract_unsigned(digit_count.unwrap())? as u32; let algorithm: HashAlgorithms = algorithm.unwrap().try_into()?; let name = extract_string(name.unwrap())?; - let hotp = extract_bool(hotp.unwrap_or( - cbor::Value::Simple(cbor::SimpleValue::FalseValue) - )).unwrap_or(false); + let hotp = + extract_bool(hotp.unwrap_or(cbor::Value::Simple(cbor::SimpleValue::FalseValue))).unwrap_or(false); - Ok(TotpEntry { - step_seconds, - shared_secret, - digit_count, - algorithm, - name, - hotp, - }) + Ok(TotpEntry { step_seconds, shared_secret, digit_count, algorithm, name, hotp }) } - - type Error = CborConversionError; } #[derive(Debug, Serialize, Deserialize, Default)] pub struct TotpEntries(pub Vec); impl From<&TotpEntries> for cbor::Value { - fn from(te: &TotpEntries) -> Self { - cbor_array_vec!(te.0.clone()) - } + fn from(te: &TotpEntries) -> Self { cbor_array_vec!(te.0.clone()) } } impl TryFrom for TotpEntries { + type Error = CborConversionError; + fn try_from(value: cbor::Value) -> Result { let raw = extract_array(value)?; let mut ret = vec![]; @@ -171,8 +164,6 @@ impl TryFrom for TotpEntries { Ok(Self(ret)) } - - type Error = CborConversionError; } impl From<&TotpEntries> for Vec { @@ -204,6 +195,8 @@ impl From for cbor::Value { } impl TryFrom for PasswordEntry { + type Error = CborConversionError; + fn try_from(value: cbor::Value) -> Result { let rawmap = match value { cbor::Value::Map(m) => m, @@ -224,24 +217,15 @@ impl TryFrom for PasswordEntry { let password = extract_string(password.unwrap())?; let notes = extract_string(notes.unwrap())?; - Ok(PasswordEntry { - description, - username, - password, - notes, - }) + Ok(PasswordEntry { description, username, password, notes }) } - - type Error = CborConversionError; } #[derive(Debug, Serialize, Deserialize, Default)] pub struct PasswordEntries(pub Vec); impl From<&PasswordEntries> for cbor::Value { - fn from(te: &PasswordEntries) -> Self { - cbor_array_vec!(te.0.clone()) - } + fn from(te: &PasswordEntries) -> Self { cbor_array_vec!(te.0.clone()) } } impl From<&PasswordEntries> for Vec { @@ -254,6 +238,8 @@ impl From<&PasswordEntries> for Vec { } impl TryFrom for PasswordEntries { + type Error = CborConversionError; + fn try_from(value: cbor::Value) -> Result { let raw = extract_array(value)?; let mut ret = vec![]; @@ -264,8 +250,6 @@ impl TryFrom for PasswordEntries { Ok(Self(ret)) } - - type Error = CborConversionError; } #[derive(Debug, Serialize, Deserialize)] @@ -313,6 +297,8 @@ impl From for cbor::Value { } impl TryFrom for DataPacket { + type Error = CborConversionError; + fn try_from(value: cbor::Value) -> Result { let rawmap = match value { cbor::Value::Map(m) => m, @@ -339,16 +325,11 @@ impl TryFrom for DataPacket { let pes: TotpEntries = shared_secret.try_into()?; DataPacket::TOTP(pes) } - others => panic!( - "cannot convert from data packet type {} from cbor::Value!", - others - ), + others => panic!("cannot convert from data packet type {} from cbor::Value!", others), }; Ok(dp) } - - type Error = CborConversionError; } pub enum PayloadType { @@ -372,6 +353,7 @@ pub enum PayloadTypeError { impl TryFrom<&Vec> for PayloadType { type Error = PayloadTypeError; + fn try_from(u: &Vec) -> Result { if u.is_empty() { return Err(PayloadTypeError::BadType); @@ -508,12 +490,7 @@ impl TryFrom for Wire { let data = extract_byte_string(data.unwrap())?; let more_data = extract_bool(more_data.unwrap())?; - Ok(Self { - index, - more_data, - size, - data, - }) + Ok(Self { index, more_data, size, data }) } }