docserver/src/defs/totp_algorithm.rs

38 lines
1.1 KiB
Rust

use serde::{Deserialize,Serialize,Deserializer};
#[derive(Eq, PartialEq, Clone, Serialize, Debug, Deserialize)]
pub enum TotpAlgorithm {
Sha1,
Sha256,
Sha512,
}
impl Default for TotpAlgorithm {
fn default() -> Self {
TotpAlgorithm::Sha1
}
}
impl std::fmt::Display for TotpAlgorithm {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TotpAlgorithm::Sha1 => write!(f,"sha1"),
TotpAlgorithm::Sha256 => write!(f,"sha256"),
TotpAlgorithm::Sha512 => write!(f,"sha512"),
}
}
}
impl TotpAlgorithm {
pub fn from_str(value: &str) -> TotpAlgorithm {
match value {
"sha1" | "SHA1" => TotpAlgorithm::Sha1,
"sha512" | "SHA512" => TotpAlgorithm::Sha512,
"sha256" | "SHA256" => TotpAlgorithm::Sha256,
_ => TotpAlgorithm::default(),
}
}
}
pub fn deserialize_totp_algorithm<'de, D>(deserializer: D) -> Result<TotpAlgorithm, D::Error>
where D: Deserializer<'de> {
let buf = String::deserialize(deserializer)?;
Ok(TotpAlgorithm::from_str(&buf))
}