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    #[serde(rename = "SHA-1")]
9    Sha1,
10    #[serde(rename = "SHA-256")]
11    Sha256,
12    #[serde(rename = "SHA-512")]
13    Sha512,
14}
15
16impl FromStr for AltchaAlgorithm {
17    type Err = ();
18    fn from_str(input: &str) -> Result<AltchaAlgorithm, Self::Err> {
19        match input {
20            "SHA-1" => Ok(AltchaAlgorithm::Sha1),
21            "SHA-256" => Ok(AltchaAlgorithm::Sha256),
22            "SHA-512" => Ok(AltchaAlgorithm::Sha512),
23            _ => Err(()),
24        }
25    }
26}
27
28impl Display for AltchaAlgorithm {
29    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30        let str = match self {
31            AltchaAlgorithm::Sha1 => "SHA-1",
32            AltchaAlgorithm::Sha256 => "SHA-256",
33            AltchaAlgorithm::Sha512 => "SHA-512",
34        };
35        write!(f, "{}", str)
36    }
37}