1use std::fmt;
2
3use crate::Kind;
4
5#[derive(Debug, Clone, thiserror::Error)]
7#[allow(missing_docs)]
8pub enum Error {
9 #[error("Unknown object kind: {kind:?}")]
10 InvalidObjectKind { kind: bstr::BString },
11}
12
13impl Kind {
14 pub fn from_bytes(s: &[u8]) -> Result<Kind, Error> {
16 Ok(match s {
17 b"tree" => Kind::Tree,
18 b"blob" => Kind::Blob,
19 b"commit" => Kind::Commit,
20 b"tag" => Kind::Tag,
21 _ => return Err(Error::InvalidObjectKind { kind: s.into() }),
22 })
23 }
24
25 pub fn as_bytes(&self) -> &[u8] {
27 match self {
28 Kind::Tree => b"tree",
29 Kind::Commit => b"commit",
30 Kind::Blob => b"blob",
31 Kind::Tag => b"tag",
32 }
33 }
34}
35
36impl fmt::Display for Kind {
37 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38 f.write_str(std::str::from_utf8(self.as_bytes()).expect("Converting Kind name to utf8"))
39 }
40}