git_hash/
kind.rs

1use std::{convert::TryFrom, str::FromStr};
2
3use crate::{oid, Kind, ObjectId};
4
5impl Default for Kind {
6    fn default() -> Self {
7        Kind::Sha1
8    }
9}
10
11impl TryFrom<u8> for Kind {
12    type Error = u8;
13
14    fn try_from(value: u8) -> Result<Self, Self::Error> {
15        Ok(match value {
16            1 => Kind::Sha1,
17            unknown => return Err(unknown),
18        })
19    }
20}
21
22impl FromStr for Kind {
23    type Err = String;
24
25    fn from_str(s: &str) -> Result<Self, Self::Err> {
26        Ok(match s {
27            "sha1" | "SHA1" => Kind::Sha1,
28            other => return Err(other.into()),
29        })
30    }
31}
32
33impl std::fmt::Display for Kind {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        match self {
36            Kind::Sha1 => f.write_str("SHA1"),
37        }
38    }
39}
40
41impl Kind {
42    /// Returns the shortest hash we support
43    #[inline]
44    pub const fn shortest() -> Self {
45        Self::Sha1
46    }
47
48    /// Returns the longest hash we support
49    #[inline]
50    pub const fn longest() -> Self {
51        Self::Sha1
52    }
53
54    /// Returns a buffer suitable to hold the longest possible hash in hex.
55    #[inline]
56    pub const fn hex_buf() -> [u8; Kind::longest().len_in_hex()] {
57        [0u8; Kind::longest().len_in_hex()]
58    }
59
60    /// Returns a buffer suitable to hold the longest possible hash as raw bytes.
61    #[inline]
62    pub const fn buf() -> [u8; Kind::longest().len_in_bytes()] {
63        [0u8; Kind::longest().len_in_bytes()]
64    }
65
66    /// Returns the amount of ascii-characters needed to encode this has in hex
67    #[inline]
68    pub const fn len_in_hex(&self) -> usize {
69        match self {
70            Kind::Sha1 => 40,
71        }
72    }
73    /// Returns the amount of bytes taken up by the hash of the current kind
74    #[inline]
75    pub const fn len_in_bytes(&self) -> usize {
76        match self {
77            Kind::Sha1 => 20,
78        }
79    }
80
81    /// Returns the kind of hash that would fit the given `hex_len`, or `None` if there is no fitting hash.
82    /// Note that 0 as `hex_len` fits always yields Sha1.
83    #[inline]
84    pub const fn from_hex_len(hex_len: usize) -> Option<Self> {
85        Some(match hex_len {
86            0..=40 => Kind::Sha1,
87            _ => return None,
88        })
89    }
90
91    /// Converts a size in bytes as obtained by `Kind::len_in_bytes()` into the corresponding hash kind, if possible.
92    ///
93    /// **Panics** if the hash length doesn't match a known hash.
94    ///
95    /// NOTE that this method isn't public as it shouldn't be encouraged to assume all hashes have the same length.
96    /// However, if there should be such a thing, our `oid` implementation will have to become an enum and it's pretty breaking
97    /// to the way it's currently being used as auto-dereffing doesn't work anymore. Let's hope it won't happen.
98    // TODO: make 'const' once Rust 1.57 is more readily available in projects using 'gitoxide'.
99    #[inline]
100    pub(crate) fn from_len_in_bytes(bytes: usize) -> Self {
101        match bytes {
102            20 => Kind::Sha1,
103            _ => panic!("BUG: must be called only with valid hash lengths produced by len_in_bytes()"),
104        }
105    }
106
107    /// Create a null-id of our hash kind.
108    #[inline]
109    pub fn null_ref(&self) -> &'static oid {
110        match self {
111            Kind::Sha1 => oid::null_sha1(),
112        }
113    }
114
115    /// Create a null-id of our hash kind.
116    #[inline]
117    pub const fn null(&self) -> ObjectId {
118        match self {
119            Kind::Sha1 => ObjectId::null_sha1(),
120        }
121    }
122}