Skip to main content

backbone_bucket/domain/entity/
hash_algorithm.rs

1use serde::{Deserialize, Serialize};
2use sqlx::Type;
3use std::str::FromStr;
4#[cfg(feature = "openapi")]
5use utoipa::ToSchema;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Type)]
8#[cfg_attr(feature = "openapi", derive(ToSchema))]
9#[serde(rename_all = "snake_case")]
10#[sqlx(type_name = "hash_algorithm", rename_all = "snake_case")]
11pub enum HashAlgorithm {
12    Sha256,
13    Md5,
14    Xxhash,
15}
16
17impl std::fmt::Display for HashAlgorithm {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        match self {
20            Self::Sha256 => write!(f, "sha256"),
21            Self::Md5 => write!(f, "md5"),
22            Self::Xxhash => write!(f, "xxhash"),
23        }
24    }
25}
26
27impl FromStr for HashAlgorithm {
28    type Err = String;
29
30    fn from_str(s: &str) -> Result<Self, Self::Err> {
31        match s.to_lowercase().as_str() {
32            "sha256" => Ok(Self::Sha256),
33            "md5" => Ok(Self::Md5),
34            "xxhash" => Ok(Self::Xxhash),
35            _ => Err(format!("Unknown HashAlgorithm variant: {}", s)),
36        }
37    }
38}
39
40impl Default for HashAlgorithm {
41    fn default() -> Self {
42        Self::Sha256
43    }
44}