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
/// A type alias for `std::result::Result` that uses `Box<dyn std::error::Error>` as the error type.
pub type Result<T> = std::result::Result<T, Error>;

/// Represents an error in the application.
#[derive(Debug, Clone)]
pub struct Error {
	msg: String,
}

impl Error {
	/// Creates a new `Error` instance with the given message.
	pub fn new(msg: &str) -> Self {
		Self { msg: msg.to_owned() }
	}
}

unsafe impl Send for Error {}

impl std::fmt::Display for Error {
	/// Formats the error message for display.
	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
		write!(f, "{}", self.msg)
	}
}

impl<T: std::error::Error> From<T> for Error {
	fn from(error: T) -> Self {
		Self { msg: error.to_string() }
	}
}

#[macro_export]
macro_rules! create_error {
	($($arg:tt)*) => {
		Err($crate::shared::Error::new(&format!($($arg)*)))
	};
}

#[cfg(test)]
mod tests {
	use super::Error;

	#[test]
	fn test() {
		let err = Error::new("hi");
		let err = err.clone();
		format!("{}", err);
		format!("{:?}", err);
	}
}