unity_version/version/
revision_hash.rs

1use thiserror::Error;
2use derive_more::Deref;
3use std::str::FromStr;
4
5#[derive(Debug, Error)]
6#[allow(dead_code)]
7pub enum RevisionHashError {
8    #[error("Input must be exactly 12 characters long")]
9    InvalidLength,
10
11    #[error("Input contains invalid characters")]
12    InvalidCharacter,
13}
14
15#[derive(Eq, Debug, Clone, Hash, Deref)]
16#[allow(dead_code)]
17pub struct RevisionHash(String);
18
19impl std::fmt::Display for RevisionHash {
20    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21        write!(f, "{}", self.0)
22    }
23}
24
25#[allow(dead_code)]
26impl RevisionHash {
27    pub fn new(input: &str) -> Result<Self, RevisionHashError> {
28        if input.len() != 12 {
29            return Err(RevisionHashError::InvalidLength);
30        }
31
32        if input.chars().all(|c| c.is_ascii_hexdigit()) {
33            Ok(RevisionHash(input.to_string()))
34        } else {
35            return Err(RevisionHashError::InvalidCharacter);
36        }
37    }
38
39    pub fn as_str(&self) -> &str {
40        &self.0
41    }
42}
43
44impl PartialEq for RevisionHash {
45    fn eq(&self, other: &Self) -> bool {
46        self.as_str() == other.0
47    }
48}
49
50impl FromStr for RevisionHash {
51    type Err = RevisionHashError;
52
53    fn from_str(s: &str) -> Result<Self, Self::Err> {
54       RevisionHash::new(s) 
55    }
56}