altcha_lib_rs/
algorithm.rs

1use serde::{Deserialize, Serialize};
2use std::fmt::Display;
3use std::str::FromStr;
4
5/// Algorithm options for the challenge
6#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
7pub enum AltchaAlgorithm {
8    #[cfg(feature = "sha1")]
9    #[serde(rename = "SHA-1")]
10    Sha1,
11    #[serde(rename = "SHA-256")]
12    Sha256,
13    #[serde(rename = "SHA-384")]
14    Sha384,
15    #[serde(rename = "SHA-512")]
16    Sha512,
17}
18
19impl FromStr for AltchaAlgorithm {
20    type Err = ();
21    fn from_str(input: &str) -> Result<AltchaAlgorithm, Self::Err> {
22        match input {
23            #[cfg(feature = "sha1")]
24            "SHA-1" => Ok(AltchaAlgorithm::Sha1),
25            "SHA-256" => Ok(AltchaAlgorithm::Sha256),
26            "SHA-384" => Ok(AltchaAlgorithm::Sha384),
27            "SHA-512" => Ok(AltchaAlgorithm::Sha512),
28            _ => Err(()),
29        }
30    }
31}
32
33impl Display for AltchaAlgorithm {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        let str = match self {
36            #[cfg(feature = "sha1")]
37            AltchaAlgorithm::Sha1 => "SHA-1",
38            AltchaAlgorithm::Sha256 => "SHA-256",
39            AltchaAlgorithm::Sha384 => "SHA-384",
40            AltchaAlgorithm::Sha512 => "SHA-512",
41        };
42        write!(f, "{}", str)
43    }
44}