1use crate::Kind;
2use gix_error::ExnMessageResult;
3
4impl Kind {
6 pub fn from_bytes(s: &[u8]) -> ExnMessageResult<Kind> {
10 Ok(match s {
11 b"tree" => Kind::Tree,
12 b"blob" => Kind::Blob,
13 b"commit" => Kind::Commit,
14 b"tag" => Kind::Tag,
15 _ => return Err(gix_error::validation("Unknown object kind").with("input", s).into()),
16 })
17 }
18}
19
20impl Kind {
22 pub fn as_bytes(&self) -> &[u8] {
24 match self {
25 Kind::Tree => b"tree",
26 Kind::Commit => b"commit",
27 Kind::Blob => b"blob",
28 Kind::Tag => b"tag",
29 }
30 }
31
32 pub fn is_commit(&self) -> bool {
34 matches!(self, Kind::Commit)
35 }
36
37 pub fn is_tree(&self) -> bool {
39 matches!(self, Kind::Tree)
40 }
41
42 pub fn is_tag(&self) -> bool {
44 matches!(self, Kind::Tag)
45 }
46
47 pub fn is_blob(&self) -> bool {
49 matches!(self, Kind::Blob)
50 }
51}
52
53impl std::fmt::Display for Kind {
54 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55 f.write_str(std::str::from_utf8(self.as_bytes()).expect("Converting Kind name to utf8"))
56 }
57}