Skip to main content

yazi_shared/path/
like.rs

1use std::borrow::Cow;
2
3use anyhow::Result;
4
5use crate::{Utf8BytePredictor, path::{AsPath, Components, Display, EndsWithError, JoinError, PathBufDyn, PathCow, PathDyn, PathDynError, PathKind, RsplitOnceError, StartsWithError, StripPrefixError, StripSuffixError}, strand::{AsStrand, Strand}};
6
7pub trait PathLike: AsPath {
8	fn as_os(&self) -> Result<&std::path::Path, PathDynError> { self.as_path().as_os() }
9
10	fn as_unix(&self) -> Result<&typed_path::UnixPath, PathDynError> { self.as_path().as_unix() }
11
12	fn components(&self) -> Components<'_> { self.as_path().components() }
13
14	fn display(&self) -> Display<'_> { self.as_path().display() }
15
16	fn encoded_bytes(&self) -> &[u8] { self.as_path().encoded_bytes() }
17
18	fn ext(&self) -> Option<Strand<'_>> { self.as_path().ext() }
19
20	fn has_root(&self) -> bool { self.as_path().has_root() }
21
22	fn is_absolute(&self) -> bool { self.as_path().is_absolute() }
23
24	fn is_empty(&self) -> bool { self.as_path().is_empty() }
25
26	#[cfg(unix)]
27	fn is_hidden(&self) -> bool { self.as_path().is_hidden() }
28
29	fn kind(&self) -> PathKind { self.as_path().kind() }
30
31	fn len(&self) -> usize { self.as_path().len() }
32
33	fn name(&self) -> Option<Strand<'_>> { self.as_path().name() }
34
35	fn parent(&self) -> Option<PathDyn<'_>> { self.as_path().parent() }
36
37	fn rsplit_pred<T>(&self, pred: T) -> Option<(PathDyn<'_>, PathDyn<'_>)>
38	where
39		T: Utf8BytePredictor,
40	{
41		self.as_path().rsplit_pred(pred)
42	}
43
44	fn stem(&self) -> Option<Strand<'_>> { self.as_path().stem() }
45
46	fn to_os_owned(&self) -> Result<std::path::PathBuf, PathDynError> { self.as_path().to_os_owned() }
47
48	fn to_owned(&self) -> PathBufDyn { self.as_path().to_owned() }
49
50	fn to_str(&self) -> Result<&str, std::str::Utf8Error> { self.as_path().to_str() }
51
52	fn to_string_lossy(&self) -> Cow<'_, str> { self.as_path().to_string_lossy() }
53
54	fn try_ends_with<T>(&self, child: T) -> Result<bool, EndsWithError>
55	where
56		T: AsStrand,
57	{
58		self.as_path().try_ends_with(child)
59	}
60
61	fn try_join<T>(&self, path: T) -> Result<PathBufDyn, JoinError>
62	where
63		T: AsStrand,
64	{
65		self.as_path().try_join(path)
66	}
67
68	fn try_rsplit_seq<T>(&self, pat: T) -> Result<(PathDyn<'_>, PathDyn<'_>), RsplitOnceError>
69	where
70		T: AsStrand,
71	{
72		self.as_path().try_rsplit_seq(pat)
73	}
74
75	fn try_starts_with<T>(&self, base: T) -> Result<bool, StartsWithError>
76	where
77		T: AsStrand,
78	{
79		self.as_path().try_starts_with(base)
80	}
81
82	fn try_strip_prefix<T>(&self, base: T) -> Result<PathDyn<'_>, StripPrefixError>
83	where
84		T: AsStrand,
85	{
86		self.as_path().try_strip_prefix(base)
87	}
88
89	fn try_strip_suffix<T>(&self, suffix: T) -> Result<PathDyn<'_>, StripSuffixError>
90	where
91		T: AsStrand,
92	{
93		self.as_path().try_strip_suffix(suffix)
94	}
95}
96
97impl<P> From<&P> for PathBufDyn
98where
99	P: PathLike,
100{
101	fn from(value: &P) -> Self { value.to_owned() }
102}
103
104impl PathLike for PathBufDyn {}
105impl PathLike for PathCow<'_> {}