use std::str::FromStr;
use crate::{oid, Kind, ObjectId};
#[cfg(feature = "sha1")]
use crate::{SIZE_OF_SHA1_DIGEST, SIZE_OF_SHA1_HEX_DIGEST};
#[cfg(feature = "sha256")]
use crate::{SIZE_OF_SHA256_DIGEST, SIZE_OF_SHA256_HEX_DIGEST};
impl TryFrom<u8> for Kind {
type Error = u8;
fn try_from(value: u8) -> Result<Self, Self::Error> {
Ok(match value {
#[cfg(feature = "sha1")]
1 => Kind::Sha1,
#[cfg(feature = "sha256")]
2 => Kind::Sha256,
unknown => return Err(unknown),
})
}
}
impl FromStr for Kind {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
#[cfg(feature = "sha1")]
"sha1" | "SHA1" | "SHA-1" => Kind::Sha1,
#[cfg(feature = "sha256")]
"sha256" | "SHA256" | "SHA-256" => Kind::Sha256,
other => return Err(other.into()),
})
}
}
impl std::fmt::Display for Kind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
#[cfg(feature = "sha1")]
Kind::Sha1 => f.write_str("sha1"),
#[cfg(feature = "sha256")]
Kind::Sha256 => f.write_str("sha256"),
}
}
}
impl Kind {
#[inline]
pub const fn shortest() -> Self {
#[cfg(all(not(feature = "sha1"), feature = "sha256"))]
{
Self::Sha256
}
#[cfg(feature = "sha1")]
{
Self::Sha1
}
}
#[inline]
pub const fn longest() -> Self {
#[cfg(feature = "sha256")]
{
Self::Sha256
}
#[cfg(all(not(feature = "sha256"), feature = "sha1"))]
{
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 {
#[cfg(feature = "sha1")]
Kind::Sha1 => SIZE_OF_SHA1_HEX_DIGEST,
#[cfg(feature = "sha256")]
Kind::Sha256 => SIZE_OF_SHA256_HEX_DIGEST,
}
}
#[inline]
pub const fn len_in_bytes(&self) -> usize {
match self {
#[cfg(feature = "sha1")]
Kind::Sha1 => SIZE_OF_SHA1_DIGEST,
#[cfg(feature = "sha256")]
Kind::Sha256 => SIZE_OF_SHA256_DIGEST,
}
}
#[inline]
pub const fn from_hex_len(hex_len: usize) -> Option<Self> {
Some(match hex_len {
#[cfg(feature = "sha1")]
0..=SIZE_OF_SHA1_HEX_DIGEST => Kind::Sha1,
#[cfg(feature = "sha256")]
0..=SIZE_OF_SHA256_HEX_DIGEST => Kind::Sha256,
_ => return None,
})
}
#[inline]
pub(crate) fn from_len_in_bytes(bytes: usize) -> Self {
match bytes {
#[cfg(feature = "sha1")]
SIZE_OF_SHA1_DIGEST => Kind::Sha1,
#[cfg(feature = "sha256")]
SIZE_OF_SHA256_DIGEST => Kind::Sha256,
_ => 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 {
#[cfg(feature = "sha1")]
Kind::Sha1 => oid::null_sha1(),
#[cfg(feature = "sha256")]
Kind::Sha256 => oid::null_sha256(),
}
}
#[inline]
pub const fn null(&self) -> ObjectId {
match self {
#[cfg(feature = "sha1")]
Kind::Sha1 => ObjectId::null_sha1(),
#[cfg(feature = "sha256")]
Kind::Sha256 => ObjectId::null_sha256(),
}
}
#[inline]
pub const fn empty_blob(&self) -> ObjectId {
ObjectId::empty_blob(*self)
}
#[inline]
pub const fn empty_tree(&self) -> ObjectId {
ObjectId::empty_tree(*self)
}
#[inline]
pub const fn all() -> &'static [Self] {
#[cfg(all(feature = "sha1", not(feature = "sha256")))]
{
&[Self::Sha1]
}
#[cfg(all(not(feature = "sha1"), feature = "sha256"))]
{
&[Self::Sha256]
}
#[cfg(all(feature = "sha1", feature = "sha256"))]
{
&[Self::Sha1, Self::Sha256]
}
}
}