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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use std::future::Future;
use std::pin::Pin;

use crate::{TracebackError, TRACEBACK_ERROR_CALLBACK};

pub trait TracebackCallback {
    fn call(&self, error: TracebackError);
}

// Define a trait that represents a function returning a Future
pub trait TracebackCallbackAsync {
    fn call(&self, error: TracebackError) -> Pin<Box<dyn Future<Output = ()> + Send + Sync>>;
}

pub enum TracebackCallbackType {
    Async(Box<dyn TracebackCallbackAsync + Send + Sync>),
    Sync(Box<dyn TracebackCallback + Send + Sync>),
}

pub fn set_traceback_callback(callback: TracebackCallbackType) {
    unsafe {
        TRACEBACK_ERROR_CALLBACK = Some(callback);
    }
}

pub fn reset_traceback_callback() {
    unsafe {
        TRACEBACK_ERROR_CALLBACK = None;
    }
}

/// Sets a custom traceback callback for error handling in a Rust program.
///
/// This macro allows you to define and set a custom traceback callback function,
/// which will be called when a TracebackError goes out of scope.
/// The traceback callback provides a way to customize how error information is
/// handled and reported.
///
/// # Usage
///
/// To use this macro, provide the name of the callback function you want to use
/// as the custom traceback callback. This function should take an argument of
/// type `utils::error_types::TracebackError`. The macro generates a unique
/// struct and function to wrap your callback and sets it as the traceback
/// callback using `utils::set_traceback_callback`.
///
/// # Example
///
/// ```rust
/// // Define a custom traceback callback function
/// fn my_traceback_callback(error: utils::error_types::TracebackError) {
///     // Custom error handling logic here
///     println!("Custom traceback callback called: {:?}", error);
/// }
///
/// // Use the set_traceback macro to set the custom traceback callback
/// set_traceback!(my_traceback_callback);
///
/// // Any TracebackErrors will now be handled by my_traceback_callback when dropped
/// ```
///
/// ```rust
/// // The same is possible with asynchronous functions
/// async fn my_traceback_callback(error: utils::error_types::TracebackError) {
///     // Custom error handling logic here
///     println!("Async custom traceback callback called: {:?}", error);
/// }
///
/// // But you have to specify that it is asynchronous
/// set_traceback!(async my_traceback_callback);
///
/// // Any TracebackErrors will now be handled by my_traceback_callback when dropped
/// ```
#[macro_export]
macro_rules! set_traceback {
    ($callback:ident) => {
        $crate::paste::unique_paste! {
            // Generate a unique identifier for the struct
            #[allow(non_camel_case_types)]
            mod [<_private_ $callback _ TempStruct>] {
                pub struct [<$callback _ TempStruct>];

                impl $crate::TracebackCallback for [<$callback _ TempStruct>] {
                    fn call(&self, error: $crate::error_types::TracebackError) {
                        super::$callback(error)
                    }
                }
            }

            // Expose the generated struct through a function
            pub fn [<$callback _ temp_struct>]() -> [<_private_ $callback _ TempStruct>]::[<$callback _ TempStruct>] {
                [<_private_ $callback _ TempStruct>]::[<$callback _ TempStruct>]
            }

            // Call the macro to set the traceback callback
            $crate::set_traceback_callback($crate::TracebackCallbackType::Sync(Box::new([<$callback _ temp_struct>]())));
        }
    };
    (async $callback:ident) => {
        $crate::paste::unique_paste! {
            // Generate a unique identifier for the struct
            #[allow(non_camel_case_types)]
            mod [<_private_ $callback _ TempStruct>] {
                pub struct [<$callback _ TempStruct>];

                impl $crate::TracebackCallbackAsync for [<$callback _ TempStruct>] {
                    fn call(
                        &self,
                        error: $crate::error_types::TracebackError,
                    ) -> std::pin::Pin<
                        Box<dyn std::future::Future<Output = ()> + std::marker::Send + std::marker::Sync>,
                    > {
                        Box::pin(super::$callback(error))
                    }
                }
            }

            // Expose the generated struct through a function
            pub fn [<$callback _ temp_struct>]() -> [<_private_ $callback _ TempStruct>]::[<$callback _ TempStruct>] {
                [<_private_ $callback _ TempStruct>]::[<$callback _ TempStruct>]
            }

            // Call the macro to set the traceback callback
            $crate::set_traceback_callback($crate::TracebackCallbackType::Async(Box::new([<$callback _ temp_struct>]())));
        }
    };
}