1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
// Copyright © 2021 The Radicle Link Contributors
//
// This file is part of radicle-link, distributed under the GPLv3 with Radicle
// Linking Exception. For full terms see the included LICENSE file.

use std::{
    borrow::{Borrow, Cow},
    convert::TryFrom,
    fmt::{self, Display, Write as _},
    iter::FromIterator,
    ops::Deref,
};

use thiserror::Error;

use crate::{check, lit, Namespaced, Qualified, RefStr, RefString};

mod iter;
pub use iter::{component, Component, Components, Iter};

pub const STAR: &PatternStr = PatternStr::from_str("*");

const CHECK_OPTS: check::Options = check::Options {
    allow_onelevel: true,
    allow_pattern: true,
};

#[repr(transparent)]
#[derive(Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub struct PatternStr(str);

impl PatternStr {
    #[inline]
    pub fn try_from_str(s: &str) -> Result<&Self, check::Error> {
        TryFrom::try_from(s)
    }

    #[inline]
    pub fn as_str(&self) -> &str {
        self
    }

    pub fn join<R>(&self, other: R) -> PatternString
    where
        R: AsRef<RefStr>,
    {
        self._join(other.as_ref())
    }

    fn _join(&self, other: &RefStr) -> PatternString {
        let mut buf = self.to_owned();
        buf.push(other);
        buf
    }

    #[inline]
    pub fn qualified(&self) -> Option<QualifiedPattern> {
        QualifiedPattern::from_patternstr(self)
    }

    #[inline]
    pub fn to_namespaced(&self) -> Option<NamespacedPattern> {
        self.into()
    }

    #[inline]
    pub fn iter(&self) -> Iter {
        self.0.split('/')
    }

    #[inline]
    pub fn components(&self) -> Components {
        Components::from(self)
    }

    pub(crate) const fn from_str(s: &str) -> &PatternStr {
        unsafe { &*(s as *const str as *const PatternStr) }
    }
}

impl Deref for PatternStr {
    type Target = str;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl AsRef<str> for PatternStr {
    #[inline]
    fn as_ref(&self) -> &str {
        self
    }
}

impl AsRef<Self> for PatternStr {
    #[inline]
    fn as_ref(&self) -> &Self {
        self
    }
}

impl<'a> TryFrom<&'a str> for &'a PatternStr {
    type Error = check::Error;

    #[inline]
    fn try_from(s: &'a str) -> Result<Self, Self::Error> {
        check::ref_format(CHECK_OPTS, s).map(|()| PatternStr::from_str(s))
    }
}

impl<'a> From<&'a RefStr> for &'a PatternStr {
    #[inline]
    fn from(rs: &'a RefStr) -> Self {
        PatternStr::from_str(rs.as_str())
    }
}

impl<'a> From<&'a PatternStr> for Cow<'a, PatternStr> {
    #[inline]
    fn from(p: &'a PatternStr) -> Cow<'a, PatternStr> {
        Cow::Borrowed(p)
    }
}

impl Display for PatternStr {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(self)
    }
}

#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub struct PatternString(pub(crate) String);

impl PatternString {
    #[inline]
    pub fn as_str(&self) -> &str {
        self.as_ref()
    }

    #[inline]
    pub fn as_pattern_str(&self) -> &PatternStr {
        self.as_ref()
    }

    #[inline]
    pub fn from_components<'a, T>(iter: T) -> Result<Self, DuplicateGlob>
    where
        T: IntoIterator<Item = Component<'a>>,
    {
        iter.into_iter().collect()
    }

    #[inline]
    pub fn and<R>(mut self, other: R) -> Self
    where
        R: AsRef<RefStr>,
    {
        self._push(other.as_ref());
        self
    }

    #[inline]
    pub fn push<R>(&mut self, other: R)
    where
        R: AsRef<RefStr>,
    {
        self._push(other.as_ref())
    }

