Skip to main content

gix_object/
find.rs

1/// The error type returned by the [`Find`](crate::Find) trait.
2pub type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
3///
4pub mod existing {
5    use gix_hash::ObjectId;
6
7    /// The error returned by the [`find(…)`][crate::FindExt::find()] trait methods.
8    #[derive(Debug, thiserror::Error)]
9    #[expect(missing_docs)]
10    pub enum Error {
11        #[error(transparent)]
12        Find(crate::find::Error),
13        #[error("An object with id {} could not be found", .oid)]
14        NotFound { oid: ObjectId },
15    }
16}
17
18///
19pub mod existing_object {
20    use gix_hash::ObjectId;
21
22    /// The error returned by the various [`find_*()`][crate::FindExt::find_commit()] trait methods.
23    #[derive(Debug, thiserror::Error)]
24    #[expect(missing_docs)]
25    pub enum Error {
26        #[error(transparent)]
27        Find(crate::find::Error),
28        #[error("Could not decode object at {oid}")]
29        Decode {
30            oid: ObjectId,
31            source: crate::decode::Error,
32        },
33        #[error("An object with id {oid} could not be found")]
34        NotFound { oid: ObjectId },
35        #[error("Expected object of kind {expected} but got {actual} at {oid}")]
36        ObjectKind {
37            oid: ObjectId,
38            actual: crate::Kind,
39            expected: crate::Kind,
40        },
41    }
42}
43
44///
45pub mod existing_iter {
46    use gix_hash::ObjectId;
47
48    /// The error returned by the various [`find_*_iter()`][crate::FindExt::find_commit_iter()] trait methods.
49    #[derive(Debug, thiserror::Error)]
50    #[expect(missing_docs)]
51    pub enum Error {
52        #[error(transparent)]
53        Find(crate::find::Error),
54        #[error("An object with id {oid} could not be found")]
55        NotFound { oid: ObjectId },
56        #[error("Expected object of kind {expected} but got {actual} at {oid}")]
57        ObjectKind {
58            oid: ObjectId,
59            actual: crate::Kind,
60            expected: crate::Kind,
61        },
62    }
63}
64
65/// An implementation of object access traits that stores nothing and finds nothing.
66#[derive(Debug, Copy, Clone)]
67pub struct Never;
68
69impl super::FindHeader for Never {
70    fn try_header(&self, _id: &gix_hash::oid) -> Result<Option<crate::Header>, Error> {
71        Ok(None)
72    }
73}
74
75impl super::Find for Never {
76    fn try_find<'a>(&self, _id: &gix_hash::oid, _buffer: &'a mut Vec<u8>) -> Result<Option<crate::Data<'a>>, Error> {
77        Ok(None)
78    }
79}
80
81impl super::Exists for Never {
82    fn exists(&self, _id: &gix_hash::oid) -> bool {
83        false
84    }
85}
86
87impl super::Write for Never {
88    fn write_buf(&self, object: crate::Kind, from: &[u8]) -> Result<gix_hash::ObjectId, crate::write::Error> {
89        crate::compute_hash(gix_hash::Kind::default(), object, from).map_err(Into::into)
90    }
91
92    fn write_buf_with_known_id(
93        &self,
94        _object: crate::Kind,
95        _from: &[u8],
96        id: gix_hash::ObjectId,
97    ) -> Result<gix_hash::ObjectId, crate::write::Error> {
98        Ok(id)
99    }
100
101    fn write_stream(
102        &self,
103        kind: crate::Kind,
104        size: u64,
105        from: &mut dyn std::io::Read,
106    ) -> Result<gix_hash::ObjectId, crate::write::Error> {
107        crate::compute_stream_hash(
108            gix_hash::Kind::default(),
109            kind,
110            from,
111            size,
112            &mut gix_features::progress::Discard,
113            &std::sync::atomic::AtomicBool::new(false),
114        )
115        .map_err(Into::into)
116    }
117
118    fn write_stream_with_known_id(
119        &self,
120        _kind: crate::Kind,
121        mut size: u64,
122        from: &mut dyn std::io::Read,
123        id: gix_hash::ObjectId,
124    ) -> Result<gix_hash::ObjectId, crate::write::Error> {
125        let mut buf = [0u8; u16::MAX as usize];
126        while size != 0 {
127            let bytes = (size as usize).min(buf.len());
128            from.read_exact(&mut buf[..bytes])?;
129            size -= bytes as u64;
130        }
131        Ok(id)
132    }
133}