1use std::hash;
2
3use crate::{Kind, ObjectId};
4
5#[cfg(feature = "sha1")]
6use crate::{EMPTY_BLOB_SHA1, EMPTY_TREE_SHA1, SIZE_OF_SHA1_DIGEST};
7
8#[cfg(feature = "sha256")]
9use crate::{EMPTY_BLOB_SHA256, EMPTY_TREE_SHA256, SIZE_OF_SHA256_DIGEST};
10
11#[derive(PartialEq, Eq, Ord, PartialOrd)]
24#[repr(transparent)]
25#[allow(non_camel_case_types)]
26#[cfg_attr(feature = "serde", derive(serde::Serialize))]
27pub struct oid {
28 bytes: [u8],
29}
30
31#[allow(clippy::derived_hash_with_manual_eq)]
37impl hash::Hash for oid {
38 fn hash<H: hash::Hasher>(&self, state: &mut H) {
39 state.write(self.as_bytes());
40 }
41}
42
43#[derive(PartialEq, Eq, Hash, Ord, PartialOrd)]
45pub struct HexDisplay<'a> {
46 inner: &'a oid,
47 hex_len: usize,
48}
49
50impl std::fmt::Display for HexDisplay<'_> {
51 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52 let mut hex = Kind::hex_buf();
53 let hex = self.inner.hex_to_buf(hex.as_mut());
54 let max_len = hex.len();
55 f.write_str(&hex[..self.hex_len.min(max_len)])
56 }
57}
58
59impl std::fmt::Debug for oid {
60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61 write!(
62 f,
63 "{}({})",
64 match self.kind() {
65 #[cfg(feature = "sha1")]
66 Kind::Sha1 => "Sha1",
67 #[cfg(feature = "sha256")]
68 Kind::Sha256 => "Sha256",
69 },
70 self.to_hex(),
71 )
72 }
73}
74
75#[allow(missing_docs)]
77#[derive(Debug, thiserror::Error)]
78pub enum Error {
79 #[error("Cannot instantiate git hash from a digest of length {0}")]
80 InvalidByteSliceLength(usize),
81}
82
83impl oid {
85 #[inline]
87 pub fn try_from_bytes(digest: &[u8]) -> Result<&Self, Error> {
88 match digest.len() {
89 #[cfg(feature = "sha1")]
90 SIZE_OF_SHA1_DIGEST => Ok(
91 #[allow(unsafe_code)]
92 unsafe {
93 &*(std::ptr::from_ref::<[u8]>(digest) as *const oid)
94 },
95 ),
96 #[cfg(feature = "sha256")]
97 SIZE_OF_SHA256_DIGEST => Ok(
98 #[allow(unsafe_code)]
99 unsafe {
100 &*(std::ptr::from_ref::<[u8]>(digest) as *const oid)
101 },
102 ),
103 len => Err(Error::InvalidByteSliceLength(len)),
104 }
105 }
106
107 pub fn from_bytes_unchecked(value: &[u8]) -> &Self {
110 Self::from_bytes(value)
111 }
112
113 pub(crate) fn from_bytes(value: &[u8]) -> &Self {
115 #[allow(unsafe_code)]
116 unsafe {
117 &*(std::ptr::from_ref::<[u8]>(value) as *const oid)
118 }
119 }
120}
121
122impl oid {
124 #[inline]
126 pub fn kind(&self) -> Kind {
127 Kind::from_len_in_bytes(self.bytes.len())
128 }
129
130 #[inline]
132 pub fn first_byte(&self) -> u8 {
133 self.bytes[0]
134 }
135
136 #[inline]
138 pub fn as_bytes(&self) -> &[u8] {
139 &self.bytes
140 }
141
142 #[inline]
144 pub fn to_hex_with_len(&self, len: usize) -> HexDisplay<'_> {
145 HexDisplay {
146 inner: self,
147 hex_len: len,
148 }
149 }
150
151 #[inline]
153 pub fn to_hex(&self) -> HexDisplay<'_> {
154 HexDisplay {
155 inner: self,
156 hex_len: self.bytes.len() * 2,
157 }
158 }
159
160 #[inline]
166 #[must_use]
167 pub fn hex_to_buf<'a>(&self, buf: &'a mut [u8]) -> &'a mut str {
168 let num_hex_bytes = self.bytes.len() * 2;
169 faster_hex::hex_encode(&self.bytes, &mut buf[..num_hex_bytes])
170 .expect("buffer size must be at least twice the hash digest size in bytes")
171 }
172
173 #[inline]
175 pub fn write_hex_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()> {
176 let mut hex = Kind::hex_buf();
177 let hex_len = self.hex_to_buf(&mut hex).len();
178 out.write_all(&hex[..hex_len])
179 }
180
181 #[inline]
183 #[doc(alias = "is_zero", alias = "git2")]
184 pub fn is_null(&self) -> bool {
185 match self.kind() {
186 #[cfg(feature = "sha1")]
187 Kind::Sha1 => &self.bytes == oid::null_sha1().as_bytes(),
188 #[cfg(feature = "sha256")]
189 Kind::Sha256 => &self.bytes == oid::null_sha256().as_bytes(),
190 }
191 }
192
193 #[inline]
195 pub fn is_empty_blob(&self) -> bool {
196 match self.kind() {
197 #[cfg(feature = "sha1")]
198 Kind::Sha1 => &self.bytes == oid::empty_blob_sha1().as_bytes(),
199 #[cfg(feature = "sha256")]
200 Kind::Sha256 => &self.bytes == oid::empty_blob_sha256().as_bytes(),
201 }
202 }
203
204 #[inline]
206 pub fn is_empty_tree(&self) -> bool {
207 match self.kind() {
208 #[cfg(feature = "sha1")]
209 Kind::Sha1 => &self.bytes == oid::empty_tree_sha1().as_bytes(),
210 #[cfg(feature = "sha256")]
211 Kind::Sha256 => &self.bytes == oid::empty_tree_sha256().as_bytes(),
212 }
213 }
214}
215
216impl oid {
218 #[inline]
220 #[cfg(feature = "sha1")]
221 pub(crate) fn null_sha1() -> &'static Self {
222 oid::from_bytes([0u8; SIZE_OF_SHA1_DIGEST].as_ref())
223 }
224
225 #[inline]
227 #[cfg(feature = "sha256")]
228 pub(crate) fn null_sha256() -> &'static Self {
229 oid::from_bytes([0u8; SIZE_OF_SHA256_DIGEST].as_ref())
230 }
231
232 #[inline]
234 #[cfg(feature = "sha1")]
235 pub(crate) fn empty_blob_sha1() -> &'static Self {
236 oid::from_bytes(EMPTY_BLOB_SHA1)
237 }
238
239 #[inline]
241 #[cfg(feature = "sha256")]
242 pub(crate) fn empty_blob_sha256() -> &'static Self {
243 oid::from_bytes(EMPTY_BLOB_SHA256)
244 }
245
246 #[inline]
248 #[cfg(feature = "sha1")]
249 pub(crate) fn empty_tree_sha1() -> &'static Self {
250 oid::from_bytes(EMPTY_TREE_SHA1)
251 }
252
253 #[inline]
255 #[cfg(feature = "sha256")]
256 pub(crate) fn empty_tree_sha256() -> &'static Self {
257 oid::from_bytes(EMPTY_TREE_SHA256)
258 }
259}
260
261impl AsRef<oid> for &oid {
262 fn as_ref(&self) -> &oid {
263 self
264 }
265}
266
267impl<'a> TryFrom<&'a [u8]> for &'a oid {
268 type Error = Error;
269
270 fn try_from(value: &'a [u8]) -> Result<Self, Self::Error> {
271 oid::try_from_bytes(value)
272 }
273}
274
275impl ToOwned for oid {
276 type Owned = ObjectId;
277
278 fn to_owned(&self) -> Self::Owned {
279 match self.kind() {
280 #[cfg(feature = "sha1")]
281 Kind::Sha1 => ObjectId::Sha1(self.bytes.try_into().expect("no bug in hash detection")),
282 #[cfg(feature = "sha256")]
283 Kind::Sha256 => ObjectId::Sha256(self.bytes.try_into().expect("no bug in hash detection")),
284 }
285 }
286}
287
288#[cfg(feature = "sha1")]
289impl<'a> From<&'a [u8; SIZE_OF_SHA1_DIGEST]> for &'a oid {
290 fn from(v: &'a [u8; SIZE_OF_SHA1_DIGEST]) -> Self {
291 oid::from_bytes(v.as_ref())
292 }
293}
294
295#[cfg(feature = "sha256")]
296impl<'a> From<&'a [u8; SIZE_OF_SHA256_DIGEST]> for &'a oid {
297 fn from(v: &'a [u8; SIZE_OF_SHA256_DIGEST]) -> Self {
298 oid::from_bytes(v.as_ref())
299 }
300}
301
302impl std::fmt::Display for &oid {
303 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
304 let mut buf = Kind::hex_buf();
305 f.write_str(self.hex_to_buf(&mut buf))
306 }
307}
308
309impl PartialEq<ObjectId> for &oid {
310 fn eq(&self, other: &ObjectId) -> bool {
311 *self == other.as_ref()
312 }
313}
314
315#[cfg(feature = "serde")]
319impl<'de: 'a, 'a> serde::Deserialize<'de> for &'a oid {
320 fn deserialize<D>(deserializer: D) -> Result<Self, <D as serde::Deserializer<'de>>::Error>
321 where
322 D: serde::Deserializer<'de>,
323 {
324 struct __Visitor<'de: 'a, 'a> {
325 marker: std::marker::PhantomData<&'a oid>,
326 lifetime: std::marker::PhantomData<&'de ()>,
327 }
328 impl<'de: 'a, 'a> serde::de::Visitor<'de> for __Visitor<'de, 'a> {
329 type Value = &'a oid;
330 fn expecting(&self, __formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
331 std::fmt::Formatter::write_str(__formatter, "tuple struct Digest")
332 }
333 #[inline]
334 fn visit_newtype_struct<__E>(self, __e: __E) -> std::result::Result<Self::Value, __E::Error>
335 where
336 __E: serde::Deserializer<'de>,
337 {
338 let __field0: &'a [u8] = match <&'a [u8] as serde::Deserialize>::deserialize(__e) {
339 Ok(__val) => __val,
340 Err(__err) => {
341 return Err(__err);
342 }
343 };
344 Ok(oid::try_from_bytes(__field0).expect("hash of known length"))
345 }
346 #[inline]
347 fn visit_seq<__A>(self, mut __seq: __A) -> std::result::Result<Self::Value, __A::Error>
348 where
349 __A: serde::de::SeqAccess<'de>,
350 {
351 let __field0 = match match serde::de::SeqAccess::next_element::<&'a [u8]>(&mut __seq) {
352 Ok(__val) => __val,
353 Err(__err) => {
354 return Err(__err);
355 }
356 } {
357 Some(__value) => __value,
358 None => {
359 return Err(serde::de::Error::invalid_length(
360 0usize,
361 &"tuple struct Digest with 1 element",
362 ));
363 }
364 };
365 Ok(oid::try_from_bytes(__field0).expect("hash of known length"))
366 }
367 }
368 serde::Deserializer::deserialize_newtype_struct(
369 deserializer,
370 "Digest",
371 __Visitor {
372 marker: std::marker::PhantomData::<&'a oid>,
373 lifetime: std::marker::PhantomData,
374 },
375 )
376 }
377}