use std::fmt;
use crate::Kind;
#[derive(Debug, Clone, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error("Unknown object kind: {kind:?}")]
InvalidObjectKind { kind: bstr::BString },
}
impl Kind {
pub fn from_bytes(s: &[u8]) -> Result<Kind, Error> {
Ok(match s {
b"tree" => Kind::Tree,
b"blob" => Kind::Blob,
b"commit" => Kind::Commit,
b"tag" => Kind::Tag,
_ => return Err(Error::InvalidObjectKind { kind: s.into() }),
})
}
}
impl Kind {
pub fn as_bytes(&self) -> &[u8] {
match self {
Kind::Tree => b"tree",
Kind::Commit => b"commit",
Kind::Blob => b"blob",
Kind::Tag => b"tag",
}
}
pub fn is_commit(&self) -> bool {
matches!(self, Kind::Commit)
}
pub fn is_tree(&self) -> bool {
matches!(self, Kind::Tree)
}
pub fn is_tag(&self) -> bool {
matches!(self, Kind::Tag)
}
pub fn is_blob(&self) -> bool {
matches!(self, Kind::Blob)
}
}
impl fmt::Display for Kind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(std::str::from_utf8(self.as_bytes()).expect("Converting Kind name to utf8"))
}
}