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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use crate::prelude::{Command, Message, TCommandService};
use crate::responses::{self, ApplicationError, ApplicationResponse, BaseError};
use async_trait::async_trait;
use hashbrown::HashMap;
use std::collections::VecDeque;
use std::ops::{Deref, DerefMut};
use std::{
	any::{Any, TypeId},
	pin::Pin,
	sync::Arc,
};
use tokio::sync::RwLock;
pub type Future<T, E> = Pin<Box<dyn futures::Future<Output = Result<T, E>> + Send>>;
pub type AtomicContextManager = Arc<RwLock<ContextManager>>;
pub type TCommandHandler<R, E> = HashMap<TypeId, Box<dyn Fn(Box<dyn Any + Send + Sync>, AtomicContextManager) -> Future<R, E> + Send + Sync>>;
pub type TEventHandler<R, E> = HashMap<String, Vec<Box<dyn Fn(Box<dyn Message>, AtomicContextManager) -> Future<R, E> + Send + Sync>>>;

/// Task Local Context Manager
/// This is called for every time Messagebus.handle is invoked within which it manages events raised in service.
/// It spawns out Executor that manages transaction.
pub struct ContextManager {
	pub event_queue: VecDeque<Box<dyn Message>>,
}

impl ContextManager {
	/// Creation of context manager returns context manager AND event receiver
	pub fn new() -> AtomicContextManager {
		Arc::new(RwLock::new(Self { event_queue: VecDeque::new() }))
	}
}

impl Deref for ContextManager {
	type Target = VecDeque<Box<dyn Message>>;
	fn deref(&self) -> &Self::Target {
		&self.event_queue
	}
}
impl DerefMut for ContextManager {
	fn deref_mut(&mut self) -> &mut Self::Target {
		&mut self.event_queue
	}
}

#[async_trait]
pub trait TMessageBus<R, E, C>
where
	responses::BaseError: std::convert::From<E>,
	R: ApplicationResponse,
	E: ApplicationError + std::convert::From<crate::responses::BaseError>,
	C: Command,
{
	fn command_handler(&self, context_manager: AtomicContextManager) -> Box<dyn TCommandService<R, E, C>>;
	fn event_handler(&self) -> &'static TEventHandler<R, E>;

	async fn handle(&self, message: C) -> Result<R, E>
	where
		C: Command,
	{
		let context_manager = ContextManager::new();

		let res = self.command_handler(context_manager.clone()).execute(message).await?;

		// Trigger event
		if !context_manager.read().await.event_queue.is_empty() {
			let event = context_manager.write().await.event_queue.pop_front();
			let _ = self._handle_event(event.unwrap(), context_manager.clone()).await;
		}

		Ok(res)
	}
	async fn handle_event(&self, msg: Box<dyn Message>) -> Result<(), E> {
		let context_manager = ContextManager::new();
		self._handle_event(msg, context_manager.clone()).await
	}

	async fn _handle_event(&self, msg: Box<dyn Message>, context_manager: AtomicContextManager) -> Result<(), E> {
		// ! msg.topic() returns the name of event. It is crucial that it corresponds to the key registered on Event Handler.

		println!("Handle Event : {:?}", msg);

		let handlers = self.event_handler().get(&msg.metadata().topic).ok_or_else(|| {
			eprintln!("Unprocessable Event Given! {:?}", msg);
			BaseError::NotFound
		})?;

		for handler in handlers.iter() {
			match handler(msg.message_clone(), context_manager.clone()).await {
				Ok(_val) => {
					eprintln!("Event Handling Succeeded!");
				}

				// ! Safety:: BaseError Must Be Enforced To Be Accepted As Variant On ServiceError
				Err(err) => match err.into() {
					BaseError::StopSentinel => {
						eprintln!("Stop Sentinel Arrived!");

						break;
					}
					BaseError::StopSentinelWithEvent(event) => {
						eprintln!("Stop Sentinel With Event Arrived!");
						context_manager.write().await.push_back(event);
						break;
					}
					err => {
						eprintln!("Error Occurred While Handling Event! Error:{:?}", err);
					}
				},
			};
		}

		// Resursive case

		let incoming_event = context_manager.write().await.event_queue.pop_front();
		if let Some(event) = incoming_event {
			if let Err(err) = self._handle_event(event, context_manager.clone()).await {
				// ! Safety:: BaseError Must Be Enforced To Be Accepted As Variant On ServiceError
				eprintln!("{:?}", err);
			}
		}

		Ok(())
	}
}

