selium_server/topic/
mod.rs

1use anyhow::Result;
2use futures::{channel::mpsc, SinkExt};
3
4pub mod config;
5pub mod pubsub;
6pub mod reqrep;
7
8pub enum Socket {
9    Pubsub(pubsub::Socket),
10    Reqrep(reqrep::Socket),
11}
12
13impl Socket {
14    fn unwrap_pubsub(self) -> pubsub::Socket {
15        match self {
16            Self::Pubsub(s) => s,
17            _ => panic!("Attempted to unwrap non-pubsub socket"),
18        }
19    }
20
21    fn unwrap_reqrep(self) -> reqrep::Socket {
22        match self {
23            Self::Reqrep(s) => s,
24            _ => panic!("Attempted to unwrap non-reqrep socket"),
25        }
26    }
27}
28
29pub enum Sender {
30    Pubsub(mpsc::Sender<pubsub::Socket>),
31    ReqRep(mpsc::Sender<reqrep::Socket>),
32}
33
34impl Sender {
35    pub async fn send(&mut self, sock: Socket) -> Result<()> {
36        match self {
37            Self::Pubsub(ref mut s) => s.send(sock.unwrap_pubsub()).await?,
38            Self::ReqRep(ref mut s) => s.send(sock.unwrap_reqrep()).await?,
39        }
40
41        Ok(())
42    }
43
44    pub fn close_channel(&mut self) {
45        match self {
46            Self::Pubsub(ref mut s) => s.close_channel(),
47            Self::ReqRep(ref mut s) => s.close_channel(),
48        }
49    }
50}