    fn _push(&mut self, other: &RefStr) {
        self.0.push('/');
        self.0.push_str(other.as_str());
    }

    #[inline]
    pub fn pop(&mut self) -> bool {
        match self.0.rfind('/') {
            None => false,
            Some(idx) => {
                self.0.truncate(idx);
                true
            }
        }
    }
}

impl Deref for PatternString {
    type Target = PatternStr;

    #[inline]
    fn deref(&self) -> &Self::Target {
        self.borrow()
    }
}

impl AsRef<PatternStr> for PatternString {
    #[inline]
    fn as_ref(&self) -> &PatternStr {
        self
    }
}

impl AsRef<str> for PatternString {
    #[inline]
    fn as_ref(&self) -> &str {
        self.0.as_str()
    }
}

impl Borrow<PatternStr> for PatternString {
    #[inline]
    fn borrow(&self) -> &PatternStr {
        PatternStr::from_str(self.0.as_str())
    }
}

impl ToOwned for PatternStr {
    type Owned = PatternString;

    #[inline]
    fn to_owned(&self) -> Self::Owned {
        PatternString(self.0.to_owned())
    }
}

impl From<RefString> for PatternString {
    #[inline]
    fn from(rs: RefString) -> Self {
        Self(rs.into())
    }
}

impl<'a> From<&'a PatternString> for Cow<'a, PatternStr> {
    #[inline]
    fn from(p: &'a PatternString) -> Cow<'a, PatternStr> {
        Cow::Borrowed(p.as_ref())
    }
}

impl From<PatternString> for String {
    #[inline]
    fn from(p: PatternString) -> Self {
        p.0
    }
}

impl TryFrom<&str> for PatternString {
    type Error = check::Error;

    #[inline]
    fn try_from(s: &str) -> Result<Self, Self::Error> {
        PatternStr::try_from_str(s).map(ToOwned::to_owned)
    }
}

impl TryFrom<String> for PatternString {
    type Error = check::Error;

    #[inline]
    fn try_from(s: String) -> Result<Self, Self::Error> {
        check::ref_format(CHECK_OPTS, s.as_str()).map(|()| PatternString(s))
    }
}

#[derive(Debug, Error)]
#[error("more than one '*' encountered")]
pub struct DuplicateGlob;

impl<'a> FromIterator<Component<'a>> for Result<PatternString, DuplicateGlob> {
    fn from_iter<T>(iter: T) -> Self
    where
        T: IntoIterator<Item = Component<'a>>,
    {
        use Component::*;

        let mut buf = String::new();
        let mut seen_glob = false;
        for c in iter {
            if let Glob(_) = c {
                if seen_glob {
                    return Err(DuplicateGlob);
                }

                seen_glob = true;
            }

            buf.push_str(c.as_str());
            buf.push('/');
        }
        buf.truncate(buf.len() - 1);

        Ok(PatternString(buf))
    }
}

impl Display for PatternString {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(&self.0)
    }
}

/// A fully-qualified refspec.
///
/// A refspec is qualified _iff_ it starts with "refs/" and has at least three
/// components. This implies that a [`QualifiedPattern`] ref has a category,
/// such as "refs/heads/*".
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub struct QualifiedPattern<'a>(pub(crate) Cow<'a, PatternStr>);

impl<'a> QualifiedPattern<'a> {
    pub fn from_patternstr(r: impl Into<Cow<'a, PatternStr>>) -> Option<Self> {
        Self::_from_patternstr(r.into())
    }

    fn _from_patternstr(r: Cow<'a, PatternStr>) -> Option<Self> {
        let mut iter = r.iter();
        match (iter.next()?, iter.next()?, iter.next()?) {
            ("refs", _, _) => Some(QualifiedPattern(r)),
            _ => None,
        }
    }

    #[inline]
    pub fn as_str(&self) -> &str {
        self.as_ref()
    }

