From ad0a078a5a2c4c9efd16b20ff47cc4e1ef922dab Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 25 Mar 2023 16:46:12 -0400 Subject: clippy, fmt, etc --- src/api.rs | 211 +++++++++++++++++++++++++++++++------------------------------ 1 file changed, 109 insertions(+), 102 deletions(-) (limited to 'src/api.rs') diff --git a/src/api.rs b/src/api.rs index 85a0ce5..bf608b3 100644 --- a/src/api.rs +++ b/src/api.rs @@ -58,27 +58,27 @@ pub enum TwoFactorProviderType { } impl TwoFactorProviderType { + #[must_use] pub fn message(&self) -> &str { match *self { - TwoFactorProviderType::Authenticator => "Enter the 6 digit verification code from your authenticator app.", - TwoFactorProviderType::Email => "Enter the PIN you received via email.", + Self::Authenticator => "Enter the 6 digit verification code from your authenticator app.", + Self::Email => "Enter the PIN you received via email.", _ => "Enter the code." } } + #[must_use] pub fn header(&self) -> &str { match *self { - TwoFactorProviderType::Authenticator => "Authenticator App", - TwoFactorProviderType::Email => "Email Code", - _ => "Two Factor Authentication" + Self::Authenticator => "Authenticator App", + Self::Email => "Email Code", + _ => "Two Factor Authentication", } } + #[must_use] pub fn grab(&self) -> bool { - match *self { - TwoFactorProviderType::Email => false, - _ => true - } + !matches!(self, Self::Email) } } @@ -162,6 +162,96 @@ impl std::str::FromStr for TwoFactorProviderType { } } +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub enum KdfType { + Pbkdf2 = 0, + Argon2id = 1, +} + +impl<'de> serde::Deserialize<'de> for KdfType { + fn deserialize(deserializer: D) -> std::result::Result + where + D: serde::Deserializer<'de>, + { + struct KdfTypeVisitor; + impl<'de> serde::de::Visitor<'de> for KdfTypeVisitor { + type Value = KdfType; + + fn expecting( + &self, + formatter: &mut std::fmt::Formatter, + ) -> std::fmt::Result { + formatter.write_str("kdf id") + } + + fn visit_str( + self, + value: &str, + ) -> std::result::Result + where + E: serde::de::Error, + { + value.parse().map_err(serde::de::Error::custom) + } + + fn visit_u64( + self, + value: u64, + ) -> std::result::Result + where + E: serde::de::Error, + { + std::convert::TryFrom::try_from(value) + .map_err(serde::de::Error::custom) + } + } + + deserializer.deserialize_any(KdfTypeVisitor) + } +} + +impl std::convert::TryFrom for KdfType { + type Error = Error; + + fn try_from(ty: u64) -> Result { + match ty { + 0 => Ok(Self::Pbkdf2), + 1 => Ok(Self::Argon2id), + _ => Err(Error::InvalidKdfType { + ty: format!("{ty}"), + }), + } + } +} + +impl std::str::FromStr for KdfType { + type Err = Error; + + fn from_str(ty: &str) -> Result { + match ty { + "0" => Ok(Self::Pbkdf2), + "1" => Ok(Self::Argon2id), + _ => Err(Error::InvalidKdfType { ty: ty.to_string() }), + } + } +} + +impl serde::Serialize for KdfType { + fn serialize( + &self, + serializer: S, + ) -> std::result::Result + where + S: serde::Serializer, + { + let s = match self { + Self::Pbkdf2 => "0", + Self::Argon2id => "1", + }; + serializer.serialize_str(s) + } +} + #[derive(serde::Serialize, Debug)] struct PreloginReq { email: String, @@ -634,7 +724,10 @@ impl Client { } } - pub async fn prelogin(&self, email: &str) -> Result<(KdfType, u32, Option, Option)> { + pub async fn prelogin( + &self, + email: &str, + ) -> Result<(KdfType, u32, Option, Option)> { let prelogin = PreloginReq { email: email.to_string(), }; @@ -646,7 +739,12 @@ impl Client { .await .map_err(|source| Error::Reqwest { source })?; let prelogin_res: PreloginRes = res.json_with_path().await?; - Ok((prelogin_res.kdf, prelogin_res.kdf_iterations, prelogin_res.kdf_memory, prelogin_res.kdf_parallelism)) + Ok(( + prelogin_res.kdf, + prelogin_res.kdf_iterations, + prelogin_res.kdf_memory, + prelogin_res.kdf_parallelism, + )) } pub async fn register( @@ -1218,94 +1316,3 @@ fn classify_login_error(error_res: &ConnectErrorRes, code: u16) -> Error { log::warn!("unexpected error received during login: {:?}", error_res); Error::RequestFailed { status: code } } - - -#[derive(Debug, Copy, Clone, PartialEq, Eq)] -pub enum KdfType { - Pbkdf2 = 0, - Argon2id = 1, -} - -impl<'de> serde::Deserialize<'de> for KdfType { - fn deserialize(deserializer: D) -> std::result::Result - where - D: serde::Deserializer<'de>, - { - struct KdfTypeVisitor; - impl<'de> serde::de::Visitor<'de> for KdfTypeVisitor { - type Value = KdfType; - - fn expecting( - &self, - formatter: &mut std::fmt::Formatter, - ) -> std::fmt::Result { - formatter.write_str("two factor provider id") - } - - fn visit_str( - self, - value: &str, - ) -> std::result::Result - where - E: serde::de::Error, - { - value.parse().map_err(serde::de::Error::custom) - } - - fn visit_u64( - self, - value: u64, - ) -> std::result::Result - where - E: serde::de::Error, - { - std::convert::TryFrom::try_from(value) - .map_err(serde::de::Error::custom) - } - } - - deserializer.deserialize_any(KdfTypeVisitor) - } -} - -impl std::convert::TryFrom for KdfType { - type Error = Error; - - fn try_from(ty: u64) -> Result { - match ty { - 0 => Ok(Self::Pbkdf2), - 1 => Ok(Self::Argon2id), - _ => Err(Error::InvalidTwoFactorProvider { - ty: format!("{ty}"), - }), - } - } -} - -impl std::str::FromStr for KdfType { - type Err = Error; - - fn from_str(ty: &str) -> Result { - match ty { - "0" => Ok(Self::Pbkdf2), - "1" => Ok(Self::Argon2id), - _ => Err(Error::InvalidTwoFactorProvider { ty: ty.to_string() }), - } - } -} - -impl serde::Serialize for KdfType { - fn serialize( - &self, - serializer: S, - ) -> std::result::Result - where - S: serde::Serializer, - { - let s = match self { - Self::Pbkdf2 => "0", - Self::Argon2id => "1", - }; - serializer.serialize_str(s) - } -} -- cgit v1.2.3-54-g00ecf