Skip to main content

git_vet/
git_types.rs

1use std::fmt;
2
3use gix::objs::tree::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
21impl Serialize for BlobOid {
22    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
23    where
24        S: Serializer,
25    {
26        serializer.serialize_str(&self.to_string())
27    }
28}
29
30impl fmt::Display for BlobOid {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        write!(f, "{}", self.0)
33    }
34}
35
36#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
37pub(crate) struct CommitOid(gix::ObjectId);
38
39impl CommitOid {
40    pub(crate) const fn new(oid: gix::ObjectId) -> Self {
41        Self(oid)
42    }
43}
44
45impl fmt::Display for CommitOid {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        write!(f, "{}", self.0)
48    }
49}
50
51#[derive(Clone, Copy, Debug, Eq, PartialEq)]
52pub(crate) struct FileMode(EntryMode);
53
54impl FileMode {
55    pub(crate) const fn new(mode: EntryMode) -> Self {
56        Self(mode)
57    }
58
59    pub(crate) fn as_tree_entry_mode(self) -> String {
60        format!("{:o}", self.0)
61    }
62
63    pub(crate) const fn is_reviewable_file(self) -> bool {
64        self.0.is_blob_or_symlink()
65    }
66
67    pub(crate) const fn is_submodule(self) -> bool {
68        self.0.is_commit()
69    }
70}
71
72#[derive(Clone, Debug)]
73pub(crate) struct TrackedFile {
74    pub(crate) path: RepoPath,
75    pub(crate) blob: BlobOid,
76    pub(crate) mode: FileMode,
77}
78
79#[derive(Clone, Debug)]
80pub(crate) struct WorkspaceFile {
81    pub(crate) head: TrackedFile,
82    pub(crate) workspace: TrackedFile,
83}