headless_lms_utils/error/macros.rs
1/*!
2Macro utilities for creating error types more ergonomically.
3
4This module provides helper macros that simplify error construction
5for types implementing `BackendError`.
6*/
7
8/// Internal helper macro that defines an error creation macro.
9///
10/// This avoids using the `paste` crate and keeps the implementation simple.
11/// Each error module should use this to define their own specialized macro.
12///
13/// # Examples
14///
15/// ```ignore
16/// // In models/src/error.rs:
17/// define_err_macro!(
18/// model_err,
19/// ModelError,
20/// ModelErrorType,
21/// "Create a ModelError."
22/// );
23/// ```
24#[macro_export]
25macro_rules! define_err_macro {
26 ($macro_name:ident, $error:ty, $error_type:ty, $doc:expr) => {
27 #[doc = $doc]
28 ///
29 /// # Examples
30 ///
31 /// ```ignore
32 /// // Without source
33 /// let err = macro_name!(Generic, "message".to_string());
34 ///
35 /// // With source
36 /// let err = macro_name!(Generic, "message".to_string(), source_err);
37 ///
38 /// // With format!
39 /// let err = macro_name!(Generic, format!("Failed: {}", detail));
40 /// ```
41 #[macro_export]
42 macro_rules! $macro_name {
43 // Without source: model_err!(Generic, "message")
44 ($variant:ident, $msg:expr) => {
45 <$error>::new(<$error_type>::$variant, $msg, None)
46 };
47 // With source: model_err!(Generic, "message", source_err)
48 ($variant:ident, $msg:expr, $src:expr) => {
49 <$error>::new(<$error_type>::$variant, $msg, Some($src.into()))
50 };
51 }
52 };
53}