crates/repo/src/git/refs/qualified.rs 6.2 KiB raw
1
// Copyright (c) Kim Altintop <kim@eagain.st>
2
// Copyright (c) The Radicle Team
3
4
//! Fully qualified Git refnames.
5
use super::*;
6
7
/// A fully qualified Git refname.
8
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
9
pub struct Qualified<'a>(pub(crate) Cow<'a, RefStr>);
10
11
impl<'a> Qualified<'a> {
12
    /// Creates a qualified refname from `refs/<a>/<b>/<tail...>`.
13
    pub fn from_components<'b, 'c, 'd, A, B, C>(a: A, b: B, tail: C) -> Self
14
    where
15
        A: Into<Component<'b>>,
16
        B: Into<Component<'c>>,
17
        C: IntoIterator<Item = Component<'d>>,
18
    {
19
        let mut inner = name::REFS.join(a.into()).and(b.into());
20
        inner.extend(tail);
21
        Self(inner.into())
22
    }
23
24
    /// Returns `refname` as qualified if it starts with `refs/` and has a category and name.
25
    pub fn from_refstr(refname: impl Into<Cow<'a, RefStr>>) -> Option<Self> {
26
        let refname = refname.into();
27
        let mut iter = refname.iter();
28
        match (iter.next(), iter.next(), iter.next()) {
29
            (Some("refs"), Some(_), Some(_)) => Some(Self(refname)),
30
            _ => None,
31
        }
32
    }
33
34
    /// Returns the qualified refname as a string.
35
    #[inline]
36
    pub fn as_str(&self) -> &str {
37
        self.as_ref()
38
    }
39
40
    /// Joins another refname suffix.
41
    #[inline]
42
    pub fn join<'b, R>(&self, other: R) -> Qualified<'b>
43
    where
44
        R: AsRef<RefStr>,
45
    {
46
        Qualified(self.0.join(other).into())
47
    }
48
49
    /// Appends a refspec pattern suffix.
50
    pub fn to_pattern<P>(&self, pattern: P) -> refspec::QualifiedPattern<'_>
51
    where
52
        P: AsRef<refspec::PatternStr>,
53
    {
54
        refspec::QualifiedPattern(Cow::Owned(RefStr::to_pattern(self, pattern.as_ref())))
55
    }
56
57
    /// Returns this refname as namespaced if it has a namespace prefix.
58
    #[inline]
59
    pub fn to_namespaced(&self) -> Option<Namespaced<'_>> {
60
        self.0.as_ref().into()
61
    }
62
63
    /// Adds `refs/namespaces/<namespace>/` before this refname.
64
    pub fn with_namespace<'b>(&self, namespace: Component<'b>) -> Namespaced<'a> {
65
        Namespaced(Cow::Owned(
66
            IntoIterator::into_iter([lit::Refs.into(), lit::Namespaces.into(), namespace])
67
                .chain(self.0.components())
68
                .collect(),
69
        ))
70
    }
71
72
    /// Returns the first three string components and the remaining iterator.
73
    #[allow(clippy::panic)]
74
    pub fn non_empty_iter(&self) -> (&str, &str, &str, name::Iter<'_>) {
75
        let mut iter = self.iter();
76
        let (Some(first), Some(second), Some(third)) = (iter.next(), iter.next(), iter.next())
77
        else {
78
            panic!("Qualified::non_empty_iter: missing component");
79
        };
80
        (first, second, third, iter)
81
    }
82
83
    /// Returns the first three components and the remaining iterator.
84
    #[allow(clippy::panic)]
85
    pub fn non_empty_components(
86
        &self,
87
    ) -> (
88
        Component<'_>,
89
        Component<'_>,
90
        Component<'_>,
91
        name::Components<'_>,
92
    ) {
93
        let mut components = self.components();
94
        let (Some(first), Some(second), Some(third)) =
95
            (components.next(), components.next(), components.next())
96
        else {
97
            panic!("Qualified::non_empty_components: missing component");
98
        };
99
        (first, second, third, components)
100
    }
101
102
    /// Returns an owned qualified refname.
103
    #[inline]
