tcp_console/
subscription.rs

1use async_trait::async_trait;
2use bytes::Bytes;
3
4#[async_trait]
5/// Trait describing how incoming messages on [Console] must be handled.
6pub trait Subscription {
7    /// Handles strongly-typed messages.
8    ///
9    /// Return optional [Bytes] that will be sent back to the message sender.
10    async fn handle(&self, message: Bytes) -> Result<Option<Bytes>, SubscriptionError>;
11
12    /// Handles free-form text messages.
13    ///
14    /// Returns an optional [String], which, if provided, will be sent back to the message sender.
15    async fn weak_handle(&self, message: &str) -> Result<Option<String>, SubscriptionError>;
16}
17
18/// Convenience type to abstract away concrete implementations of [Subscription] errors.
19pub type SubscriptionError = Box<dyn std::error::Error + Send + Sync>;
20
21/// Convenience type to abstract away concrete implementations of [Subscription].
22pub(crate) type BoxedSubscription = Box<dyn Subscription + Send + Sync>;