#![deny(unsafe_code)]
#![deny(rust_2018_idioms, missing_docs)]
mod borrowed;
use std::{convert::TryFrom, str::FromStr};
pub use borrowed::oid;
mod owned;
pub use owned::{prefix, ObjectId, Prefix};
#[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> {
Self::from_hex(s.as_bytes())
}
}
}
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 = 1,
}
impl Default for Kind {
fn default() -> Self {
Kind::Sha1
}
}
impl TryFrom<u8> for Kind {
type Error = u8;
fn try_from(value: u8) -> Result<Self, Self::Error> {
Ok(match value {
1 => Kind::Sha1,
unknown => return Err(unknown),
})
}
}
impl FromStr for Kind {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"sha1" | "SHA1" => Kind::Sha1,
other => return Err(other.into()),
})
}
}
impl std::fmt::Display for Kind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Kind::Sha1 => f.write_str("SHA1"),
}
}
}
impl Kind {
#[inline]
pub const fn shortest() -> Self {
Self::Sha1
}
#[inline]
pub const fn longest() -> Self {
Self::Sha1
}
#[inline]
pub const fn hex_buf() -> [u8; Kind::longest().len_in_hex()] {
[0u8; Kind::longest().len_in_hex()]
}
#[inline]
pub const fn buf() -> [u8; Kind::longest().len_in_bytes()] {
[0u8; Kind::longest().len_in_bytes()]
}
#[inline]
pub const fn len_in_hex(&self) -> usize {
match self {
Kind::Sha1 => 40,
}
}
#[inline]
pub const fn len_in_bytes(&self) -> usize {
match self {
Kind::Sha1 => 20,
}
}
#[inline]
pub(crate) fn from_len_in_bytes(bytes: usize) -> Self {
match bytes {
20 => Kind::Sha1,
_ => panic!("BUG: must be called only with valid hash lengths produced by len_in_bytes()"),
}
}
#[inline]
pub fn null_ref(&self) -> &'static oid {
match self {
Kind::Sha1 => oid::null_sha1(),
}
}
#[inline]
pub const fn null(&self) -> ObjectId {
match self {
Kind::Sha1 => ObjectId::null_sha1(),
}
}
}