gix_protocol/handshake/refs/
mod.rs

1use bstr::BStr;
2
3use super::Ref;
4
5///
6pub mod parse {
7    use bstr::BString;
8
9    /// The error returned when parsing References/refs from the server response.
10    #[derive(Debug, thiserror::Error)]
11    #[allow(missing_docs)]
12    pub enum Error {
13        #[error(transparent)]
14        Io(#[from] std::io::Error),
15        #[error(transparent)]
16        DecodePacketline(#[from] gix_transport::packetline::decode::Error),
17        #[error(transparent)]
18        Id(#[from] gix_hash::decode::Error),
19        #[error("{symref:?} could not be parsed. A symref is expected to look like <NAME>:<target>.")]
20        MalformedSymref { symref: BString },
21        #[error("{0:?} could not be parsed. A V1 ref line should be '<hex-hash> <path>'.")]
22        MalformedV1RefLine(BString),
23        #[error(
24            "{0:?} could not be parsed. A V2 ref line should be '<hex-hash> <path>[ (peeled|symref-target):<value>'."
25        )]
26        MalformedV2RefLine(BString),
27        #[error("The ref attribute {attribute:?} is unknown. Found in line {line:?}")]
28        UnknownAttribute { attribute: BString, line: BString },
29        #[error("{message}")]
30        InvariantViolation { message: &'static str },
31    }
32}
33
34impl Ref {
35    /// Provide shared fields referring to the ref itself, namely `(name, target, [peeled])`.
36    /// In case of peeled refs, the tag object itself is returned as it is what the ref directly refers to, and target of the tag is returned
37    /// as `peeled`.
38    /// If `unborn`, the first object id will be the null oid.
39    pub fn unpack(&self) -> (&BStr, Option<&gix_hash::oid>, Option<&gix_hash::oid>) {
40        match self {
41            Ref::Direct { full_ref_name, object } => (full_ref_name.as_ref(), Some(object), None),
42            Ref::Symbolic {
43                full_ref_name,
44                tag,
45                object,
46                ..
47            } => (
48                full_ref_name.as_ref(),
49                Some(tag.as_deref().unwrap_or(object)),
50                tag.as_deref().map(|_| object.as_ref()),
51            ),
52            Ref::Peeled {
53                full_ref_name,
54                tag: object,
55                object: peeled,
56            } => (full_ref_name.as_ref(), Some(object), Some(peeled)),
57            Ref::Unborn {
58                full_ref_name,
59                target: _,
60            } => (full_ref_name.as_ref(), None, None),
61        }
62    }
63}
64
65#[cfg(any(feature = "blocking-client", feature = "async-client"))]
66pub(crate) mod shared;
67
68#[cfg(feature = "async-client")]
69mod async_io;
70#[cfg(feature = "async-client")]
71pub use async_io::{from_v1_refs_received_as_part_of_handshake_and_capabilities, from_v2_refs};
72
73#[cfg(feature = "blocking-client")]
74mod blocking_io;
75#[cfg(feature = "blocking-client")]
76pub use blocking_io::{from_v1_refs_received_as_part_of_handshake_and_capabilities, from_v2_refs};