Skip to main content

rs_netty/
traits.rs

1use crate::{
2    context::{BusinessContext, Context, DatagramContext, InboundContext, OutboundContext},
3    Result,
4};
5
6/// Result of an inbound, business, or outbound stage.
7///
8/// `Next` forwards the transformed message to the next stage. `Stop` consumes
9/// the message and stops processing for that direction.
10pub enum Flow<T> {
11    /// Continue the pipeline with this message.
12    Next(T),
13    /// Stop processing this message without treating it as an error.
14    Stop,
15}
16
17impl<T> Flow<T> {
18    /// Maps the contained `Next` value while preserving `Stop`.
19    pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Flow<U> {
20        match self {
21            Flow::Next(value) => Flow::Next(f(value)),
22            Flow::Stop => Flow::Stop,
23        }
24    }
25}
26
27#[trait_variant::make(Inbound: Send)]
28/// Inbound transformation stage for decoded messages.
29///
30/// Implement this trait when a stage should validate, filter, or transform a
31/// message before it reaches the business stages or final handler.
32pub trait LocalInbound<I>: 'static {
33    /// Message type forwarded to the next stage.
34    type Out: Send + 'static;
35
36    /// Processes one inbound message.
37    async fn read(&mut self, ctx: &mut InboundContext, msg: I) -> Result<Flow<Self::Out>>;
38}
39
40#[trait_variant::make(Business: Send)]
41/// Middle pipeline stage for application-level transformations.
42pub trait LocalBusiness<I>: 'static {
43    /// Message type forwarded to the next stage.
44    type Out: Send + 'static;
45
46    /// Handles one business message.
47    async fn handle(&mut self, ctx: &mut BusinessContext, msg: I) -> Result<Flow<Self::Out>>;
48}
49
50#[trait_variant::make(Handler: Send)]
51/// Final TCP handler for inbound messages.
52///
53/// A handler receives the fully transformed inbound message and writes values
54/// of type [`Self::Write`] back through the outbound side of the pipeline.
55pub trait LocalHandler<I>: 'static {
56    /// Application message type accepted by `Context::write`.
57    type Write: Send + 'static;
58
59    /// Handles one inbound TCP message.
60    async fn read(&mut self, ctx: &mut Context<Self::Write>, msg: I) -> Result<()>;
61}
62
63#[trait_variant::make(DatagramHandler: Send)]
64/// Final UDP handler for inbound datagrams.
65pub trait LocalDatagramHandler<I>: 'static {
66    /// Application message type accepted by `DatagramContext::write`.
67    type Write: Send + 'static;
68
69    /// Handles one inbound UDP datagram.
70    async fn read(&mut self, ctx: &mut DatagramContext<Self::Write>, msg: I) -> Result<()>;
71}
72
73#[trait_variant::make(Outbound: Send)]
74/// Outbound transformation stage for application writes.
75pub trait LocalOutbound<I>: 'static {
76    /// Message type forwarded to the next outbound stage or final encoder.
77    type Out: Send + 'static;
78
79    /// Processes one outbound message.
80    async fn write(&mut self, ctx: &mut OutboundContext, msg: I) -> Result<Flow<Self::Out>>;
81}