simple_fs/
error.rs

1use derive_more::Display;
2use std::io;
3use std::path::Path;
4use std::time::SystemTimeError;
5
6pub type Result<T> = core::result::Result<T, Error>;
7
8#[derive(Debug, Display)]
9pub enum Error {
10	// -- Path
11	#[display("Path is not valid UTF-8: '{_0}'")]
12	PathNotUtf8(String),
13	#[display("Path has no file name: '{_0}'")]
14	PathHasNoFileName(String),
15
16	// -- File
17	#[display("File not found at path: '{_0}'")]
18	FileNotFound(String),
19	#[display("Cannot open file '{}'\nCause: {}", _0.path, _0.cause)]
20	FileCantOpen(PathAndCause),
21	#[display("Cannot read path '{}'\nCause: {}", _0.path, _0.cause)]
22	FileCantRead(PathAndCause),
23	#[display("Cannot write file '{}'\nCause: {}", _0.path, _0.cause)]
24	FileCantWrite(PathAndCause),
25	#[display("Cannot create file '{}'\nCause: {}", _0.path, _0.cause)]
26	FileCantCreate(PathAndCause),
27	#[display("File path has no parent directory: '{_0}'")]
28	FileHasNoParent(String),
29
30	// -- Metadata
31	#[display("Cannot get metadata for path '{}'\nCause: {}", _0.path, _0.cause)]
32	CantGetMetadata(PathAndCause),
33	#[display("Cannot get 'modified' metadata for path '{}'\nCause: {}", _0.path, _0.cause)]
34	CantGetMetadataModified(PathAndCause),
35
36	// -- Time
37	#[display("Cannot get duration from system time. Cause: {_0}")]
38	CantGetDurationSystemTimeError(SystemTimeError),
39
40	// -- Directory
41	#[display("Cannot create directory (and parents) '{}'\nCause: {}", _0.path, _0.cause)]
42	DirCantCreateAll(PathAndCause),
43
44	// -- Path Validations
45	#[display("Path is invalid: '{}'\nCause: {}",_0.path, _0.cause)]
46	PathNotValidForPath(PathAndCause),
47
48	// -- Glob
49	#[display("Cannot create glob pattern '{glob}'.\nCause: {cause}")]
50	GlobCantNew { glob: String, cause: globset::Error },
51	#[display("Cannot build glob set from '{globs:?}'.\nCause: {cause}")]
52	GlobSetCantBuild { globs: Vec<String>, cause: globset::Error },
53
54	// -- Watch
55	#[display("Failed to watch path '{path}'.\nCause: {cause}")]
56	FailToWatch { path: String, cause: String },
57	#[display("Cannot watch path because it was not found: '{_0}'")]
58	CantWatchPathNotFound(String),
59
60	// -- Other
61	#[display("Cannot compute relative path from '{base}' to '{path}'")]
62	CannotDiff { path: String, base: String },
63	#[display("Cannot Canonicalize path '{}'\nCause: {}", _0.path, _0.cause)]
64	CannotCanonicalize(PathAndCause),
65
66	// -- with-json
67	#[cfg(feature = "with-json")]
68	#[display("Cannot read json path '{}'\nCause: {}", _0.path, _0.cause)]
69	JsonCantRead(PathAndCause),
70	#[cfg(feature = "with-json")]
71	#[display("Cannot write JSON to path '{}'\nCause: {}", _0.path, _0.cause)]
72	JsonCantWrite(PathAndCause),
73	#[cfg(feature = "with-json")]
74	#[display("Error processing NDJSON: {_0}")]
75	NdJson(String),
76
77	// -- with-toml
78	#[cfg(feature = "with-toml")]
79	#[display("Cannot read TOML from path '{}'\nCause: {}", _0.path, _0.cause)]
80	TomlCantRead(PathAndCause),
81	#[cfg(feature = "with-toml")]
82	#[display("Cannot write TOML to path '{}'\nCause: {}", _0.path, _0.cause)]
83	TomlCantWrite(PathAndCause),
84}
85
86// region:    --- Cause Types
87
88#[derive(Debug, Display)]
89pub enum Cause {
90	Io(Box<io::Error>),
91
92	#[cfg(feature = "with-json")]
93	SerdeJson(Box<serde_json::Error>),
94
95	#[cfg(feature = "with-toml")]
96	TomlDe(Box<toml::de::Error>),
97
98	#[cfg(feature = "with-toml")]
99	TomlSer(Box<toml::ser::Error>),
100}
101
102#[derive(Debug)]
103pub struct PathAndCause {
104	path: String,
105	cause: Cause,
106}
107
108// endregion: --- Cause Types
109
110// region:    --- IO
111
112impl From<(&Path, io::Error)> for PathAndCause {
113	fn from(val: (&Path, io::Error)) -> Self {
114		PathAndCause {
115			path: val.0.to_string_lossy().to_string(),
116			cause: Cause::Io(Box::new(val.1)),
117		}
118	}
119}
120
121// endregion: --- IO
122
123// region:    --- JSON
124
125#[cfg(feature = "with-json")]
126impl From<(&Path, serde_json::Error)> for PathAndCause {
127	fn from(val: (&Path, serde_json::Error)) -> Self {
128		PathAndCause {
129			path: val.0.to_string_lossy().to_string(),
130			cause: Cause::SerdeJson(Box::new(val.1)),
131		}
132	}
133}
134
135// endregion: --- JSON
136
137// region:    --- TOML
138
139#[cfg(feature = "with-toml")]
140impl From<(&Path, toml::de::Error)> for PathAndCause {
141	fn from(val: (&Path, toml::de::Error)) -> Self {
142		PathAndCause {
143			path: val.0.to_string_lossy().to_string(),
144			cause: Cause::TomlDe(Box::new(val.1)),
145		}
146	}
147}
148
149#[cfg(feature = "with-toml")]
150impl From<(&Path, toml::ser::Error)> for PathAndCause {
151	fn from(val: (&Path, toml::ser::Error)) -> Self {
152		PathAndCause {
153			path: val.0.to_string_lossy().to_string(),
154			cause: Cause::TomlSer(Box::new(val.1)),
155		}
156	}
157}
158
159// endregion: --- TOML
160
161// region:    --- Error Boilerplate
162
163impl std::error::Error for Error {}
164
165// endregion: --- Error Boilerplate