Skip to main content

yazi_shared/path/
components.rs

1use std::iter::FusedIterator;
2
3use typed_path::Components as _;
4
5use crate::{path::{Component, PathDyn}, strand::Strand};
6
7#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
8pub enum Components<'a> {
9	Os(std::path::Components<'a>),
10	Unix(typed_path::UnixComponents<'a>),
11}
12
13impl<'a> Components<'a> {
14	pub fn path(&self) -> PathDyn<'a> {
15		match self {
16			Self::Os(c) => PathDyn::Os(c.as_path()),
17			Self::Unix(c) => PathDyn::Unix(c.as_path()),
18		}
19	}
20
21	pub fn strand(&self) -> Strand<'a> {
22		match self {
23			Self::Os(c) => Strand::Os(c.as_path().as_os_str()),
24			Self::Unix(c) => Strand::Bytes(c.as_bytes()),
25		}
26	}
27}
28
29impl<'a> Iterator for Components<'a> {
30	type Item = Component<'a>;
31
32	fn next(&mut self) -> Option<Component<'a>> {
33		match self {
34			Self::Os(c) => c.next().map(Into::into),
35			Self::Unix(c) => c.next().map(Into::into),
36		}
37	}
38}
39
40impl<'a> DoubleEndedIterator for Components<'a> {
41	fn next_back(&mut self) -> Option<Component<'a>> {
42		match self {
43			Self::Os(c) => c.next_back().map(Into::into),
44			Self::Unix(c) => c.next_back().map(Into::into),
45		}
46	}
47}
48
49impl FusedIterator for Components<'_> {}