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
use core::fmt;
use core::iter::FusedIterator;
use core::marker::PhantomData;

use crate::{Utf8Component, Utf8Components, Utf8Encoding, Utf8Path};

/// An iterator over the [`Utf8Component`]s of a [`Utf8Path`], as [`str`] slices.
///
/// This `struct` is created by the [`iter`] method on [`Utf8Path`].
/// See its documentation for more.
///
/// [`iter`]: Utf8Path::iter
#[derive(Clone)]
pub struct Utf8Iter<'a, T>
where
    T: Utf8Encoding<'a>,
{
    _encoding: PhantomData<T>,
    inner: <T as Utf8Encoding<'a>>::Components,
}

impl<'a, T> Utf8Iter<'a, T>
where
    T: for<'enc> Utf8Encoding<'enc> + 'a,
{
    pub(crate) fn new(inner: <T as Utf8Encoding<'a>>::Components) -> Self {
        Self {
            _encoding: PhantomData,
            inner,
        }
    }

    /// Extracts a slice corresponding to the portion of the path remaining for iteration.
    ///
    /// # Examples
    ///
    /// ```
    /// use typed_path::{Utf8Path, Utf8UnixEncoding};
    ///
    /// // NOTE: A path cannot be created on its own without a defined encoding
    /// let mut iter = Utf8Path::<Utf8UnixEncoding>::new("/tmp/foo/bar.txt").iter();
    /// iter.next();
    /// iter.next();
    ///
    /// assert_eq!(Utf8Path::<Utf8UnixEncoding>::new("foo/bar.txt"), iter.as_path());
    /// ```
    pub fn as_path(&self) -> &Utf8Path<T> {
        Utf8Path::new(self.inner.as_str())
    }
}

impl<'a, T> fmt::Debug for Utf8Iter<'a, T>
where
    T: for<'enc> Utf8Encoding<'enc> + 'a,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        struct DebugHelper<'a, T>(&'a Utf8Path<T>)
        where
            T: for<'enc> Utf8Encoding<'enc>;

        impl<'a, T> fmt::Debug for DebugHelper<'a, T>
        where
            T: for<'enc> Utf8Encoding<'enc>,
        {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.debug_list().entries(self.0.iter()).finish()
            }
        }

        f.debug_tuple(stringify!(Iter))
            .field(&DebugHelper(self.as_path()))
            .finish()
    }
}

impl<'a, T> AsRef<Utf8Path<T>> for Utf8Iter<'a, T>
where
    T: for<'enc> Utf8Encoding<'enc> + 'a,
{
    #[inline]
    fn as_ref(&self) -> &Utf8Path<T> {
        self.as_path()
    }
}

impl<'a, T> AsRef<[u8]> for Utf8Iter<'a, T>
where
    T: for<'enc> Utf8Encoding<'enc> + 'a,
{
    #[inline]
    fn as_ref(&self) -> &[u8] {
        self.as_path().as_str().as_bytes()
    }
}

impl<'a, T> AsRef<str> for Utf8Iter<'a, T>
where
    T: for<'enc> Utf8Encoding<'enc> + 'a,
{
    #[inline]
    fn as_ref(&self) -> &str {
        self.as_path().as_str()
    }
}

impl<'a, T> Iterator for Utf8Iter<'a, T>
where
    T: for<'enc> Utf8Encoding<'enc> + 'a,
{
    type Item = &'a str;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        match self.inner.next() {
            Some(c) => Some(c.as_str()),
            None => None,
        }
    }
}

impl<'a, T> DoubleEndedIterator for Utf8Iter<'a, T>
where
    T: for<'enc> Utf8Encoding<'enc> + 'a,
{
    #[inline]
    fn next_back(&mut self) -> Option<Self::Item> {
        match self.inner.next_back() {
            Some(c) => Some(c.as_str()),
            None => None,
        }
    }
}

impl<'a, T> FusedIterator for Utf8Iter<'a, T> where T: for<'enc> Utf8Encoding<'enc> + 'a {}

/// An iterator over [`Utf8Path`] and its ancestors.
///
/// This `struct` is created by the [`ancestors`] method on [`Utf8Path`].
/// See its documentation for more.
///
/// # Examples
///
/// ```
/// use typed_path::{Utf8Path, Utf8UnixEncoding};
///
/// // NOTE: A path cannot be created on its own without a defined encoding
/// let path = Utf8Path::<Utf8UnixEncoding>::new("/foo/bar");
///
/// for ancestor in path.ancestors() {
///     println!("{}", ancestor);
/// }
/// ```
///
/// [`ancestors`]: Utf8Path::ancestors
#[derive(Copy, Clone, Debug)]
pub struct Utf8Ancestors<'a, T>
where
    T: for<'enc> Utf8Encoding<'enc>,
{
    pub(crate) next: Option<&'a Utf8Path<T>>,
}

impl<'a, T> Iterator for Utf8Ancestors<'a, T>
where
    T: for<'enc> Utf8Encoding<'enc>,
{
    type Item = &'a Utf8Path<T>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        let next = self.next;
        self.next = next.and_then(Utf8Path::parent);
        next
    }
}

impl<'a, T> FusedIterator for Utf8Ancestors<'a, T> where T: for<'enc> Utf8Encoding<'enc> {}