1use std::cmp::Ordering;
2
3use crate::State;
4
5pub mod entries {
7 use bstr::BString;
8
9 #[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
24pub mod extensions {
26 use crate::extension;
27
28 pub fn no_find<'a>(_: &git_hash::oid, _: &'a mut Vec<u8>) -> Option<git_object::TreeRefIter<'a>> {
30 None
31 }
32
33 #[derive(Debug, thiserror::Error)]
35 #[allow(missing_docs)]
36 pub enum Error {
37 #[error(transparent)]
38 Tree(#[from] extension::tree::verify::Error),
39 }
40}
41
42impl State {
43 pub fn verify_entries(&self) -> Result<(), entries::Error> {
45 let mut previous = None::<&crate::Entry>;
46 for (idx, entry) in self.entries.iter().enumerate() {
47 if let Some(prev) = previous {
48 if prev.cmp(entry, self) != Ordering::Less {
49 return 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 }
60 Ok(())
61 }
62
63 pub fn verify_extensions<F>(&self, use_find: bool, find: F) -> Result<(), extensions::Error>
65 where
66 F: for<'a> FnMut(&git_hash::oid, &'a mut Vec<u8>) -> Option<git_object::TreeRefIter<'a>>,
67 {
68 self.tree().map(|t| t.verify(use_find, find)).transpose()?;
69 Ok(())
72 }
73}