gix_refspec/match_group/types.rs
1use std::borrow::Cow;
2
3use bstr::BStr;
4use gix_hash::oid;
5
6use crate::RefSpecRef;
7
8/// A match group is able to match a list of ref specs in order while handling negation, conflicts and one to many mappings.
9#[derive(Default, Debug, Clone)]
10pub struct MatchGroup<'a> {
11 /// The specs that take part in item matching.
12 pub specs: Vec<RefSpecRef<'a>>,
13}
14
15///
16pub mod match_lhs {
17 use crate::match_group::Mapping;
18 use crate::MatchGroup;
19
20 /// The outcome of any matching operation of a [`MatchGroup`].
21 ///
22 /// It's used to validate and process the contained [mappings](Mapping).
23 #[derive(Debug, Clone)]
24 pub struct Outcome<'spec, 'item> {
25 /// The match group that produced this outcome.
26 pub group: MatchGroup<'spec>,
27 /// The mappings derived from matching [items](crate::match_group::Item).
28 pub mappings: Vec<Mapping<'item, 'spec>>,
29 }
30}
31
32///
33pub mod match_rhs {
34 use crate::match_group::Mapping;
35 use crate::MatchGroup;
36
37 /// The outcome of any matching operation of a [`MatchGroup`].
38 ///
39 /// It's used to validate and process the contained [mappings](Mapping).
40 #[derive(Debug, Clone)]
41 pub struct Outcome<'spec, 'item> {
42 /// The match group that produced this outcome.
43 pub group: MatchGroup<'spec>,
44 /// The mappings derived from matching [items](crate::match_group::Item).
45 pub mappings: Vec<Mapping<'spec, 'item>>,
46 }
47}
48
49/// An item to match, input to various matching operations.
50#[derive(Debug, Copy, Clone)]
51pub struct Item<'a> {
52 /// The full name of the references, like `refs/heads/main`
53 pub full_ref_name: &'a BStr,
54 /// The id that `full_ref_name` points to, which typically is a commit, but can also be a tag object (or anything else).
55 pub target: &'a oid,
56 /// The object an annotated tag is pointing to, if `target` is an annotated tag.
57 pub object: Option<&'a oid>,
58}
59
60#[derive(Debug, Clone, PartialEq, Eq, Hash)]
61/// The source (or left-hand) side of a mapping.
62pub enum SourceRef<'a> {
63 /// A full reference name, which is expected to be valid.
64 ///
65 /// Validity, however, is not enforced here.
66 FullName(Cow<'a, BStr>),
67 /// The name of an object that is expected to exist on the remote side.
68 /// Note that it might not be advertised by the remote but part of the object graph,
69 /// and thus gets sent in the pack. The server is expected to fail unless the desired
70 /// object is present but at some time it is merely a request by the user.
71 ObjectId(gix_hash::ObjectId),
72}
73
74impl SourceRef<'_> {
75 /// Create a fully owned instance by consuming this one.
76 pub fn into_owned(self) -> Source {
77 match self {
78 SourceRef::ObjectId(id) => Source::ObjectId(id),
79 SourceRef::FullName(name) => Source::FullName(name.into_owned().into()),
80 }
81 }
82}
83
84impl std::fmt::Display for SourceRef<'_> {
85 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86 match self {
87 SourceRef::FullName(name) => name.fmt(f),
88 SourceRef::ObjectId(id) => id.fmt(f),
89 }
90 }
91}
92
93/// The source (or left-hand) side of a mapping, which owns its name.
94pub type Source = SourceRef<'static>;
95
96/// A mapping from a remote to a local refs for fetches or local to remote refs for pushes.
97///
98/// Mappings are like edges in a graph, initially without any constraints.
99#[derive(Debug, Clone)]
100pub struct Mapping<'a, 'b> {
101 /// The index into the initial `items` list that matched against a spec.
102 pub item_index: Option<usize>,
103 /// The name of the remote side for fetches or the local one for pushes that matched.
104 pub lhs: SourceRef<'a>,
105 /// The name of the local side for fetches or the remote one for pushes that corresponds to `lhs`, if available.
106 pub rhs: Option<Cow<'b, BStr>>,
107 /// The index of the matched ref-spec as seen from the match group.
108 pub spec_index: usize,
109}
110
111impl std::hash::Hash for Mapping<'_, '_> {
112 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
113 self.lhs.hash(state);
114 self.rhs.hash(state);
115 }
116}