gix_index/
verify.rs

1use std::cmp::Ordering;
2
3use crate::State;
4
5///
6pub mod entries {
7    use bstr::BString;
8
9    /// The error returned by [`State::verify_entries()`][crate::State::verify_entries()].
10    #[derive(Debug, thiserror::Error)]
11    #[allow(missing_docs)]
12    pub enum Error {
13        #[error("Entry '{current_path}' (stage = {current_stage}) at index {current_index} should order after prior entry '{previous_path}' (stage = {previous_stage})")]
14        OutOfOrder {
15            current_index: usize,
16            current_path: BString,
17            current_stage: u8,
18            previous_path: BString,
19            previous_stage: u8,
20        },
21    }
22}
23
24///
25pub mod extensions {
26    use crate::extension;
27
28    /// The error returned by [`State::verify_extensions()`][crate::State::verify_extensions()].
29    #[derive(Debug, thiserror::Error)]
30    #[allow(missing_docs)]
31    pub enum Error {
32        #[error(transparent)]
33        Tree(#[from] extension::tree::verify::Error),
34    }
35}
36
37impl State {
38    /// Assure our entries are consistent.
39    pub fn verify_entries(&self) -> Result<(), entries::Error> {
40        let _span = gix_features::trace::coarse!("gix_index::File::verify_entries()");
41        let mut previous = None::<&crate::Entry>;
42        for (idx, entry) in self.entries.iter().enumerate() {
43            if let Some(prev) = previous {
44                if prev.cmp(entry, self) != Ordering::Less {
45                    return Err(entries::Error::OutOfOrder {
46                        current_index: idx,
47                        current_path: entry.path(self).into(),
48                        current_stage: entry.flags.stage() as u8,
49                        previous_path: prev.path(self).into(),
50                        previous_stage: prev.flags.stage() as u8,
51                    });
52                }
53            }
54            previous = Some(entry);
55        }
56        Ok(())
57    }
58
59    /// Note: `objects` cannot be `Option<F>` as we can't call it with a closure then due to the indirection through `Some`.
60    pub fn verify_extensions(&self, use_find: bool, objects: impl gix_object::Find) -> Result<(), extensions::Error> {
61        self.tree().map(|t| t.verify(use_find, objects)).transpose()?;
62        // TODO: verify links by running the whole set of tests on the index
63        //       - do that once we load it as well, or maybe that's lazy loaded? Too many questions for now.
64        Ok(())
65    }
66}