simple_fs/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use derive_more::From;
use std::io;
use std::path::Path;
use std::time::SystemTimeError;

pub type Result<T> = core::result::Result<T, Error>;

#[derive(Debug)]
pub enum Error {
	// -- Path
	PathNotUtf8(String),
	PathHasNoFileName(String),

	// -- File
	FileNotFound(String),
	FileCantOpen(PathAndCause),
	FileCantRead(PathAndCause),
	FileCantWrite(PathAndCause),
	FileCantCreate(PathAndCause),
	FileHasNoParent(String),

	// -- Metadata
	CantGetMetadata(PathAndCause),
	CantGetMetadataModified(PathAndCause),

	// -- Time
	CantGetDurationSystemTimeError(SystemTimeError),

	// -- Directory
	DirCantCreateAll(PathAndCause),

	// -- Path Validations
	PathNotValidForPath(PathAndCause),

	// -- Glob
	GlobCantNew {
		glob: String,
		cause: globset::Error,
	},
	GlobSetCantBuild {
		globs: Vec<String>,
		cause: globset::Error,
	},

	// -- Watch
	FailToWatch {
		path: String,
		cause: String,
	},
	CantWatchPathNotFound(String),

	// -- with-json
	#[cfg(feature = "with-json")]
	JsonCantRead(PathAndCause),
	#[cfg(feature = "with-json")]
	JsonCantWrite(PathAndCause),

	// -- with-toml
	#[cfg(feature = "with-toml")]
	TomlCantRead(PathAndCause),
	#[cfg(feature = "with-toml")]
	TomlCantWrite(PathAndCause),
}

// region:    --- Cause Types

#[derive(Debug)]
pub enum Cause {
	#[allow(unused)]
	Io(Box<io::Error>),

	#[cfg(feature = "with-json")]
	SerdeJson(Box<serde_json::Error>),
	#[cfg(feature = "with-toml")]
	TomlDe(Box<toml::de::Error>),
	#[cfg(feature = "with-toml")]
	TomlSer(Box<toml::ser::Error>),
}

#[derive(Debug)]
#[allow(unused)]
pub struct PathAndCause {
	path: String,
	cause: Cause,
}

// endregion: --- Cause Types

// region:    --- IO

impl From<(&Path, io::Error)> for PathAndCause {
	fn from(val: (&Path, io::Error)) -> Self {
		PathAndCause {
			path: val.0.to_string_lossy().to_string(),
			cause: Cause::Io(Box::new(val.1)),
		}
	}
}

// endregion: --- IO

// region:    --- JSON

#[cfg(feature = "with-json")]
impl From<(&Path, serde_json::Error)> for PathAndCause {
	fn from(val: (&Path, serde_json::Error)) -> Self {
		PathAndCause {
			path: val.0.to_string_lossy().to_string(),
			cause: Cause::SerdeJson(Box::new(val.1)),
		}
	}
}

// endregion: --- JSON

// region:    --- TOML

#[cfg(feature = "with-toml")]
impl From<(&Path, toml::de::Error)> for PathAndCause {
	fn from(val: (&Path, toml::de::Error)) -> Self {
		PathAndCause {
			path: val.0.to_string_lossy().to_string(),
			cause: Cause::TomlDe(Box::new(val.1)),
		}
	}
}

#[cfg(feature = "with-toml")]
impl From<(&Path, toml::ser::Error)> for PathAndCause {
	fn from(val: (&Path, toml::ser::Error)) -> Self {
		PathAndCause {
			path: val.0.to_string_lossy().to_string(),
			cause: Cause::TomlSer(Box::new(val.1)),
		}
	}
}

// endregion: --- TOML

// region:    --- Error Boilerplate

impl core::fmt::Display for Error {
	fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::result::Result<(), core::fmt::Error> {
		write!(fmt, "{self:?}")
	}
}

impl std::error::Error for Error {}

// endregion: --- Error Boilerplate