1use crate::SPath;
2use derive_more::{Display, From};
3use std::io;
4use std::path::Path;
5use std::time::SystemTimeError;
6
7pub type Result<T> = core::result::Result<T, Error>;
8
9#[derive(Debug, Display)]
10pub enum Error {
11 #[display("Path is not valid UTF-8: '{_0}'")]
13 PathNotUtf8(String),
14 #[display("Path has no file name: '{_0}'")]
15 PathHasNoFileName(String),
16 #[display("Strip Prefix fail. Path '{path}' does not a base of '{prefix}'")]
17 StripPrefix {
18 prefix: String,
19 path: String,
20 },
21
22 #[display("File not found at path: '{_0}'")]
24 FileNotFound(String),
25 #[display("Cannot open file '{}'\nCause: {}", _0.path, _0.cause)]
26 FileCantOpen(PathAndCause),
27 #[display("Cannot read path '{}'\nCause: {}", _0.path, _0.cause)]
28 FileCantRead(PathAndCause),
29 #[display("Cannot write file '{}'\nCause: {}", _0.path, _0.cause)]
30 FileCantWrite(PathAndCause),
31 #[display("Cannot create file '{}'\nCause: {}", _0.path, _0.cause)]
32 FileCantCreate(PathAndCause),
33 #[display("File path has no parent directory: '{_0}'")]
34 FileHasNoParent(String),
35
36 #[display("Cannot sort by globs.\nCause: {cause}")]
38 SortByGlobs {
39 cause: String,
40 },
41
42 #[display("Cannot get metadata for path '{}'\nCause: {}", _0.path, _0.cause)]
44 CantGetMetadata(PathAndCause),
45 #[display("Cannot get 'modified' metadata for path '{}'\nCause: {}", _0.path, _0.cause)]
46 CantGetMetadataModified(PathAndCause),
47
48 #[display("Cannot get duration from system time. Cause: {_0}")]
50 CantGetDurationSystemTimeError(SystemTimeError),
51
52 #[display("Cannot create directory (and parents) '{}'\nCause: {}", _0.path, _0.cause)]
54 DirCantCreateAll(PathAndCause),
55
56 #[display("Path is invalid: '{}'\nCause: {}",_0.path, _0.cause)]
58 PathNotValidForPath(PathAndCause),
59
60 #[display("Cannot create glob pattern '{glob}'.\nCause: {cause}")]
62 GlobCantNew {
63 glob: String,
64 cause: globset::Error,
65 },
66 #[display("Cannot build glob set from '{globs:?}'.\nCause: {cause}")]
67 GlobSetCantBuild {
68 globs: Vec<String>,
69 cause: globset::Error,
70 },
71
72 #[display("Failed to watch path '{path}'.\nCause: {cause}")]
74 FailToWatch {
75 path: String,
76 cause: String,
77 },
78 #[display("Cannot watch path because it was not found: '{_0}'")]
79 CantWatchPathNotFound(String),
80
81 SpanInvalidStartAfterEnd,
83 SpanOutOfBounds,
84 SpanInvalidUtf8,
85
86 #[display("Cannot compute relative path from '{base}' to '{path}'")]
88 CannotDiff {
89 path: String,
90 base: String,
91 },
92 #[display("Cannot Canonicalize path '{}'\nCause: {}", _0.path, _0.cause)]
93 CannotCanonicalize(PathAndCause),
94
95 #[cfg(feature = "with-json")]
97 #[display("Cannot read json path '{}'\nCause: {}", _0.path, _0.cause)]
98 JsonCantRead(PathAndCause),
99 #[cfg(feature = "with-json")]
100 #[display("Cannot write JSON to path '{}'\nCause: {}", _0.path, _0.cause)]
101 JsonCantWrite(PathAndCause),
102 #[cfg(feature = "with-json")]
103 #[display("Error processing NDJSON: {_0}")]
104 NdJson(String),
105
106 #[cfg(feature = "with-toml")]
108 #[display("Cannot read TOML from path '{}'\nCause: {}", _0.path, _0.cause)]
109 TomlCantRead(PathAndCause),
110 #[cfg(feature = "with-toml")]
111 #[display("Cannot write TOML to path '{}'\nCause: {}", _0.path, _0.cause)]
112 TomlCantWrite(PathAndCause),
113}
114
115impl Error {
116 pub fn sort_by_globs(cause: impl std::fmt::Display) -> Error {
117 Error::SortByGlobs {
118 cause: cause.to_string(),
119 }
120 }
121}
122
123#[derive(Debug, Display, From)]
126pub enum Cause {
127 #[from]
128 Custom(String),
129
130 #[from]
131 Io(Box<io::Error>),
132
133 #[cfg(feature = "with-json")]
134 SerdeJson(Box<serde_json::Error>),
135
136 #[cfg(feature = "with-toml")]
137 TomlDe(Box<toml::de::Error>),
138
139 #[cfg(feature = "with-toml")]
140 TomlSer(Box<toml::ser::Error>),
141}
142
143#[derive(Debug)]
144pub struct PathAndCause {
145 path: String,
146 cause: Cause,
147}
148
149impl From<(&Path, io::Error)> for PathAndCause {
154 fn from(val: (&Path, io::Error)) -> Self {
155 PathAndCause {
156 path: val.0.to_string_lossy().to_string(),
157 cause: Cause::Io(Box::new(val.1)),
158 }
159 }
160}
161
162impl From<(&SPath, io::Error)> for PathAndCause {
163 fn from(val: (&SPath, io::Error)) -> Self {
164 PathAndCause {
165 path: val.0.to_string(),
166 cause: Cause::Io(Box::new(val.1)),
167 }
168 }
169}
170
171impl From<(&SPath, std::time::SystemTimeError)> for PathAndCause {
173 fn from(val: (&SPath, std::time::SystemTimeError)) -> Self {
174 PathAndCause {
175 path: val.0.to_string(),
176 cause: Cause::Custom(val.1.to_string()),
177 }
178 }
179}
180
181#[cfg(feature = "with-json")]
186impl From<(&Path, serde_json::Error)> for PathAndCause {
187 fn from(val: (&Path, serde_json::Error)) -> Self {
188 PathAndCause {
189 path: val.0.to_string_lossy().to_string(),
190 cause: Cause::SerdeJson(Box::new(val.1)),
191 }
192 }
193}
194
195#[cfg(feature = "with-toml")]
200impl From<(&Path, toml::de::Error)> for PathAndCause {
201 fn from(val: (&Path, toml::de::Error)) -> Self {
202 PathAndCause {
203 path: val.0.to_string_lossy().to_string(),
204 cause: Cause::TomlDe(Box::new(val.1)),
205 }
206 }
207}
208
209#[cfg(feature = "with-toml")]
210impl From<(&Path, toml::ser::Error)> for PathAndCause {
211 fn from(val: (&Path, toml::ser::Error)) -> Self {
212 PathAndCause {
213 path: val.0.to_string_lossy().to_string(),
214 cause: Cause::TomlSer(Box::new(val.1)),
215 }
216 }
217}
218
219impl std::error::Error for Error {}
224
225