nautilus_common/msgbus/
handler.rs

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// -------------------------------------------------------------------------------------------------
15
16//! 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.
20
21use std::{any::Any, marker::PhantomData, rc::Rc};
22
23use ustr::Ustr;
24use uuid::Uuid;
25
26pub trait MessageHandler: Any {
27    /// Returns the unique identifier for this handler.
28    fn id(&self) -> Ustr;
29    /// Handles a message of any type.
30    fn handle(&self, message: &dyn Any);
31    /// Returns this handler as a trait object.
32    fn as_any(&self) -> &dyn Any;
33}
34
35pub struct TypedMessageHandler<T: 'static + ?Sized, F: Fn(&T) + 'static> {
36    id: Ustr,
37    callback: F,
38    _phantom: PhantomData<T>,
39}
40
41impl<T: 'static, F: Fn(&T) + 'static> TypedMessageHandler<T, F> {
42    /// Creates a new handler with an optional custom ID.
43    pub fn new<S: AsRef<str>>(id: Option<S>, callback: F) -> Self {
44        let id_ustr = id
45            .map(|s| Ustr::from(s.as_ref()))
46            .unwrap_or_else(generate_unique_handler_id);
47        Self {
48            id: id_ustr,
49            callback,
50            _phantom: PhantomData,
51        }
52    }
53
54    /// Creates a new handler with an auto-generated ID.
55    pub fn from(callback: F) -> Self {
56        Self::new::<Ustr>(None, callback)
57    }
58}
59
60impl<T: 'static, F: Fn(&T) + 'static> MessageHandler for TypedMessageHandler<T, F> {
61    fn id(&self) -> Ustr {
62        self.id
63    }
64
65    fn handle(&self, message: &dyn Any) {
66        if let Some(typed_msg) = message.downcast_ref::<T>() {
67            (self.callback)(typed_msg);
68        }
69    }
70
71    fn as_any(&self) -> &dyn Any {
72        self
73    }
74}
75
76impl<F: Fn(&dyn Any) + 'static> TypedMessageHandler<dyn Any, F> {
77    /// Creates a new handler for dynamic Any messages with an optional custom ID.
78    pub fn new_any<S: AsRef<str>>(id: Option<S>, callback: F) -> Self {
79        let id_ustr = id
80            .map(|s| Ustr::from(s.as_ref()))
81            .unwrap_or_else(generate_unique_handler_id);
82
83        Self {
84            id: id_ustr,
85            callback,
86            _phantom: PhantomData,
87        }
88    }
89
90    /// Creates a handler for Any messages with an optional ID.
91    pub fn from_any<S: AsRef<str>>(id_opt: Option<S>, callback: F) -> Self {
92        Self::new_any(id_opt, callback)
93    }
94
95    /// Creates a handler for Any messages with an auto-generated ID.
96    pub fn with_any(callback: F) -> Self {
97        Self::new_any::<&str>(None, callback)
98    }
99}
100
101impl<F: Fn(&dyn Any) + 'static> MessageHandler for TypedMessageHandler<dyn Any, F> {
102    fn id(&self) -> Ustr {
103        self.id
104    }
105
106    fn handle(&self, message: &dyn Any) {
107        (self.callback)(message);
108    }
109
110    fn as_any(&self) -> &dyn Any {
111        self
112    }
113}
114
115fn generate_unique_handler_id() -> Ustr {
116    Ustr::from(&Uuid::new_v4().to_string())
117}
118
119#[derive(Clone)]
120#[repr(transparent)]
121pub struct ShareableMessageHandler(pub Rc<dyn MessageHandler>);
122
123impl From<Rc<dyn MessageHandler>> for ShareableMessageHandler {
124    fn from(value: Rc<dyn MessageHandler>) -> Self {
125        Self(value)
126    }
127}
128
129// SAFETY: Message handlers cannot be sent across thread boundaries
130unsafe impl Send for ShareableMessageHandler {}