roblib_client/transports/
mod.rs1use anyhow::Result;
2use roblib::{cmd::Command, event::Event};
3
4#[cfg(feature = "http")]
5pub mod http;
6#[cfg(feature = "tcp")]
7pub mod tcp;
8#[cfg(feature = "udp")]
9pub mod udp;
10#[cfg(feature = "ws")]
11pub mod ws;
12
13const ID_START: u32 = 1;
14
15pub trait Transport {
16 fn cmd<C>(&self, cmd: C) -> Result<C::Return>
17 where
18 C: Command;
19}
20
21pub trait Subscribable: Transport {
22 fn subscribe<E, F>(&self, event: E, handler: F) -> Result<()>
23 where
24 E: Event,
25 F: (FnMut(E::Item) -> Result<()>) + Send + Sync + 'static;
26
27 fn unsubscribe<E>(&self, event: E) -> Result<()>
28 where
29 E: Event;
30}
31
32#[cfg(feature = "async")]
33#[cfg_attr(feature = "async", async_trait::async_trait)]
34pub trait TransportAsync: Send + Sync {
35 async fn cmd<C>(&self, cmd: C) -> Result<C::Return>
36 where
37 C: Command;
38}
39
40#[cfg(feature = "async")]
41#[cfg_attr(feature = "async", async_trait::async_trait)]
42pub trait SubscribableAsync: TransportAsync {
43 async fn subscribe<E: Event>(&self, ev: E)
44 -> Result<tokio::sync::broadcast::Receiver<E::Item>>;
45
46 async fn unsubscribe<E>(&self, ev: E) -> Result<()>
47 where
48 E: Event;
49}