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
// Import necessary items from the callbacks module
use crate::callbacks::types::event::{EventState, EventType};
// Import necessary items from the pyo3 crate
use pyo3::prelude::*;
use tokio::sync::mpsc;

// Define the base interface for event callbacks
pub trait EventCallbackInterface {
	fn handle_event(&mut self, event_type: EventType, event_data: EventState);
}

// Define a basic event handler struct
#[derive(Clone, Debug)]
#[pyclass]
pub struct EventHandler {
	event_sender: Option<mpsc::Sender<(EventType, EventState)>>,
}

impl EventHandler {
	// Constructor for EventHandler
	pub fn new(event_sender: Option<mpsc::Sender<(EventType, EventState)>>) -> Self {
		EventHandler { event_sender }
	}
}

// Define a Python-compatible event callback interface
#[derive(Clone, Debug)]
#[pyclass]
pub struct PyEventCallbackInterface {
	// Internal event handler instance
	event_handler: EventHandler,
}

// Implement Python methods for PyEventCallbackInterface
#[pymethods]
impl PyEventCallbackInterface {
	// Python constructor for PyEventCallbackInterface
	#[new]
	pub fn new(event_handler: EventHandler) -> Self {
		PyEventCallbackInterface { event_handler }
	}

	// Python method to handle events
	fn handle_event(&mut self, event_type: EventType, event_data: EventState) {
		// Delegate the event handling to the internal event handler
		self.event_handler.handle_event(event_type, event_data);
	}
}

// Implement the EventCallbackInterface for the EventHandler
impl EventCallbackInterface for EventHandler {
	// Implementation of the handle_event method for EventHandler
	fn handle_event(&mut self, event_type: EventType, event_data: EventState) {
		// If the event sender is not None, send the event
		if let Some(event_sender) = &self.event_sender {
			// Send the event
			event_sender.try_send((event_type, event_data)).unwrap_or_else(|e| {
				println!("Error sending event: {:?}", e);
			});
		} else {
			println!("Event sender is None");
			println!("Event type: {:?}", event_type);
			println!("Event data: {:?}", event_data);
		}
	}
}