    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37

    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))
    }