    #[inline]
    pub fn join<'b, R>(&self, other: R) -> QualifiedPattern<'b>
    where
        R: AsRef<RefStr>,
    {
        QualifiedPattern(Cow::Owned(self.0.join(other)))
    }

    #[inline]
    pub fn to_namespaced(&self) -> Option<NamespacedPattern> {
        self.0.as_ref().into()
    }

    /// Add a namespace.
    ///
    /// Creates a new [`NamespacedPattern`] by prefxing `self` with
    /// `refs/namespaces/<ns>`.
    pub fn with_namespace<'b>(
        &self,
        ns: Component<'b>,
    ) -> Result<NamespacedPattern<'a>, DuplicateGlob> {
        PatternString::from_components(
            IntoIterator::into_iter([lit::Refs.into(), lit::Namespaces.into(), ns])
                .chain(self.components()),
        )
        .map(|pat| NamespacedPattern(Cow::Owned(pat)))
    }

    /// Like [`Self::non_empty_components`], but with string slices.
    pub fn non_empty_iter(&self) -> (&str, &str, &str, Iter) {
        let mut iter = self.iter();
        (
            iter.next().unwrap(),
            iter.next().unwrap(),
            iter.next().unwrap(),
            iter,
        )
    }

    /// Return the first three [`Component`]s, and a possibly empty iterator
    /// over the remaining ones.
    ///
    /// A qualified ref is guaranteed to have at least three components, which
    /// this method provides a witness of. This is useful eg. for pattern
    /// matching on the prefix.
    pub fn non_empty_components(&self) -> (Component, Component, Component, Components) {
        let mut cs = self.components();
        (
            cs.next().unwrap(),
            cs.next().unwrap(),
            cs.next().unwrap(),
            cs,
        )
    }

    #[inline]
    pub fn to_owned<'b>(&self) -> QualifiedPattern<'b> {
        QualifiedPattern(Cow::Owned(self.0.clone().into_owned()))
    }

    #[inline]
    pub fn into_owned<'b>(self) -> QualifiedPattern<'b> {
        QualifiedPattern(Cow::Owned(self.0.into_owned()))
    }

    #[inline]
    pub fn into_patternstring(self) -> PatternString {
        self.into()
    }
}

impl Deref for QualifiedPattern<'_> {
    type Target = PatternStr;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl AsRef<PatternStr> for QualifiedPattern<'_> {
    #[inline]
    fn as_ref(&self) -> &PatternStr {
        self
    }
}

impl AsRef<str> for QualifiedPattern<'_> {
    #[inline]
    fn as_ref(&self) -> &str {
        self.0.as_str()
    }
}

impl AsRef<Self> for QualifiedPattern<'_> {
    #[inline]
    fn as_ref(&self) -> &Self {
        self
    }
}

impl Display for QualifiedPattern<'_> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl<'a> From<Qualified<'a>> for QualifiedPattern<'a> {
    #[inline]
    fn from(q: Qualified<'a>) -> Self {
        Self(Cow::Owned(q.into_refstring().into()))
    }
}

impl<'a> From<QualifiedPattern<'a>> for Cow<'a, PatternStr> {
    #[inline]
    fn from(q: QualifiedPattern<'a>) -> Self {
        q.0
    }
}

impl From<QualifiedPattern<'_>> for PatternString {
    #[inline]
    fn from(q: QualifiedPattern) -> Self {
        q.0.into_owned()
    }
}

/// A [`PatternString`] ref under a git namespace.
///
/// A ref is namespaced if it starts with "refs/namespaces/", another path
/// component, and "refs/". Eg.
///
///     refs/namespaces/xyz/refs/heads/main
///
/// Note that namespaces can be nested, so the result of
/// [`NamespacedPattern::strip_namespace`] may be convertible to a
/// [`NamespacedPattern`] again. For example:
///
/// ```no_run
/// let full = pattern!("refs/namespaces/a/refs/namespaces/b/refs/heads/*");
/// let namespaced = full.to_namespaced().unwrap();
/// let strip_first = namespaced.strip_namespace();
/// let nested = strip_first.namespaced().unwrap();
/// let strip_second = nested.strip_namespace();
///
/// assert_eq!("a", namespaced.namespace().as_str());
/// assert_eq!("b", nested.namespace().as_str());
/// assert_eq!("refs/namespaces/b/refs/heads/*", strip_first.as_str());
/// assert_eq!("refs/heads/*", strip_second.as_str());
/// ```
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub struct NamespacedPattern<'a>(Cow<'a, PatternStr>);

