1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3// https://nautechsystems.io
4//
5// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6// You may not use this file except in compliance with the License.
7// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// -------------------------------------------------------------------------------------------------
1516//! Message handler functionality for the message bus system.
17//!
18//! This module provides a trait and implementations for handling messages
19//! in a type-safe manner, enabling both typed and untyped message processing.
2021use std::{any::Any, marker::PhantomData, rc::Rc};
2223use ustr::Ustr;
24use uuid::Uuid;
2526pub trait MessageHandler: Any {
27/// Returns the unique identifier for this handler.
28fn id(&self) -> Ustr;
29/// Handles a message of any type.
30fn handle(&self, message: &dyn Any);
31/// Returns this handler as a trait object.
32fn as_any(&self) -> &dyn Any;
33}
3435pub struct TypedMessageHandler<T: 'static + ?Sized, F: Fn(&T) + 'static> {
36 id: Ustr,
37 callback: F,
38 _phantom: PhantomData<T>,
39}
4041impl<T: 'static, F: Fn(&T) + 'static> TypedMessageHandler<T, F> {
42/// Creates a new handler with an optional custom ID.
43pub fn new<S: AsRef<str>>(id: Option<S>, callback: F) -> Self {
44let id_ustr = id
45 .map(|s| Ustr::from(s.as_ref()))
46 .unwrap_or_else(generate_unique_handler_id);
47Self {
48 id: id_ustr,
49 callback,
50 _phantom: PhantomData,
51 }
52 }
5354/// Creates a new handler with an auto-generated ID.
55pub fn from(callback: F) -> Self {
56Self::new::<Ustr>(None, callback)
57 }
58}
5960impl<T: 'static, F: Fn(&T) + 'static> MessageHandler for TypedMessageHandler<T, F> {
61fn id(&self) -> Ustr {
62self.id
63 }
6465fn handle(&self, message: &dyn Any) {
66if let Some(typed_msg) = message.downcast_ref::<T>() {
67 (self.callback)(typed_msg);
68 }
69 }
7071fn as_any(&self) -> &dyn Any {
72self
73}
74}
7576impl<F: Fn(&dyn Any) + 'static> TypedMessageHandler<dyn Any, F> {
77/// Creates a new handler for dynamic Any messages with an optional custom ID.
78pub fn new_any<S: AsRef<str>>(id: Option<S>, callback: F) -> Self {
79let id_ustr = id
80 .map(|s| Ustr::from(s.as_ref()))
81 .unwrap_or_else(generate_unique_handler_id);
8283Self {
84 id: id_ustr,
85 callback,
86 _phantom: PhantomData,
87 }
88 }
8990/// Creates a handler for Any messages with an optional ID.
91pub fn from_any<S: AsRef<str>>(id_opt: Option<S>, callback: F) -> Self {
92Self::new_any(id_opt, callback)
93 }
9495/// Creates a handler for Any messages with an auto-generated ID.
96pub fn with_any(callback: F) -> Self {
97Self::new_any::<&str>(None, callback)
98 }
99}
100101impl<F: Fn(&dyn Any) + 'static> MessageHandler for TypedMessageHandler<dyn Any, F> {
102fn id(&self) -> Ustr {
103self.id
104 }
105106fn handle(&self, message: &dyn Any) {
107 (self.callback)(message);
108 }
109110fn as_any(&self) -> &dyn Any {
111self
112}
113}
114115fn generate_unique_handler_id() -> Ustr {
116 Ustr::from(&Uuid::new_v4().to_string())
117}
118119#[derive(Clone)]
120#[repr(transparent)]
121pub struct ShareableMessageHandler(pub Rc<dyn MessageHandler>);
122123impl From<Rc<dyn MessageHandler>> for ShareableMessageHandler {
124fn from(value: Rc<dyn MessageHandler>) -> Self {
125Self(value)
126 }
127}
128129// SAFETY: Message handlers cannot be sent across thread boundaries
130unsafe impl Send for ShareableMessageHandler {}