temp_mongo/
error.rs

1use std::path::PathBuf;
2
3/// An error that can occur when creating or cleaning a MongDB instance.
4pub struct Error {
5	/// The actual error.
6	inner: ErrorInner,
7}
8
9#[derive(Debug)]
10pub enum ErrorInner {
11	/// Failed to create the temporary directory.
12	MakeTempDir(std::io::Error),
13
14	/// Failed to create the database directory.
15	MakeDbDir(PathBuf, std::io::Error),
16
17	/// Failed to spawn the server.
18	SpawnServer(String, std::io::Error),
19
20	/// Failed to kill the server.
21	KillServer(std::io::Error),
22
23	/// Failed to clean up the temporary directory.
24	CleanDir(PathBuf, std::io::Error),
25
26	/// Failed to connect to the server.
27	Connect(String, mongodb::error::Error),
28}
29
30impl std::error::Error for Error {}
31
32impl std::fmt::Debug for Error {
33	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34		std::fmt::Debug::fmt(&self.inner, f)
35	}
36}
37
38impl std::fmt::Display for Error {
39	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40		std::fmt::Display::fmt(&self.inner, f)
41	}
42}
43
44impl std::fmt::Display for ErrorInner {
45	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46		match self {
47			Self::MakeTempDir(e) => write!(f, "Failed to create temporary directory: {e}"),
48			Self::MakeDbDir(path, e) => write!(f, "Failed to create data directory {}: {e}", path.display()),
49			Self::SpawnServer(name, e) => write!(f, "Failed to run server command: {name}: {e}"),
50			Self::KillServer(e) => write!(f, "Failed to terminate spanwed server: {e}"),
51			Self::CleanDir(path, e) => write!(f, "Failed to clean up temporary state directory {}: {e}", path.display()),
52			Self::Connect(address, e) => write!(f, "Failed to connect to server at {address}: {e}"),
53		}
54	}
55}
56
57impl From<ErrorInner> for Error {
58	fn from(inner: ErrorInner) -> Self {
59		Self { inner }
60	}
61}