git_odb/
find.rs

1///
2pub mod existing {
3    use git_hash::ObjectId;
4
5    /// The error returned by the [`find(…)`][crate::FindExt::find()] trait methods.
6    #[derive(Debug, thiserror::Error)]
7    #[allow(missing_docs)]
8    pub enum Error<T: std::error::Error + 'static> {
9        #[error(transparent)]
10        Find(T),
11        #[error("An object with id {} could not be found", .oid)]
12        NotFound { oid: ObjectId },
13    }
14}
15
16///
17pub mod existing_object {
18    use git_hash::ObjectId;
19
20    /// The error returned by the various [`find_*()`][crate::FindExt::find_commit()] trait methods.
21    #[derive(Debug, thiserror::Error)]
22    #[allow(missing_docs)]
23    pub enum Error<T: std::error::Error + 'static> {
24        #[error(transparent)]
25        Find(T),
26        #[error(transparent)]
27        Decode(git_object::decode::Error),
28        #[error("An object with id {oid} could not be found")]
29        NotFound { oid: ObjectId },
30        #[error("Expected object of kind {expected}")]
31        ObjectKind { expected: git_object::Kind },
32    }
33}
34
35///
36pub mod existing_iter {
37    use git_hash::ObjectId;
38
39    /// The error returned by the various [`find_*_iter()`][crate::FindExt::find_commit_iter()] trait methods.
40    #[derive(Debug, thiserror::Error)]
41    #[allow(missing_docs)]
42    pub enum Error<T: std::error::Error + 'static> {
43        #[error(transparent)]
44        Find(T),
45        #[error("An object with id {oid} could not be found")]
46        NotFound { oid: ObjectId },
47        #[error("Expected object of kind {expected}")]
48        ObjectKind { expected: git_object::Kind },
49    }
50}
51
52/// An object header informing about object properties, without it being fully decoded in the process.
53#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
54pub enum Header {
55    /// The object was not packed, but is currently located in the loose object portion of the database.
56    ///
57    /// As packs are searched first, this means that in this very moment, the object whose header we retrieved is unique
58    /// in the object database.
59    Loose {
60        /// The kind of the object.
61        kind: git_object::Kind,
62        /// The size of the object's data in bytes.
63        size: u64,
64    },
65    /// The object was present in a pack.
66    ///
67    /// Note that this does not imply it is unique in the database, as it might be present in more than one pack and even
68    /// as loose object.
69    Packed(git_pack::data::decode::header::Outcome),
70}
71
72mod header {
73    use super::Header;
74
75    impl Header {
76        /// Return the object kind of the object we represent.
77        pub fn kind(&self) -> git_object::Kind {
78            match self {
79                Header::Packed(out) => out.kind,
80                Header::Loose { kind, .. } => *kind,
81            }
82        }
83        /// Return the size of the object in bytes.
84        pub fn size(&self) -> u64 {
85            match self {
86                Header::Packed(out) => out.object_size,
87                Header::Loose { size, .. } => *size,
88            }
89        }
90        /// Return the amount of deltas decoded to obtain this header, if the object was packed.
91        pub fn num_deltas(&self) -> Option<u32> {
92            match self {
93                Header::Packed(out) => out.num_deltas.into(),
94                Header::Loose { .. } => None,
95            }
96        }
97    }
98
99    impl From<git_pack::data::decode::header::Outcome> for Header {
100        fn from(packed_header: git_pack::data::decode::header::Outcome) -> Self {
101            Header::Packed(packed_header)
102        }
103    }
104
105    impl From<(usize, git_object::Kind)> for Header {
106        fn from((object_size, kind): (usize, git_object::Kind)) -> Self {
107            Header::Loose {
108                kind,
109                size: object_size as u64,
110            }
111        }
112    }
113}