summaryrefslogtreecommitdiffstats
path: root/vendor/gix-index/src/extension/decode.rs
blob: af032f4e3bad3beb9467b6ae67e1d42c4c0dd7c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use std::convert::TryInto;

use crate::{extension, extension::Signature, util::from_be_u32};

pub(crate) fn header(data: &[u8]) -> (Signature, u32, &[u8]) {
    let (signature, data) = data.split_at(4);
    let (size, data) = data.split_at(4);
    (signature.try_into().unwrap(), from_be_u32(size), data)
}

mod error {
    use crate::extension;

    /// The error returned when decoding extensions.
    #[derive(Debug, thiserror::Error)]
    #[allow(missing_docs)]
    pub enum Error {
        #[error(
            "Encountered mandatory extension '{}' which isn't implemented yet",
            String::from_utf8_lossy(signature)
        )]
        MandatoryUnimplemented { signature: extension::Signature },
        #[error("Could not parse mandatory link extension")]
        Link(#[from] extension::link::decode::Error),
    }
}
pub use error::Error;

pub(crate) fn all(
    maybe_beginning_of_extensions: &[u8],
    object_hash: gix_hash::Kind,
) -> Result<(Outcome, &[u8]), Error> {
    let mut ext_iter = match extension::Iter::new_without_checksum(maybe_beginning_of_extensions, object_hash) {
        Some(iter) => iter,
        None => return Ok((Outcome::default(), maybe_beginning_of_extensions)),
    };

    let mut ext = Outcome::default();
    for (signature, ext_data) in ext_iter.by_ref() {
        match signature {
            extension::tree::SIGNATURE => {
                ext.tree = extension::tree::decode(ext_data, object_hash);
            }
            extension::resolve_undo::SIGNATURE => {
                ext.resolve_undo = extension::resolve_undo::decode(ext_data, object_hash);
            }
            extension::untracked_cache::SIGNATURE => {
                ext.untracked = extension::untracked_cache::decode(ext_data, object_hash);
            }
            extension::fs_monitor::SIGNATURE => {
                ext.fs_monitor = extension::fs_monitor::decode(ext_data);
            }
            extension::end_of_index_entry::SIGNATURE => {} // skip already done
            extension::index_entry_offset_table::SIGNATURE => {} // not relevant/obtained already
            mandatory if mandatory[0].is_ascii_lowercase() => match mandatory {
                extension::link::SIGNATURE => ext.link = extension::link::decode(ext_data, object_hash)?.into(),
                extension::sparse::SIGNATURE => {
                    if !ext_data.is_empty() {
                        // only used as a marker, if this changes we need this implementation.
                        return Err(Error::MandatoryUnimplemented { signature: mandatory });
                    }
                    ext.is_sparse = true
                }
                unknown => return Err(Error::MandatoryUnimplemented { signature: unknown }),
            },
            _unknown => {} // skip unknown extensions, too
        }
    }
    Ok((ext, &maybe_beginning_of_extensions[ext_iter.consumed..]))
}

#[derive(Default)]
pub(crate) struct Outcome {
    pub tree: Option<extension::Tree>,
    pub link: Option<extension::Link>,
    pub resolve_undo: Option<extension::resolve_undo::Paths>,
    pub untracked: Option<extension::UntrackedCache>,
    pub fs_monitor: Option<extension::FsMonitor>,
    pub is_sparse: bool,
}