1use std::{cmp::Ordering, convert::TryFrom};
2
3use crate::{oid, ObjectId, Prefix};
4
5#[derive(Debug, thiserror::Error)]
7#[allow(missing_docs)]
8pub enum Error {
9 #[error(
10 "The minimum hex length of a short object id is {}, got {hex_len}",
11 Prefix::MIN_HEX_LEN
12 )]
13 TooShort { hex_len: usize },
14 #[error("An object of kind {object_kind} cannot be larger than {} in hex, but {hex_len} was requested", object_kind.len_in_hex())]
15 TooLong { object_kind: crate::Kind, hex_len: usize },
16}
17
18pub mod from_hex {
20 #[derive(Debug, Eq, PartialEq, thiserror::Error)]
22 #[allow(missing_docs)]
23 pub enum Error {
24 #[error(
25 "The minimum hex length of a short object id is {}, got {hex_len}",
26 super::Prefix::MIN_HEX_LEN
27 )]
28 TooShort { hex_len: usize },
29 #[error("An id cannot be larger than {} chars in hex, but {hex_len} was requested", crate::Kind::longest().len_in_hex())]
30 TooLong { hex_len: usize },
31 #[error("Invalid character {c} at position {index}")]
32 Invalid { c: char, index: usize },
33 }
34}
35
36impl Prefix {
37 pub const MIN_HEX_LEN: usize = 4;
39
40 pub fn new(id: impl AsRef<oid>, hex_len: usize) -> Result<Self, Error> {
45 let id = id.as_ref();
46 if hex_len > id.kind().len_in_hex() {
47 Err(Error::TooLong {
48 object_kind: id.kind(),
49 hex_len,
50 })
51 } else if hex_len < Self::MIN_HEX_LEN {
52 Err(Error::TooShort { hex_len })
53 } else {
54 let mut prefix = ObjectId::null(id.kind());
55 let b = prefix.as_mut_slice();
56 let copy_len = (hex_len + 1) / 2;
57 b[..copy_len].copy_from_slice(&id.as_bytes()[..copy_len]);
58 if hex_len % 2 == 1 {
59 b[hex_len / 2] &= 0xf0;
60 }
61
62 Ok(Prefix { bytes: prefix, hex_len })
63 }
64 }
65
66 pub fn as_oid(&self) -> &oid {
71 &self.bytes
72 }
73
74 pub fn hex_len(&self) -> usize {
78 self.hex_len
79 }
80
81 pub fn cmp_oid(&self, candidate: &oid) -> Ordering {
84 let common_len = self.hex_len / 2;
85
86 self.bytes.as_bytes()[..common_len]
87 .cmp(&candidate.as_bytes()[..common_len])
88 .then(if self.hex_len % 2 == 1 {
89 let half_byte_idx = self.hex_len / 2;
90 self.bytes.as_bytes()[half_byte_idx].cmp(&(candidate.as_bytes()[half_byte_idx] & 0xf0))
91 } else {
92 Ordering::Equal
93 })
94 }
95
96 pub fn from_hex(value: &str) -> Result<Self, from_hex::Error> {
98 use hex::FromHex;
99 let hex_len = value.len();
100
101 if hex_len > crate::Kind::longest().len_in_hex() {
102 return Err(from_hex::Error::TooLong { hex_len });
103 } else if hex_len < Self::MIN_HEX_LEN {
104 return Err(from_hex::Error::TooShort { hex_len });
105 };
106
107 let src = if value.len() % 2 == 0 {
108 Vec::from_hex(value)
109 } else {
110 let mut buf = [0u8; crate::Kind::longest().len_in_hex()];
111 buf[..value.len()].copy_from_slice(value.as_bytes());
112 buf[value.len()] = b'0';
113 Vec::from_hex(&buf[..value.len() + 1])
114 }
115 .map_err(|e| match e {
116 hex::FromHexError::InvalidHexCharacter { c, index } => from_hex::Error::Invalid { c, index },
117 hex::FromHexError::OddLength | hex::FromHexError::InvalidStringLength => panic!("This is already checked"),
118 })?;
119
120 let mut bytes = ObjectId::null(crate::Kind::from_hex_len(value.len()).expect("hex-len is already checked"));
121 let dst = bytes.as_mut_slice();
122 let copy_len = src.len();
123 dst[..copy_len].copy_from_slice(&src);
124
125 Ok(Prefix { bytes, hex_len })
126 }
127}
128
129impl TryFrom<&str> for Prefix {
132 type Error = from_hex::Error;
133
134 fn try_from(value: &str) -> Result<Self, Self::Error> {
135 Prefix::from_hex(value)
136 }
137}
138
139impl std::fmt::Display for Prefix {
140 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
141 self.bytes.to_hex_with_len(self.hex_len).fmt(f)
142 }
143}
144
145impl From<ObjectId> for Prefix {
146 fn from(oid: ObjectId) -> Self {
147 Prefix {
148 bytes: oid,
149 hex_len: oid.kind().len_in_hex(),
150 }
151 }
152}