104
    pub fn to_owned<'b>(&self) -> Qualified<'b> {
105
        Qualified(Cow::Owned(self.0.clone().into_owned()))
106
    }
107
108
    /// Converts this value into an owned qualified refname.
109
    #[inline]
110
    pub fn into_owned<'b>(self) -> Qualified<'b> {
111
        Qualified(Cow::Owned(self.0.into_owned()))
112
    }
113
114
    /// Converts this qualified refname into an owned refname.
115
    #[inline]
116
    pub fn into_refstring(self) -> RefString {
117
        self.into()
118
    }
119
}
120
121
impl Deref for Qualified<'_> {
122
    type Target = RefStr;
123
124
    #[inline]
125
    fn deref(&self) -> &Self::Target {
126
        &self.0
127
    }
128
}
129
130
impl AsRef<RefStr> for Qualified<'_> {
131
    #[inline]
132
    fn as_ref(&self) -> &RefStr {
133
        self
134
    }
135
}
136
137
impl AsRef<str> for Qualified<'_> {
138
    #[inline]
139
    fn as_ref(&self) -> &str {
140
        self.0.as_str()
141
    }
142
}
143
144
impl AsRef<Self> for Qualified<'_> {
145
    #[inline]
146
    fn as_ref(&self) -> &Self {
147
        self
148
    }
149
}
150
151
impl<'a> From<Qualified<'a>> for Cow<'a, RefStr> {
152
    #[inline]
153
    fn from(qualified: Qualified<'a>) -> Self {
154
        qualified.0
155
    }
156
}
157
158
impl From<Qualified<'_>> for RefString {
159
    #[inline]
160
    fn from(qualified: Qualified<'_>) -> Self {
161
        qualified.0.into_owned()
162
    }
163
}
164
165
impl<T, U> From<(lit::Refs, T, U)> for Qualified<'_>
166
where
167
    T: AsRef<RefStr>,
168
    U: AsRef<RefStr>,
169
{
170
    #[inline]
171
    fn from((refs, category, name): (lit::Refs, T, U)) -> Self {
172
        let refs: &RefStr = refs.into();
173
        Self(Cow::Owned(refs.join(category).and(name)))
174
    }
175
}
176
177
impl<T> From<lit::RefsHeads<T>> for Qualified<'_>
178
where
179
    T: AsRef<RefStr>,
180
{
181
    #[inline]
182
    fn from((refs, heads, name): lit::RefsHeads<T>) -> Self {
183
        Self(Cow::Owned(
184
            IntoIterator::into_iter([Component::from(refs), heads.into()])
185
                .collect::<RefString>()
186
                .and(name),
187
        ))
188
    }
189
}
190
191
impl<T> From<lit::RefsTags<T>> for Qualified<'_>
192
where
193
    T: AsRef<RefStr>,
194
{
195
    #[inline]
196
    fn from((refs, tags, name): lit::RefsTags<T>) -> Self {
197
        Self(Cow::Owned(
198
            IntoIterator::into_iter([Component::from(refs), tags.into()])
199
                .collect::<RefString>()
200
                .and(name),
201
        ))
202
    }
203
}
204
205
impl<T> From<lit::RefsNotes<T>> for Qualified<'_>
206
where
207
    T: AsRef<RefStr>,
208
{
209
    #[inline]
210
    fn from((refs, notes, name): lit::RefsNotes<T>) -> Self {
211
        Self(Cow::Owned(
212
            IntoIterator::into_iter([Component::from(refs), notes.into()])
213
                .collect::<RefString>()
214
                .and(name),
215
        ))
216
    }
217
}
218
219
impl<T> From<lit::RefsRemotes<T>> for Qualified<'_>
220
where
221
    T: AsRef<RefStr>,
222
{
223
    #[inline]
224
    fn from((refs, remotes, name): lit::RefsRemotes<T>) -> Self {
225
        Self(Cow::Owned(
226
            IntoIterator::into_iter([Component::from(refs), remotes.into()])
227
                .collect::<RefString>()
228
                .and(name),
229
        ))
230
    }
231
}
232
233
impl Display for Qualified<'_> {
234
    #[inline]
235
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
236
        self.0.fmt(f)
237
    }
238
}