1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use std::{
borrow::Borrow,
convert::TryInto,
fmt,
hash::{Hash, Hasher},
ops::Deref,
};
use crate::{borrowed::oid, Kind, SIZE_OF_SHA1_DIGEST};
#[derive(PartialEq, Eq, Ord, PartialOrd, Clone, Copy)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub enum ObjectId {
Sha1([u8; SIZE_OF_SHA1_DIGEST]),
}
#[allow(clippy::derive_hash_xor_eq)]
impl Hash for ObjectId {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write(self.as_slice())
}
}
#[allow(missing_docs)]
pub mod decode {
use std::str::FromStr;
use crate::object_id::ObjectId;
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error("A hash sized {0} hexadecimal characters is invalid")]
InvalidHexEncodingLength(usize),
#[error("Invalid character {c} at position {index}")]
Invalid { c: char, index: usize },
}
impl ObjectId {
pub fn from_hex(buffer: &[u8]) -> Result<ObjectId, Error> {
use hex::FromHex;
match buffer.len() {
40 => Ok(ObjectId::Sha1(<[u8; 20]>::from_hex(buffer).map_err(
|err| match err {
hex::FromHexError::InvalidHexCharacter { c, index } => Error::Invalid { c, index },
hex::FromHexError::OddLength | hex::FromHexError::InvalidStringLength => {
unreachable!("BUG: This is already checked")
}
},
)?)),
len => Err(Error::InvalidHexEncodingLength(len)),
}
}
}
impl FromStr for ObjectId {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::from_hex(s.as_bytes())
}
}
}
impl ObjectId {
#[inline]
pub fn kind(&self) -> crate::Kind {
match self {
ObjectId::Sha1(_) => crate::Kind::Sha1,
}
}
#[inline]
pub fn as_slice(&self) -> &[u8] {
match self {
Self::Sha1(b) => b.as_ref(),
}
}
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [u8] {
match self {
Self::Sha1(b) => b.as_mut(),
}
}
#[inline]
pub const fn empty_blob(hash: Kind) -> ObjectId {
match hash {
Kind::Sha1 => {
ObjectId::Sha1(*b"\xe6\x9d\xe2\x9b\xb2\xd1\xd6\x43\x4b\x8b\x29\xae\x77\x5a\xd8\xc2\xe4\x8c\x53\x91")
}
}
}
#[inline]
pub const fn empty_tree(hash: Kind) -> ObjectId {
match hash {
Kind::Sha1 => {
ObjectId::Sha1(*b"\x4b\x82\x5d\xc6\x42\xcb\x6e\xb9\xa0\x60\xe5\x4b\xf8\xd6\x92\x88\xfb\xee\x49\x04")
}
}
}
#[inline]
pub fn is_null(&self) -> bool {
match self {
ObjectId::Sha1(digest) => &digest[..] == oid::null_sha1().as_bytes(),
}
}
#[inline]
pub const fn null(kind: crate::Kind) -> ObjectId {
match kind {
crate::Kind::Sha1 => Self::null_sha1(),
}
}
}
impl ObjectId {
#[inline]
fn new_sha1(id: [u8; SIZE_OF_SHA1_DIGEST]) -> Self {
ObjectId::Sha1(id)
}
#[inline]
pub(crate) fn from_20_bytes(b: &[u8]) -> ObjectId {
let mut id = [0; SIZE_OF_SHA1_DIGEST];
id.copy_from_slice(b);
ObjectId::Sha1(id)
}
#[inline]
pub(crate) const fn null_sha1() -> ObjectId {
ObjectId::Sha1([0u8; 20])
}
}
impl std::fmt::Debug for ObjectId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ObjectId::Sha1(_hash) => f.write_str("Sha1(")?,
}
for b in self.as_bytes() {
write!(f, "{b:02x}")?;
}
f.write_str(")")
}
}
impl From<[u8; SIZE_OF_SHA1_DIGEST]> for ObjectId {
fn from(v: [u8; 20]) -> Self {
Self::new_sha1(v)
}
}
impl From<&[u8]> for ObjectId {
fn from(v: &[u8]) -> Self {
match v.len() {
20 => Self::Sha1(v.try_into().expect("prior length validation")),
other => panic!("BUG: unsupported hash len: {other}"),
}
}
}
impl From<&crate::oid> for ObjectId {
fn from(v: &oid) -> Self {
match v.kind() {
crate::Kind::Sha1 => ObjectId::from_20_bytes(v.as_bytes()),
}
}
}
impl Deref for ObjectId {
type Target = oid;
fn deref(&self) -> &Self::Target {
self.as_ref()
}
}
impl AsRef<crate::oid> for ObjectId {
fn as_ref(&self) -> &oid {
oid::from_bytes_unchecked(self.as_slice())
}
}
impl Borrow<crate::oid> for ObjectId {
fn borrow(&self) -> &oid {
self.as_ref()
}
}
impl fmt::Display for ObjectId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_hex())
}
}
impl PartialEq<&crate::oid> for ObjectId {
fn eq(&self, other: &&oid) -> bool {
self.as_ref() == *other
}
}