1use std::cmp::Ordering;
23use crate::State;
45///
6pub mod entries {
7use bstr::BString;
89/// The error returned by [State::verify_entries()][crate::State::verify_entries()].
10#[derive(Debug, thiserror::Error)]
11 #[allow(missing_docs)]
12pub enum Error {
13#[error("Entry '{current_path}' (stage = {current_stage}) at index {current_index} should order after prior entry '{previous_path}' (stage = {previous_stage})")]
14OutOfOrder {
15 current_index: usize,
16 current_path: BString,
17 current_stage: u8,
18 previous_path: BString,
19 previous_stage: u8,
20 },
21 }
22}
2324///
25pub mod extensions {
26use crate::extension;
2728/// An implementation of a `find` function that never finds or returns any object, a no-op.
29pub fn no_find<'a>(_: &git_hash::oid, _: &'a mut Vec<u8>) -> Option<git_object::TreeRefIter<'a>> {
30None
31}
3233/// The error returned by [State::verify_extensions()][crate::State::verify_extensions()].
34#[derive(Debug, thiserror::Error)]
35 #[allow(missing_docs)]
36pub enum Error {
37#[error(transparent)]
38Tree(#[from] extension::tree::verify::Error),
39 }
40}
4142impl State {
43/// Assure our entries are consistent.
44pub fn verify_entries(&self) -> Result<(), entries::Error> {
45let mut previous = None::<&crate::Entry>;
46for (idx, entry) in self.entries.iter().enumerate() {
47if let Some(prev) = previous {
48if prev.cmp(entry, self) != Ordering::Less {
49return Err(entries::Error::OutOfOrder {
50 current_index: idx,
51 current_path: entry.path(self).into(),
52 current_stage: entry.flags.stage() as u8,
53 previous_path: prev.path(self).into(),
54 previous_stage: prev.flags.stage() as u8,
55 });
56 }
57 }
58 previous = Some(entry);
59 }
60Ok(())
61 }
6263/// Note: `find` cannot be `Option<F>` as we can't call it with a closure then due to the indirection through `Some`.
64pub fn verify_extensions<F>(&self, use_find: bool, find: F) -> Result<(), extensions::Error>
65where
66F: for<'a> FnMut(&git_hash::oid, &'a mut Vec<u8>) -> Option<git_object::TreeRefIter<'a>>,
67 {
68self.tree().map(|t| t.verify(use_find, find)).transpose()?;
69// TODO: verify links by running the whole set of tests on the index
70 // - do that once we load it as well, or maybe that's lazy loaded? Too many questions for now.
71Ok(())
72 }
73}