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
#![deny(unsafe_code)]
#![deny(rust_2018_idioms, missing_docs)]
mod borrowed;
pub use borrowed::oid;
#[allow(missing_docs)]
mod owned;
pub use owned::ObjectId;
#[allow(missing_docs)]
pub mod decode {
use std::str::FromStr;
use quick_error::quick_error;
use crate::owned::ObjectId;
quick_error! {
#[derive(Debug)]
#[allow(missing_docs)]
pub enum Error {
InvalidHexEncodingLength(length: usize) {
display("A hash sized {} hexadecimal characters is invalid", length)
}
}
}
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).expect("our length check is correct thus we can decode hex"),
)),
len => Err(Error::InvalidHexEncodingLength(len)),
}
}
}
impl FromStr for ObjectId {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
use hex::FromHex;
match s.len() {
40 => Ok(ObjectId::Sha1(
<[u8; 20]>::from_hex(s).expect("our length check is correct thus we can decode hex"),
)),
len => Err(Error::InvalidHexEncodingLength(len)),
}
}
}
}
pub const SIZE_OF_SHA1_DIGEST: usize = 20;
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub enum Kind {
Sha1,
}
impl Default for Kind {
fn default() -> Self {
Kind::Sha1
}
}
impl Kind {
pub const fn shortest() -> Self {
Self::Sha1
}
pub const fn longest() -> Self {
Self::Sha1
}
pub const fn len_in_hex(&self) -> usize {
match self {
Kind::Sha1 => 40,
}
}
}