1use crate::Kind;
2use gix_error::{Result, bail};
3
4impl Kind {
6 pub fn from_bytes(s: &[u8]) -> Result<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 _ => {
16 bail!(gix_error::validation("Unknown object kind").with_input(s));
17 }
18 })
19 }
20}
21
22impl Kind {
24 pub fn as_bytes(&self) -> &[u8] {
26 match self {
27 Kind::Tree => b"tree",
28 Kind::Commit => b"commit",
29 Kind::Blob => b"blob",
30 Kind::Tag => b"tag",
31 }
32 }
33
34 pub fn is_commit(&self) -> bool {
36 matches!(self, Kind::Commit)
37 }
38
39 pub fn is_tree(&self) -> bool {
41 matches!(self, Kind::Tree)
42 }
43
44 pub fn is_tag(&self) -> bool {
46 matches!(self, Kind::Tag)
47 }
48
49 pub fn is_blob(&self) -> bool {
51 matches!(self, Kind::Blob)
52 }
53}
54
55impl std::fmt::Display for Kind {
56 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57 f.write_str(std::str::from_utf8(self.as_bytes()).expect("Converting Kind name to utf8"))
58 }
59}