crates/repo/src/git/refs/lit.rs 5.7 KiB raw
1
// Copyright (c) Kim Altintop <kim@eagain.st>
2
// Copyright (c) The Radicle Team
3
4
//! Known literal refname components.
5
use super::*;
6
7
/// A known literal [`RefStr`].
8
pub trait Lit: Sized + sealed::Sealed {
9
    /// The literal value.
10
    const SELF: Self;
11
    /// The literal refname string.
12
    const NAME: &'static RefStr;
13
14
    /// Converts a component into this literal when it matches.
15
    #[inline]
16
    fn from_component(component: &name::Component<'_>) -> Option<Self> {
17
        (component.as_ref() == Self::NAME).then_some(Self::SELF)
18
    }
19
}
20
21
impl<T: Lit> From<T> for &'static RefStr {
22
    #[inline]
23
    fn from(_: T) -> Self {
24
        T::NAME
25
    }
26
}
27
28
/// Seals the literal trait.
29
mod sealed {
30
    /// Marker trait for literal implementors.
31
    pub trait Sealed {}
32
}
33
34
/// A known literal component.
35
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
36
pub enum KnownLit {
37
    /// The `refs` component.
38
    Refs,
39
    /// The `heads` component.
40
    Heads,
41
    /// The `namespaces` component.
42
    Namespaces,
43
    /// The `remotes` component.
44
    Remotes,
45
    /// The `tags` component.
46
    Tags,
47
    /// The `notes` component.
48
    Notes,
49
}
50
51
impl KnownLit {
52
    /// Returns the known literal matching `component`.
53
    pub fn from_component(component: &name::Component<'_>) -> Option<Self> {
54
        let component: &RefStr = component.as_ref();
55
        if component == Refs::NAME {
56
            Some(Self::Refs)
57
        } else if component == Heads::NAME {
58
            Some(Self::Heads)
59
        } else if component == Namespaces::NAME {
60
            Some(Self::Namespaces)
61
        } else if component == Remotes::NAME {
62
            Some(Self::Remotes)
63
        } else if component == Tags::NAME {
64
            Some(Self::Tags)
65
        } else if component == Notes::NAME {
66
            Some(Self::Notes)
67
        } else {
68
            None
69
        }
70
    }
71
}
72
73
impl From<KnownLit> for name::Component<'static> {
74
    #[inline]
75
    fn from(lit: KnownLit) -> Self {
76
        match lit {
77
            KnownLit::Refs => Refs.into(),
78
            KnownLit::Heads => Heads.into(),
79
            KnownLit::Namespaces => Namespaces.into(),
80
            KnownLit::Remotes => Remotes.into(),
81
            KnownLit::Tags => Tags.into(),
82
            KnownLit::Notes => Notes.into(),
83
        }
84
    }
85
}
86
87
/// Either a known literal or any component.
88
pub enum SomeLit<'a> {
89
    /// A known literal.
90
    Known(KnownLit),
91
    /// Any other component.
92
    Any(name::Component<'a>),
93
}
94
95
impl SomeLit<'_> {
96
    /// Returns the known literal, if this value is known.
97
    pub fn known(self) -> Option<KnownLit> {
98
        match self {
99
            Self::Known(lit) => Some(lit),
100
            Self::Any(_) => None,
101
        }
102
    }
103
}
104
105
impl<'a> From<name::Component<'a>> for SomeLit<'a> {
106
    #[inline]
107
    fn from(component: name::Component<'a>) -> Self {
108
        match KnownLit::from_component(&component) {
109
            Some(lit) => Self::Known(lit),
110
            None => Self::Any(component),
111
        }
112
    }
113
}
114
115
/// Literal tuple for `refs/heads/<name>`.
116
pub type RefsHeads<T> = (Refs, Heads, T);
117
/// Literal tuple for `refs/tags/<name>`.
118
pub type RefsTags<T> = (Refs, Tags, T);
119
/// Literal tuple for `refs/notes/<name>`.
120
pub type RefsNotes<T> = (Refs, Notes, T);
121
/// Literal tuple for `refs/remotes/<name>`.
122
pub type RefsRemotes<T> = (Refs, Remotes, T);
123
/// Literal tuple for `refs/namespaces/<namespace>/<qualified>`.
124
pub type RefsNamespaces<'a, T> = (Refs, Namespaces, T, Qualified<'a>);
125
126
/// Creates a `refs/heads/<name>` literal tuple.
127
#[inline]
128
pub fn refs_heads<T: AsRef<RefStr>>(name: T) -> RefsHeads<T> {
129
    (Refs, Heads, name)
130
}
131
132
/// Creates a `refs/tags/<name>` literal tuple.
133
#[inline]
134
pub fn refs_tags<T: AsRef<RefStr>>(name: T) -> RefsTags<T> {
135
    (Refs, Tags, name)
136
}
137
138
/// Creates a `refs/notes/<name>` literal tuple.
139
#[inline]
140
pub fn refs_notes<T: AsRef<RefStr>>(name: T) -> RefsNotes<T> {
141
    (Refs, Notes, name)
142
}
143
144
/// Creates a `refs/remotes/<name>` literal tuple.
145
#[inline]
146
pub fn refs_remotes<T: AsRef<RefStr>>(name: T) -> RefsRemotes<T> {
147
    (Refs, Remotes, name)
148
}
149
150
/// Creates a `refs/namespaces/<namespace>/<qualified>` literal tuple.
151
#[inline]
152
pub fn refs_namespaces<'a, 'b, T>(namespace: T, name: Qualified<'b>) -> RefsNamespaces<'b, T>
153
where
154
    T: Into<name::Component<'a>>,
155
{
156
    (Refs, Namespaces, namespace, name)
157
}
158
159
/// The `refs` literal.
160
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
161
pub struct Refs;
162
impl Lit for Refs {
163
    const SELF: Self = Self;
164
    const NAME: &'static RefStr = name::REFS;
165
}
166
impl sealed::Sealed for Refs {}
167
168
/// The `heads` literal.
169
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
170
pub struct Heads;
171
impl Lit for Heads {
172
    const SELF: Self = Self;
173
    const NAME: &'static RefStr = name::HEADS;
174
}
175
impl sealed::Sealed for Heads {}
176
177
/// The `namespaces` literal.
178
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
179
pub struct Namespaces;
180
impl Lit for Namespaces {
181
    const SELF: Self = Self;
182
    const NAME: &'static RefStr = name::NAMESPACES;
183
}
184
impl sealed::Sealed for Namespaces {}
185
186
/// The `remotes` literal.
187
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
188
pub struct Remotes;
189
impl Lit for Remotes {
190
    const SELF: Self = Self;
191
    const NAME: &'static RefStr = name::REMOTES;
192
}
193
impl sealed::Sealed for Remotes {}
194
195
/// The `tags` literal.
196
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
197
pub struct Tags;
198
impl Lit for Tags {
199
    const SELF: Self = Self;
200
    const NAME: &'static RefStr = name::TAGS;
201
}
202
impl sealed::Sealed for Tags {}
203
204
/// The `notes` literal.
205
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
206
pub struct Notes;
207
impl Lit for Notes {
208
    const SELF: Self = Self;
209
    const NAME: &'static RefStr = name::NOTES;
210
}
211
impl sealed::Sealed for Notes {}