Skip to main content

yazi_shared/path/
error.rs

1use thiserror::Error;
2
3use crate::strand::StrandError;
4
5// --- EndsWithError
6#[derive(Debug, Error)]
7#[error("calling ends_with on paths with different encodings")]
8pub enum EndsWithError {
9	FromStrand(#[from] StrandError),
10}
11
12// --- JoinError
13#[derive(Debug, Error)]
14#[error("calling join on paths with different encodings")]
15pub enum JoinError {
16	FromStrand(#[from] StrandError),
17	FromPathDyn(#[from] PathDynError),
18}
19
20impl From<StartsWithError> for JoinError {
21	fn from(err: StartsWithError) -> Self {
22		match err {
23			StartsWithError::FromStrand(e) => Self::FromStrand(e),
24		}
25	}
26}
27
28impl From<JoinError> for std::io::Error {
29	fn from(err: JoinError) -> Self { Self::other(err) }
30}
31
32// --- PathDynError
33#[derive(Debug, Error)]
34pub enum PathDynError {
35	#[error("conversion to OS path failed")]
36	AsOs,
37	#[error("conversion to Unix path failed")]
38	AsUnix,
39	#[error("conversion to UTF-8 path failed")]
40	AsUtf8,
41}
42
43impl From<StrandError> for PathDynError {
44	fn from(err: StrandError) -> Self {
45		match err {
46			StrandError::AsOs => Self::AsOs,
47			StrandError::AsUtf8 => Self::AsUtf8,
48		}
49	}
50}
51
52impl From<PathDynError> for std::io::Error {
53	fn from(err: PathDynError) -> Self { Self::other(err) }
54}
55
56// --- SetNameError
57#[derive(Debug, Error)]
58#[error("calling set_name on paths with different encodings")]
59pub enum SetNameError {
60	FromStrand(#[from] StrandError),
61}
62
63impl From<SetNameError> for std::io::Error {
64	fn from(err: SetNameError) -> Self { Self::other(err) }
65}
66
67// --- RsplitOnce
68#[derive(Error, Debug)]
69#[error("calling rsplit_once on paths with different encodings")]
70pub enum RsplitOnceError {
71	#[error("conversion to OsStr failed")]
72	AsOs,
73	#[error("conversion to UTF-8 str failed")]
74	AsUtf8,
75	#[error("the pattern was not found")]
76	NotFound,
77}
78
79impl From<StrandError> for RsplitOnceError {
80	fn from(err: StrandError) -> Self {
81		match err {
82			StrandError::AsOs => Self::AsOs,
83			StrandError::AsUtf8 => Self::AsUtf8,
84		}
85	}
86}
87
88// --- StartsWithError
89#[derive(Error, Debug)]
90#[error("calling starts_with on paths with different encodings")]
91pub enum StartsWithError {
92	FromStrand(#[from] StrandError),
93}
94
95// --- StripPrefixError
96#[derive(Debug, Error)]
97pub enum StripPrefixError {
98	#[error("calling strip_prefix on URLs with different schemes")]
99	Exotic,
100	#[error("the base is not a prefix of the path")]
101	NotPrefix,
102	#[error("calling strip_prefix on paths with different encodings")]
103	WrongEncoding,
104}
105
106impl From<StrandError> for StripPrefixError {
107	fn from(err: StrandError) -> Self {
108		match err {
109			StrandError::AsOs | StrandError::AsUtf8 => Self::WrongEncoding,
110		}
111	}
112}
113
114impl From<std::path::StripPrefixError> for StripPrefixError {
115	fn from(_: std::path::StripPrefixError) -> Self { Self::NotPrefix }
116}
117
118impl From<typed_path::StripPrefixError> for StripPrefixError {
119	fn from(_: typed_path::StripPrefixError) -> Self { Self::NotPrefix }
120}
121
122// --- StripSuffixError
123#[derive(Debug, Error)]
124pub enum StripSuffixError {
125	#[error("calling strip_suffix on URLs with different schemes")]
126	Exotic,
127	#[error("the base is not a suffix of the path")]
128	NotSuffix,
129	#[error("calling strip_suffix on paths with different encodings")]
130	WrongEncoding,
131}
132
133impl From<StrandError> for StripSuffixError {
134	fn from(err: StrandError) -> Self {
135		match err {
136			StrandError::AsOs | StrandError::AsUtf8 => Self::WrongEncoding,
137		}
138	}
139}