Skip to main content

git_vet/
git_types.rs

1use std::fmt;
2
3use gix::objs::tree::{EntryKind, EntryMode};
4use serde::{Serialize, Serializer};
5
6use crate::path::RepoPath;
7
8#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
9pub struct BlobOid(gix::ObjectId);
10
11impl BlobOid {
12    pub(crate) const fn new(oid: gix::ObjectId) -> Self {
13        Self(oid)
14    }
15
16    pub(crate) const fn as_object_id(&self) -> gix::ObjectId {
17        self.0
18    }
19
20    pub(crate) fn short(&self) -> String {
21        self.0.to_hex_with_len(12).to_string()
22    }
23}
24
25impl Serialize for BlobOid {
26    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
27    where
28        S: Serializer,
29    {
30        serializer.serialize_str(&self.to_string())
31    }
32}
33
34impl fmt::Display for BlobOid {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        write!(f, "{}", self.0)
37    }
38}
39
40#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
41pub struct CommitOid(gix::ObjectId);
42
43impl CommitOid {
44    pub(crate) const fn new(oid: gix::ObjectId) -> Self {
45        Self(oid)
46    }
47}
48
49impl fmt::Display for CommitOid {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        write!(f, "{}", self.0)
52    }
53}
54
55#[derive(Clone, Copy, Debug, Eq, PartialEq)]
56pub struct FileMode(EntryMode);
57
58impl FileMode {
59    pub(crate) const fn new(mode: EntryMode) -> Self {
60        Self(mode)
61    }
62
63    pub(crate) const fn kind(self) -> EntryKind {
64        self.0.kind()
65    }
66
67    pub(crate) const fn is_reviewable_file(self) -> bool {
68        self.0.is_blob_or_symlink()
69    }
70
71    pub(crate) const fn is_submodule(self) -> bool {
72        self.0.is_commit()
73    }
74
75    pub(crate) fn as_octal(self) -> String {
76        format!("{:o}", self.0)
77    }
78}
79
80#[derive(Clone, Debug)]
81pub struct TrackedFile {
82    pub(crate) path: RepoPath,
83    pub(crate) blob: BlobOid,
84    pub(crate) mode: FileMode,
85}
86
87#[derive(Clone, Debug)]
88pub struct HistoricalBlob {
89    pub(crate) blob: BlobOid,
90    pub(crate) mode: FileMode,
91}