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
/// Defines a custom error enum with standardized internal error handling.
///
/// This macro simplifies the creation of internal module-specific error types by:
///
/// - Declaring an enum with the given variants and associated data.
/// - Providing a `description()` method that formats each variant into a human-readable message.
/// - Implementing `Debug` by printing the formatted description.
/// - Automatically converting the enum into a `mikros::errors::Error::Internal`
/// using the formatted description.
///
/// All generated errors are treated as internal errors (`Error::Internal`) when
/// converted, making this macro ideal for use inside internal features where
/// external error visibility is restricted.
///
/// # Syntax
///
/// ```rust,ignore
/// module_errors!(
/// ErrorName {
/// Variant1(arg1: Type1, arg2: Type2) => "Error occurred: {}, {}",
/// Variant2 => "A simple error message",
/// Variant3(msg: String) => "Something failed: {}"
/// }
/// );
/// ```
///
/// # Example
///
/// ```rust,ignore
/// module_errors!(
/// MyError {
/// Internal(msg: String) => "Internal error: {}",
/// NotFound => "Resource not found"
/// }
/// );
///
/// fn do_something() -> Result<(), mikros::errors::Error> {
/// Err(MyError::Internal("disk full".into()).into())
/// }
/// ```
///
/// This expands to:
/// ```rust,ignore
/// pub enum MyError {
/// Internal(String),
/// NotFound,
/// }
///
/// impl MyError {
/// pub fn description(&self) -> String {
/// match self {
/// MyError::Internal(msg) => format!("Internal error: {}", msg),
/// MyError::NotFound => "Resource not found".to_string(),
/// }
/// }
/// }
///
/// impl Debug for MyError {
/// fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
/// write!(f, "{}", self.description())
/// }
/// }
///
/// impl From<MyError> for mikros::errors::Error {
/// fn from(e: MyError) -> mikros::errors::Error {
/// mikros::errors::Error::Internal(e.description())
/// }
/// }
/// ```
) => ;
}