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
use std::path::PathBuf;

/// An error that can occur when creating or cleaning a MongDB instance.
pub struct Error {
	/// The actual error.
	inner: ErrorInner,
}

#[derive(Debug)]
pub enum ErrorInner {
	/// Failed to create the temporary directory.
	MakeTempDir(std::io::Error),

	/// Failed to create the database directory.
	MakeDbDir(PathBuf, std::io::Error),

	/// Failed to spawn the server.
	SpawnServer(String, std::io::Error),

	/// Failed to kill the server.
	KillServer(std::io::Error),

	/// Failed to clean up the temporary directory.
	CleanDir(PathBuf, std::io::Error),

	/// Failed to connect to the server.
	Connect(String, mongodb::error::Error),
}

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

impl std::fmt::Debug for Error {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		std::fmt::Debug::fmt(&self.inner, f)
	}
}

impl std::fmt::Display for Error {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		std::fmt::Display::fmt(&self.inner, f)
	}
}

impl std::fmt::Display for ErrorInner {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			Self::MakeTempDir(e) => write!(f, "Failed to create temporary directory: {e}"),
			Self::MakeDbDir(path, e) => write!(f, "Failed to create data directory {}: {e}", path.display()),
			Self::SpawnServer(name, e) => write!(f, "Failed to run server command: {name}: {e}"),
			Self::KillServer(e) => write!(f, "Failed to terminate spanwed server: {e}"),
			Self::CleanDir(path, e) => write!(f, "Failed to clean up temporary state directory {}: {e}", path.display()),
			Self::Connect(address, e) => write!(f, "Failed to connect to server at {address}: {e}"),
		}
	}
}

impl From<ErrorInner> for Error {
	fn from(inner: ErrorInner) -> Self {
		Self { inner }
	}
}