gix_pack/data/input/
types.rs

1use std::io;
2
3/// Returned by [`BytesToEntriesIter::new_from_header()`][crate::data::input::BytesToEntriesIter::new_from_header()] and as part
4/// of `Item` of [`BytesToEntriesIter`][crate::data::input::BytesToEntriesIter].
5#[derive(thiserror::Error, Debug)]
6#[allow(missing_docs)]
7pub enum Error {
8    #[error("An IO operation failed while streaming an entry")]
9    Io(#[from] io::Error),
10    #[error(transparent)]
11    PackParse(#[from] crate::data::header::decode::Error),
12    #[error("pack checksum in trailer was {expected}, but actual checksum was {actual}")]
13    ChecksumMismatch {
14        expected: gix_hash::ObjectId,
15        actual: gix_hash::ObjectId,
16    },
17    #[error("pack is incomplete: it was decompressed into {actual} bytes but {expected} bytes where expected.")]
18    IncompletePack { actual: u64, expected: u64 },
19    #[error("The object {object_id} could not be decoded or wasn't found")]
20    NotFound { object_id: gix_hash::ObjectId },
21}
22
23/// Iteration Mode
24#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
25#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
26pub enum Mode {
27    /// Provide the trailer as read from the pack
28    AsIs,
29    /// Generate an own hash and trigger an error on the last iterated object
30    /// if it does not match the hash provided with the pack.
31    ///
32    /// This way the one iterating the data cannot miss corruption as long as
33    /// the iteration is continued through to the end.
34    Verify,
35    /// Generate an own hash and if there was an error or the objects are depleted early
36    /// due to partial packs, return the last valid entry and with our own hash thus far.
37    /// Note that the existing pack hash, if present, will be ignored.
38    /// As we won't know which objects fails, every object will have the hash obtained thus far.
39    /// This also means that algorithms must know about this possibility, or else might wrongfully
40    /// assume the pack is finished.
41    Restore,
42}
43
44/// Define what to do with the compressed bytes portion of a pack [`Entry`][super::Entry]
45#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
46#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
47pub enum EntryDataMode {
48    /// Do nothing with the compressed bytes we read
49    Ignore,
50    /// Only create a CRC32 of the entry, otherwise similar to `Ignore`
51    Crc32,
52    /// Keep them and pass them along in a newly allocated buffer
53    Keep,
54    /// As above, but also compute a CRC32
55    KeepAndCrc32,
56}
57
58impl EntryDataMode {
59    /// Returns true if a crc32 should be computed
60    pub fn crc32(&self) -> bool {
61        match self {
62            EntryDataMode::KeepAndCrc32 | EntryDataMode::Crc32 => true,
63            EntryDataMode::Keep | EntryDataMode::Ignore => false,
64        }
65    }
66    /// Returns true if compressed bytes should be kept
67    pub fn keep(&self) -> bool {
68        match self {
69            EntryDataMode::Keep | EntryDataMode::KeepAndCrc32 => true,
70            EntryDataMode::Ignore | EntryDataMode::Crc32 => false,
71        }
72    }
73}