impl<'a> NamespacedPattern<'a> {
    pub fn namespace(&self) -> Component {
        self.components().nth(2).unwrap()
    }

    pub fn strip_namespace(&self) -> PatternString {
        PatternString::from_components(self.components().skip(3))
            .expect("BUG: NamespacedPattern was constructed with a duplicate glob")
    }

    pub fn strip_namespace_recursive(&self) -> PatternString {
        let mut strip = self.strip_namespace();
        while let Some(ns) = strip.to_namespaced() {
            strip = ns.strip_namespace();
        }
        strip
    }

    #[inline]
    pub fn to_owned<'b>(&self) -> NamespacedPattern<'b> {
        NamespacedPattern(Cow::Owned(self.0.clone().into_owned()))
    }

    #[inline]
    pub fn into_owned<'b>(self) -> NamespacedPattern<'b> {
        NamespacedPattern(Cow::Owned(self.0.into_owned()))
    }

    #[inline]
    pub fn into_qualified(self) -> QualifiedPattern<'a> {
        self.into()
    }
}

impl Deref for NamespacedPattern<'_> {
    type Target = PatternStr;

    #[inline]
    fn deref(&self) -> &Self::Target {
        self.borrow()
    }
}

impl AsRef<PatternStr> for NamespacedPattern<'_> {
    #[inline]
    fn as_ref(&self) -> &PatternStr {
        self.0.as_ref()
    }
}

impl AsRef<str> for NamespacedPattern<'_> {
    #[inline]
    fn as_ref(&self) -> &str {
        self.0.as_str()
    }
}

impl Borrow<PatternStr> for NamespacedPattern<'_> {
    #[inline]
    fn borrow(&self) -> &PatternStr {
        PatternStr::from_str(self.0.as_str())
    }
}

impl<'a> From<Namespaced<'a>> for NamespacedPattern<'a> {
    #[inline]
    fn from(ns: Namespaced<'a>) -> Self {
        NamespacedPattern(Cow::Owned(ns.to_ref_string().into()))
    }
}

impl<'a> From<NamespacedPattern<'a>> for QualifiedPattern<'a> {
    #[inline]
    fn from(ns: NamespacedPattern<'a>) -> Self {
        Self(ns.0)
    }
}

impl<'a> From<&'a PatternStr> for Option<NamespacedPattern<'a>> {
    fn from(rs: &'a PatternStr) -> Self {
        let mut cs = rs.iter();
        match (cs.next()?, cs.next()?, cs.next()?, cs.next()?) {
            ("refs", "namespaces", _, "refs") => Some(NamespacedPattern(Cow::from(rs))),

            _ => None,
        }
    }
}

impl Display for NamespacedPattern<'_> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.0.fmt(f)
    }
}

/// A Git [refspec].
///
/// **Note** that this is a simplified version of a [refspec] where
/// the `src` and `dst` are required and there is no way to construct
/// a negative refspec, e.g. `^refs/heads/no-thanks`.
///
/// [refspec]: https://git-scm.com/book/en/v2/Git-Internals-The-Refspec
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Refspec<T, U> {
    pub src: T,
    pub dst: U,
    pub force: bool,
}

impl<T, U> fmt::Display for Refspec<T, U>
where
    T: fmt::Display,
    U: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.force {
            f.write_char('+')?;
        }
        write!(f, "{}:{}", self.src, self.dst)
    }
}