1#![allow(missing_docs)]
16
17use std::fmt;
18use std::fmt::Debug;
19
20use crate::hex_util;
21
22pub trait ObjectId {
23 fn object_type(&self) -> String;
24 fn as_bytes(&self) -> &[u8];
25 fn to_bytes(&self) -> Vec<u8>;
26 fn hex(&self) -> String;
27}
28
29macro_rules! id_type {
44 ( $(#[$attr:meta])*
45 $vis:vis $name:ident { $hex_method:ident() }
46 ) => {
47 $(#[$attr])*
48 #[derive(ContentHash, PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
49 $vis struct $name(Vec<u8>);
50 $crate::object_id::impl_id_type!($name, $hex_method);
51 };
52}
53
54macro_rules! impl_id_type {
55 ($name:ident, $hex_method:ident) => {
56 impl $name {
57 pub fn new(value: Vec<u8>) -> Self {
58 Self(value)
59 }
60
61 pub fn from_bytes(bytes: &[u8]) -> Self {
62 Self(bytes.to_vec())
63 }
64
65 pub fn from_hex(hex: &'static str) -> Self {
70 Self::try_from_hex(hex).unwrap()
71 }
72
73 pub fn try_from_hex(hex: &str) -> Option<Self> {
75 $crate::hex_util::decode_hex(hex).map(Self)
76 }
77 }
78
79 impl std::fmt::Debug for $name {
80 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
81 f.debug_tuple(stringify!($name)).field(&self.hex()).finish()
83 }
84 }
85
86 impl std::fmt::Display for $name {
87 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
88 f.pad(&self.$hex_method())
89 }
90 }
91
92 impl serde::Serialize for $name {
93 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
94 where
95 S: serde::Serializer,
96 {
97 if serializer.is_human_readable() {
98 self.$hex_method().serialize(serializer)
99 } else {
100 self.as_bytes().serialize(serializer)
101 }
102 }
103 }
104
105 impl crate::object_id::ObjectId for $name {
106 fn object_type(&self) -> String {
107 stringify!($name)
108 .strip_suffix("Id")
109 .unwrap()
110 .to_ascii_lowercase()
111 .to_string()
112 }
113
114 fn as_bytes(&self) -> &[u8] {
115 &self.0
116 }
117
118 fn to_bytes(&self) -> Vec<u8> {
119 self.0.clone()
120 }
121
122 fn hex(&self) -> String {
123 $crate::hex_util::encode_hex(&self.0)
124 }
125 }
126 };
127}
128
129pub(crate) use id_type;
130pub(crate) use impl_id_type;
131
132#[derive(Clone, PartialEq, Eq)]
135pub struct HexPrefix {
136 min_prefix_bytes: Vec<u8>,
139 has_odd_byte: bool,
140}
141
142impl HexPrefix {
143 pub fn try_from_hex(prefix: &str) -> Option<HexPrefix> {
146 let (min_prefix_bytes, has_odd_byte) = hex_util::decode_hex_prefix(prefix)?;
147 Some(HexPrefix {
148 min_prefix_bytes,
149 has_odd_byte,
150 })
151 }
152
153 pub fn try_from_reverse_hex(prefix: &str) -> Option<HexPrefix> {
156 let (min_prefix_bytes, has_odd_byte) = hex_util::decode_reverse_hex_prefix(prefix)?;
157 Some(HexPrefix {
158 min_prefix_bytes,
159 has_odd_byte,
160 })
161 }
162
163 pub fn from_bytes(bytes: &[u8]) -> Self {
164 HexPrefix {
165 min_prefix_bytes: bytes.to_owned(),
166 has_odd_byte: false,
167 }
168 }
169
170 pub fn from_id<T: ObjectId + ?Sized>(id: &T) -> Self {
172 Self::from_bytes(id.as_bytes())
173 }
174
175 pub fn hex(&self) -> String {
177 let mut hex_string = hex_util::encode_hex(&self.min_prefix_bytes);
178 if self.has_odd_byte {
179 hex_string.pop().unwrap();
180 }
181 hex_string
182 }
183
184 pub fn reverse_hex(&self) -> String {
186 let mut hex_string = hex_util::encode_reverse_hex(&self.min_prefix_bytes);
187 if self.has_odd_byte {
188 hex_string.pop().unwrap();
189 }
190 hex_string
191 }
192
193 pub fn min_prefix_bytes(&self) -> &[u8] {
197 &self.min_prefix_bytes
198 }
199
200 pub fn as_full_bytes(&self) -> Option<&[u8]> {
202 (!self.has_odd_byte).then_some(&self.min_prefix_bytes)
203 }
204
205 fn split_odd_byte(&self) -> (Option<u8>, &[u8]) {
206 if self.has_odd_byte {
207 let (&odd, prefix) = self.min_prefix_bytes.split_last().unwrap();
208 (Some(odd), prefix)
209 } else {
210 (None, &self.min_prefix_bytes)
211 }
212 }
213
214 pub fn matches<Q: ObjectId>(&self, id: &Q) -> bool {
216 let id_bytes = id.as_bytes();
217 let (maybe_odd, prefix) = self.split_odd_byte();
218 if id_bytes.starts_with(prefix) {
219 if let Some(odd) = maybe_odd {
220 matches!(id_bytes.get(prefix.len()), Some(v) if v & 0xf0 == odd)
221 } else {
222 true
223 }
224 } else {
225 false
226 }
227 }
228}
229
230impl Debug for HexPrefix {
231 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
232 f.debug_tuple("HexPrefix").field(&self.hex()).finish()
233 }
234}
235
236#[derive(Debug, Clone, PartialEq, Eq)]
238pub enum PrefixResolution<T> {
239 NoMatch,
240 SingleMatch(T),
241 AmbiguousMatch,
242}
243
244impl<T> PrefixResolution<T> {
245 pub fn map<U>(self, f: impl FnOnce(T) -> U) -> PrefixResolution<U> {
246 match self {
247 PrefixResolution::NoMatch => PrefixResolution::NoMatch,
248 PrefixResolution::SingleMatch(x) => PrefixResolution::SingleMatch(f(x)),
249 PrefixResolution::AmbiguousMatch => PrefixResolution::AmbiguousMatch,
250 }
251 }
252}
253
254impl<T: Clone> PrefixResolution<T> {
255 pub fn plus(&self, other: &PrefixResolution<T>) -> PrefixResolution<T> {
256 match (self, other) {
257 (PrefixResolution::NoMatch, other) => other.clone(),
258 (local, PrefixResolution::NoMatch) => local.clone(),
259 (PrefixResolution::AmbiguousMatch, _) => PrefixResolution::AmbiguousMatch,
260 (_, PrefixResolution::AmbiguousMatch) => PrefixResolution::AmbiguousMatch,
261 (PrefixResolution::SingleMatch(_), PrefixResolution::SingleMatch(_)) => {
262 PrefixResolution::AmbiguousMatch
263 }
264 }
265 }
266}
267
268#[cfg(test)]
269mod tests {
270 use super::*;
271 use crate::backend::ChangeId;
272 use crate::backend::CommitId;
273
274 #[test]
275 fn test_display_object_id() {
276 let commit_id = CommitId::from_hex("deadbeef0123");
277 assert_eq!(format!("{commit_id}"), "deadbeef0123");
278 assert_eq!(format!("{commit_id:.6}"), "deadbe");
279
280 let change_id = ChangeId::from_hex("deadbeef0123");
281 assert_eq!(format!("{change_id}"), "mlpmollkzyxw");
282 assert_eq!(format!("{change_id:.6}"), "mlpmol");
283 }
284
285 #[test]
286 fn test_hex_prefix_prefixes() {
287 let prefix = HexPrefix::try_from_hex("").unwrap();
288 assert_eq!(prefix.min_prefix_bytes(), b"");
289
290 let prefix = HexPrefix::try_from_hex("1").unwrap();
291 assert_eq!(prefix.min_prefix_bytes(), b"\x10");
292
293 let prefix = HexPrefix::try_from_hex("12").unwrap();
294 assert_eq!(prefix.min_prefix_bytes(), b"\x12");
295
296 let prefix = HexPrefix::try_from_hex("123").unwrap();
297 assert_eq!(prefix.min_prefix_bytes(), b"\x12\x30");
298
299 let bad_prefix = HexPrefix::try_from_hex("0x123");
300 assert_eq!(bad_prefix, None);
301
302 let bad_prefix = HexPrefix::try_from_hex("foobar");
303 assert_eq!(bad_prefix, None);
304 }
305
306 #[test]
307 fn test_hex_prefix_matches() {
308 let id = CommitId::from_hex("1234");
309
310 assert!(HexPrefix::try_from_hex("").unwrap().matches(&id));
311 assert!(HexPrefix::try_from_hex("1").unwrap().matches(&id));
312 assert!(HexPrefix::try_from_hex("12").unwrap().matches(&id));
313 assert!(HexPrefix::try_from_hex("123").unwrap().matches(&id));
314 assert!(HexPrefix::try_from_hex("1234").unwrap().matches(&id));
315 assert!(!HexPrefix::try_from_hex("12345").unwrap().matches(&id));
316
317 assert!(!HexPrefix::try_from_hex("a").unwrap().matches(&id));
318 assert!(!HexPrefix::try_from_hex("1a").unwrap().matches(&id));
319 assert!(!HexPrefix::try_from_hex("12a").unwrap().matches(&id));
320 assert!(!HexPrefix::try_from_hex("123a").unwrap().matches(&id));
321 }
322}