git_hash/
prefix.rs

1use std::{cmp::Ordering, convert::TryFrom};
2
3use crate::{oid, ObjectId, Prefix};
4
5/// The error returned by [Prefix::new()].
6#[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
18///
19pub mod from_hex {
20    /// The error returned by [Prefix::from_hex][super::Prefix::from_hex()].
21    #[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    /// The smallest allowed prefix length below which chances for collisions are too high even in small repositories.
38    pub const MIN_HEX_LEN: usize = 4;
39
40    /// Create a new instance by taking a full `id` as input and truncating it to `hex_len`.
41    ///
42    /// For instance, with `hex_len` of 7 the resulting prefix is 3.5 bytes, or 3 bytes and 4 bits
43    /// wide, with all other bytes and bits set to zero.
44    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    /// Returns the prefix as object id.
67    ///
68    /// Note that it may be deceptive to use given that it looks like a full
69    /// object id, even though its post-prefix bytes/bits are set to zero.
70    pub fn as_oid(&self) -> &oid {
71        &self.bytes
72    }
73
74    /// Return the amount of hexadecimal characters that are set in the prefix.
75    ///
76    /// This gives the prefix a granularity of 4 bits.
77    pub fn hex_len(&self) -> usize {
78        self.hex_len
79    }
80
81    /// Provided with candidate id which is a full hash, determine how this prefix compares to it,
82    /// only looking at the prefix bytes, ignoring everything behind that.
83    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    /// Create an instance from the given hexadecimal prefix `value`, e.g. `35e77c16` would yield a `Prefix` with `hex_len()` = 8.
97    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
129/// Create an instance from the given hexadecimal prefix, e.g. `35e77c16` would yield a `Prefix`
130/// with `hex_len()` = 8.
131impl 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}