pub struct TypeHandler { /* private fields */ }Expand description
A handler that dispatches messages to other handlers based on a metadata field (e.g., “type”).
§Example
use mq_bridge::type_handler::TypeHandler;
use mq_bridge::{CanonicalMessage, Handled};
use serde::Deserialize;
#[derive(Deserialize)]
struct MyCommand { id: String }
let handler = TypeHandler::new()
.add("my_command", |cmd: MyCommand| async move {
println!("Received command: {}", cmd.id);
Ok(Handled::Ack)
});Implementations§
Source§impl TypeHandler
impl TypeHandler
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new TypeHandler that looks for the specified key in message metadata to determine the message type.
Sourcepub fn add_handler(
self,
type_name: &str,
handler: impl Handler + 'static,
) -> Self
pub fn add_handler( self, type_name: &str, handler: impl Handler + 'static, ) -> Self
Registers a generic handler for a specific type name.
Sourcepub fn with_fallback(self, handler: Arc<dyn Handler>) -> Self
pub fn with_fallback(self, handler: Arc<dyn Handler>) -> Self
Sets a fallback handler to be used when no type match is found.
Sourcepub fn add<T, H, Args>(self, type_name: &str, handler: H) -> Selfwhere
T: DeserializeOwned + Send + Sync + 'static,
H: IntoTypedHandler<T, Args>,
Args: Send + Sync + 'static,
pub fn add<T, H, Args>(self, type_name: &str, handler: H) -> Selfwhere
T: DeserializeOwned + Send + Sync + 'static,
H: IntoTypedHandler<T, Args>,
Args: Send + Sync + 'static,
Registers a typed handler function.
The handler can accept either:
fn(T) -> Future<Output = Result<Handled, HandlerError>>fn(T, MessageContext) -> Future<Output = Result<Handled, HandlerError>>
Trait Implementations§
Source§impl Clone for TypeHandler
impl Clone for TypeHandler
Source§fn clone(&self) -> TypeHandler
fn clone(&self) -> TypeHandler
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Default for TypeHandler
impl Default for TypeHandler
Source§impl Handler for TypeHandler
impl Handler for TypeHandler
Auto Trait Implementations§
impl Freeze for TypeHandler
impl !RefUnwindSafe for TypeHandler
impl Send for TypeHandler
impl Sync for TypeHandler
impl Unpin for TypeHandler
impl !UnwindSafe for TypeHandler
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more