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
// 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::Cow,
    fmt::{self, Display},
    ops::Deref,
};

use super::RefStr;
use crate::lit;

pub type Iter<'a> = std::str::Split<'a, char>;

/// Iterator created by the [`RefStr::iter`] method.
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[derive(Clone)]
pub struct Components<'a> {
    inner: std::str::Split<'a, char>,
}

impl<'a> Iterator for Components<'a> {
    type Item = Component<'a>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.inner
            .next()
            .map(RefStr::from_str)
            .map(Cow::from)
            .map(Component)
    }
}

impl<'a> DoubleEndedIterator for Components<'a> {
    #[inline]
    fn next_back(&mut self) -> Option<Self::Item> {
        self.inner
            .next_back()
            .map(RefStr::from_str)
            .map(Cow::from)
            .map(Component)
    }
}

impl<'a> From<&'a RefStr> for Components<'a> {
    #[inline]
    fn from(rs: &'a RefStr) -> Self {
        Self {
            inner: rs.as_str().split('/'),
        }
    }
}

/// A path component of a [`RefStr`].
///
/// A [`Component`] is a valid [`RefStr`] which does not contain any '/'
/// separators.
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub struct Component<'a>(Cow<'a, RefStr>);

impl<'a> Component<'a> {
    #[inline]
    pub fn from_refstr(r: impl Into<Cow<'a, RefStr>>) -> Option<Self> {
        let r = r.into();
        if !r.contains('/') {
            Some(Self(r))
        } else {
            None
        }
    }

    #[inline]
    pub fn as_lit<T: lit::Lit>(&self) -> Option<T> {
        T::from_component(self)
    }

    #[inline]
    pub fn into_inner(self) -> Cow<'a, RefStr> {
        self.0
    }
}

impl<'a> Deref for Component<'a> {
    type Target = RefStr;

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

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

impl<'a> From<&'a RefStr> for Option<Component<'a>> {
    #[inline]
    fn from(r: &'a RefStr) -> Self {
        if !r.contains('/') {
            Some(Component(Cow::from(r)))
        } else {
            None
        }
    }
}

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

impl<T: lit::Lit> From<T> for Component<'static> {
    #[inline]
    fn from(_: T) -> Self {
        Component(Cow::from(T::NAME))
    }
}

impl<'a> From<lit::SomeLit<'a>> for Component<'a> {
    #[inline]
    fn from(s: lit::SomeLit<'a>) -> Self {
        use lit::SomeLit::*;

        match s {
            Known(k) => k.into(),
            Any(c) => c,
        }
    }
}

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

pub mod component {
    use super::Component;
    use crate::name;
    use std::borrow::Cow;

    pub const HEADS: Component = Component(Cow::Borrowed(name::HEADS));
    pub const MAIN: Component = Component(Cow::Borrowed(name::MAIN));
    pub const MASTER: Component = Component(Cow::Borrowed(name::MASTER));
    pub const NAMESPACES: Component = Component(Cow::Borrowed(name::NAMESPACES));
    pub const NOTES: Component = Component(Cow::Borrowed(name::NOTES));
    pub const ORIGIN: Component = Component(Cow::Borrowed(name::ORIGIN));
    pub const REFS: Component = Component(Cow::Borrowed(name::REFS));
    pub const REMOTES: Component = Component(Cow::Borrowed(name::REMOTES));
    pub const TAGS: Component = Component(Cow::Borrowed(name::TAGS));
}