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

use crate::common::{Utf8Ancestors, Utf8Iter};
use crate::typed::Utf8TypedPath;
use crate::unix::Utf8UnixEncoding;
use crate::windows::Utf8WindowsEncoding;

/// An iterator over the [`Utf8TypedComponent`]s of a [`Utf8TypedPath`], as [`str`] slices.
///
/// This `struct` is created by the [`iter`] method on [`Utf8TypedPath`].
/// See its documentation for more.
///
/// [`iter`]: Utf8TypedPath::iter
/// [`Utf8TypedComponent`]: crate::Utf8TypedComponent
#[derive(Clone)]
pub enum Utf8TypedIter<'a> {
    Unix(Utf8Iter<'a, Utf8UnixEncoding>),
    Windows(Utf8Iter<'a, Utf8WindowsEncoding>),
}

impl<'a> Utf8TypedIter<'a> {
    /// Extracts a slice corresponding to the portion of the path remaining for iteration.
    ///
    /// # Examples
    ///
    /// ```
    /// use typed_path::Utf8TypedPath;
    ///
    /// let mut iter = Utf8TypedPath::derive("/tmp/foo/bar.txt").iter();
    /// iter.next();
    /// iter.next();
    ///
    /// assert_eq!(Utf8TypedPath::derive("foo/bar.txt"), iter.to_path());
    /// ```
    pub fn to_path(&self) -> Utf8TypedPath {
        match self {
            Self::Unix(it) => Utf8TypedPath::Unix(it.as_path()),
            Self::Windows(it) => Utf8TypedPath::Windows(it.as_path()),
        }
    }

    /// Returns reference to the underlying str slice represented by this iterator.
    pub fn as_str(&self) -> &str {
        impl_typed_fn!(self, as_ref)
    }
}

impl<'a> fmt::Debug for Utf8TypedIter<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        struct DebugHelper<'a>(Utf8TypedPath<'a>);

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

        f.debug_tuple(stringify!(Utf8TypedIter))
            .field(&DebugHelper(self.to_path()))
            .finish()
    }
}

impl<'a> AsRef<[u8]> for Utf8TypedIter<'a> {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        self.as_str().as_bytes()
    }
}

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

impl<'a> Iterator for Utf8TypedIter<'a> {
    type Item = &'a str;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        match self {
            Self::Unix(it) => it.next(),
            Self::Windows(it) => it.next(),
        }
    }
}

impl<'a> DoubleEndedIterator for Utf8TypedIter<'a> {
    #[inline]
    fn next_back(&mut self) -> Option<Self::Item> {
        match self {
            Self::Unix(it) => it.next_back(),
            Self::Windows(it) => it.next_back(),
        }
    }
}

impl<'a> FusedIterator for Utf8TypedIter<'a> {}

/// An iterator over [`Utf8TypedPath`] and its ancestors.
///
/// This `struct` is created by the [`ancestors`] method on [`Utf8TypedPath`].
/// See its documentation for more.
///
/// # Examples
///
/// ```
/// use typed_path::Utf8TypedPath;
///
/// let path = Utf8TypedPath::derive("/foo/bar");
///
/// for ancestor in path.ancestors() {
///     println!("{}", ancestor);
/// }
/// ```
///
/// [`ancestors`]: Utf8TypedPath::ancestors
#[derive(Copy, Clone, Debug)]
pub enum Utf8TypedAncestors<'a> {
    Unix(Utf8Ancestors<'a, Utf8UnixEncoding>),
    Windows(Utf8Ancestors<'a, Utf8WindowsEncoding>),
}

impl<'a> Iterator for Utf8TypedAncestors<'a> {
    type Item = Utf8TypedPath<'a>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        match self {
            Self::Unix(it) => it.next().map(Utf8TypedPath::Unix),
            Self::Windows(it) => it.next().map(Utf8TypedPath::Windows),
        }
    }
}

impl<'a> FusedIterator for Utf8TypedAncestors<'a> {}