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
use std::cmp::Ordering;
use quick_error::quick_error;
use crate::{oid, ObjectId, Prefix};
quick_error! {
#[derive(Debug)]
#[allow(missing_docs)]
pub enum Error {
TooShort { hex_len: usize } {
display("The minimum hex length of a short object id is 4, got {}", hex_len)
}
TooLong { object_kind: crate::Kind, hex_len: usize } {
display("An object of kind {} cannot be larger than {} in hex, but {} was requested", object_kind, object_kind.len_in_hex(), hex_len)
}
}
}
impl Prefix {
pub fn new(id: impl AsRef<oid>, hex_len: usize) -> Result<Self, Error> {
let id = id.as_ref();
if hex_len > id.kind().len_in_hex() {
Err(Error::TooLong {
object_kind: id.kind(),
hex_len,
})
} else if hex_len < 4 {
Err(Error::TooShort { hex_len })
} else {
let mut prefix = ObjectId::null(id.kind());
let b = prefix.as_mut_slice();
let copy_len = (hex_len + 1) / 2;
b[..copy_len].copy_from_slice(&id.as_bytes()[..copy_len]);
if hex_len % 2 == 1 {
b[hex_len / 2] &= 0xf0;
}
Ok(Prefix { bytes: prefix, hex_len })
}
}
pub fn as_oid(&self) -> &oid {
&self.bytes
}
pub fn hex_len(&self) -> usize {
self.hex_len
}
pub fn cmp_oid(&self, candidate: &oid) -> Ordering {
let common_len = self.hex_len / 2;
self.bytes.as_bytes()[..common_len]
.cmp(&candidate.as_bytes()[..common_len])
.then(if self.hex_len % 2 == 1 {
let half_byte_idx = self.hex_len / 2;
self.bytes.as_bytes()[half_byte_idx].cmp(&(candidate.as_bytes()[half_byte_idx] & 0xf0))
} else {
Ordering::Equal
})
}
pub fn from_hex(_hex: &str) -> Self {
todo!("Prefix::from_hex()")
}
}
impl std::fmt::Display for Prefix {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.bytes.to_hex_with_len(self.hex_len).fmt(f)
}
}