appletheia_application/command/
command_hash.rs1use std::fmt::{self, Display};
2
3use serde::{Deserialize, Serialize};
4use thiserror::Error;
5
6#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
7#[serde(transparent)]
8pub struct CommandHash(String);
9
10#[derive(Debug, Error)]
11pub enum CommandHashError {
12 #[error("command hash must be 64 lowercase hex chars")]
13 InvalidFormat,
14}
15
16impl CommandHash {
17 pub const LENGTH: usize = 64;
18
19 pub fn new(value: String) -> Result<Self, CommandHashError> {
20 if value.len() != Self::LENGTH {
21 return Err(CommandHashError::InvalidFormat);
22 }
23 if !value
24 .as_bytes()
25 .iter()
26 .all(|&b| b.is_ascii_hexdigit() && !b.is_ascii_uppercase())
27 {
28 return Err(CommandHashError::InvalidFormat);
29 }
30 Ok(Self(value))
31 }
32
33 pub fn as_str(&self) -> &str {
34 &self.0
35 }
36}
37
38impl AsRef<str> for CommandHash {
39 fn as_ref(&self) -> &str {
40 self.as_str()
41 }
42}
43
44impl Display for CommandHash {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 write!(f, "{}", self.as_str())
47 }
48}
49
50impl From<CommandHash> for String {
51 fn from(value: CommandHash) -> Self {
52 value.0
53 }
54}