1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use anyhow::Result;
use futures::{channel::mpsc, SinkExt};

pub mod pubsub;
pub mod reqrep;

pub enum Socket<T, E> {
    Pubsub(pubsub::Socket<T, E>),
    Reqrep(reqrep::Socket<E>),
}

impl<T, E> Socket<T, E> {
    fn unwrap_pubsub(self) -> pubsub::Socket<T, E> {
        match self {
            Self::Pubsub(s) => s,
            _ => panic!("Attempted to unwrap non-pubsub socket"),
        }
    }

    fn unwrap_reqrep(self) -> reqrep::Socket<E> {
        match self {
            Self::Reqrep(s) => s,
            _ => panic!("Attempted to unwrap non-reqrep socket"),
        }
    }
}

pub enum Sender<T, E> {
    Pubsub(mpsc::Sender<pubsub::Socket<T, E>>),
    ReqRep(mpsc::Sender<reqrep::Socket<E>>),
}

impl<T, E> Sender<T, E> {
    pub async fn send(&mut self, sock: Socket<T, E>) -> Result<()> {
        match self {
            Self::Pubsub(ref mut s) => s.send(sock.unwrap_pubsub()).await?,
            Self::ReqRep(ref mut s) => s.send(sock.unwrap_reqrep()).await?,
        }

        Ok(())
    }

    pub fn close_channel(&mut self) {
        match self {
            Self::Pubsub(ref mut s) => s.close_channel(),
            Self::ReqRep(ref mut s) => s.close_channel(),
        }
    }
}