/// init_event_handler creating macro
/// Not that crate must have `Dependency` struct with its own implementation
#[macro_export]
macro_rules! init_event_handler {
    (
		R: $response:ty,
		E: $error:ty $(,)?
        {
			$(
				$event:ty: [$($handler:expr $(=>($($injectable:ident $(( $($arg:ident),* ))? ),*))?),* $(,)? ]
			),*
			$(,)?
		}
    ) =>{
		pub fn event_handler() -> &'static ::ruva::prelude::TEventHandler<$response, $error>  {
			extern crate self as current_crate;
			static EVENT_HANDLER: ::std::sync::OnceLock<::ruva::prelude::TEventHandler<$response, $error>> = std::sync::OnceLock::new();
			EVENT_HANDLER.get_or_init(||{
			use current_crate::dependencies;
            let mut _map : ::ruva::prelude::TEventHandler<$response, $error> = ::ruva::prelude::HandlerMapper::new();
            $(
                _map.insert(
                    stringify!($event).into(),
                    vec![
                        $(
                            Box::new(
                                |e:Box<dyn Message>, context_manager: ::ruva::prelude::AtomicContextManager| -> std::pin::Pin<Box<dyn futures::Future<Output = Result<$response, $error>> + Send>>{


									#[allow(unused)]
									macro_rules! matcher{
										($a:ident)=>{
											context_manager.clone()
										}
									}

                                    Box::pin($handler(
                                        // * Convert event so event handler accepts not Box<dyn Message> but `event_happend` type of message.
                                        // Safety:: client should access this vector of handlers by providing the corresponding event name
                                        // So, when it is followed, it logically doesn't make sense to cause an error.
                                        *e.downcast::<$event>().expect("Not Convertible!"),
										$(
											// * Injectable functions are added here.
											$(dependencies::$injectable( $( $(matcher!($arg)),*)?),)*
										)?
                                    ))
                                }
                                ),
                        )*
                    ]
                );
            )*
            _map
        })
    }
};
	(
		E: $error:ty,
		R: $response:ty $(,)?
		{
			$(
				$event:ty: [$($handler:expr $(=>($($injectable:ident $(( $($arg:ident),* ))? ),*))?),* $(,)? ]
			),*
			$(,)?
		}
	) =>{
		init_event_handler!(
			R:$response,E:$error,
			{
				$(
					$event: [$($handler $(=>($($injectable $(( $($arg),* ))? ),*))?),* ]
				),*
			}
		)
	}
}

/// init_command_handler creating macro
/// Note that crate must have `crate::dependencies` must exist
#[macro_export]
macro_rules! init_command {
    (
		R: $response:ty,
		E: $error:ty $(,)?
        {
			$(
				$command:ty:$handler:expr $(=>($($injectable:ident $(( $($arg:ident),* ))? ),*))?
			),*
			$(,)?
		}
    )
        => {

		pub fn command_handler() -> &'static ruva::prelude::TCommandHandler<$response, $error> {
			extern crate self as current_crate;
			static COMMAND_HANDLER: ::std::sync::OnceLock<ruva::prelude::TCommandHandler<$response, $error>> = std::sync::OnceLock::new();

			COMMAND_HANDLER.get_or_init(||{
				use current_crate::dependencies;
				let mut _map: ruva::prelude::TCommandHandler<$response,$error>= ::ruva::prelude::TCommandHandler::new();

				$(
					_map.insert(
						// ! Only one command per one handler is acceptable, so the later insertion override preceding one.
						std::any::TypeId::of::<$command>(),

							Box::new( |c:Box<dyn std::any::Any+Send+Sync>, context_manager: ::ruva::prelude::AtomicContextManager|->std::pin::Pin<Box<dyn futures::Future<Output = Result<$response, $error>> + Send>> {
								// * Convert event so event handler accepts not Box<dyn Message> but `event_happend` type of message.
								// ! Logically, as it's from TypId of command, it doesn't make to cause an error.
								#[allow(unused)]
								macro_rules! matcher{
									($a:ident)=>{
										context_manager.clone()
									}
								}
								Box::pin($handler(
									*c.downcast::<$command>().unwrap(),
								$(
									// * Injectable functions are added here.
									$(dependencies::$injectable( $( $(matcher!($arg)),*)?),)*
								)?
							))
							}
						),
					);
				)*
				_map
			})
			}
   	 	};
	(
		E: $error:ty,
		R: $response:ty $(,)?
        {
			$(
				$command:ty:$handler:expr $(=>($($injectable:ident $(( $($arg:ident),* ))? ),*))?
			),*
			$(,)?
		}
	) =>{
		init_command!(
			R:$response,E:$error
			{
				$(
					$command:$handler $(=>($($injectable $(( $($arg),* ))? ),*))?
				),*

			}

	 )
	}
}