Skip to main content

zeromq_2sat/
message.rs

1use bytes::Bytes;
2
3use std::collections::vec_deque::{Iter, VecDeque};
4use std::convert::{From, TryFrom};
5use std::fmt;
6
7#[derive(Debug)]
8pub struct ZmqEmptyMessageError;
9
10impl fmt::Display for ZmqEmptyMessageError {
11    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
12        write!(f, "Unable to construct an empty ZmqMessage")
13    }
14}
15
16#[derive(Debug, Clone)]
17pub struct ZmqMessage {
18    frames: VecDeque<Bytes>,
19}
20
21impl ZmqMessage {
22    pub fn push_back(&mut self, frame: Bytes) {
23        self.frames.push_back(frame);
24    }
25
26    pub fn push_front(&mut self, frame: Bytes) {
27        self.frames.push_front(frame);
28    }
29
30    pub fn iter(&self) -> Iter<'_, Bytes> {
31        self.frames.iter()
32    }
33
34    pub(crate) fn pop_front(&mut self) -> Option<Bytes> {
35        self.frames.pop_front()
36    }
37
38    pub fn len(&self) -> usize {
39        self.frames.len()
40    }
41
42    pub fn is_empty(&self) -> bool {
43        self.frames.is_empty()
44    }
45
46    pub fn get(&self, index: usize) -> Option<&Bytes> {
47        self.frames.get(index)
48    }
49
50    pub fn into_vec(self) -> Vec<Bytes> {
51        Vec::from(self.frames)
52    }
53
54    pub fn into_vecdeque(self) -> VecDeque<Bytes> {
55        self.frames
56    }
57
58    pub fn prepend(&mut self, message: &ZmqMessage) {
59        for frame in message.iter().rev() {
60            self.push_front(frame.clone());
61        }
62    }
63
64    pub fn split_off(&mut self, at: usize) -> ZmqMessage {
65        let frames = self.frames.split_off(at);
66        ZmqMessage { frames }
67    }
68}
69
70impl TryFrom<Vec<Bytes>> for ZmqMessage {
71    type Error = ZmqEmptyMessageError;
72    fn try_from(v: Vec<Bytes>) -> Result<Self, Self::Error> {
73        if v.is_empty() {
74            Err(ZmqEmptyMessageError)
75        } else {
76            Ok(Self { frames: v.into() })
77        }
78    }
79}
80
81impl TryFrom<VecDeque<Bytes>> for ZmqMessage {
82    type Error = ZmqEmptyMessageError;
83    fn try_from(v: VecDeque<Bytes>) -> Result<Self, Self::Error> {
84        if v.is_empty() {
85            Err(ZmqEmptyMessageError)
86        } else {
87            Ok(Self { frames: v })
88        }
89    }
90}
91
92impl From<Vec<u8>> for ZmqMessage {
93    fn from(v: Vec<u8>) -> Self {
94        ZmqMessage::from(Bytes::from(v))
95    }
96}
97
98impl From<Bytes> for ZmqMessage {
99    fn from(b: Bytes) -> Self {
100        Self {
101            frames: vec![b].into(),
102        }
103    }
104}
105
106impl From<String> for ZmqMessage {
107    fn from(s: String) -> Self {
108        let b: Bytes = s.into();
109        ZmqMessage::from(b)
110    }
111}
112
113impl From<&str> for ZmqMessage {
114    fn from(s: &str) -> Self {
115        ZmqMessage::from(s.to_owned())
116    }
117}
118
119impl TryFrom<ZmqMessage> for Vec<u8> {
120    type Error = &'static str;
121
122    fn try_from(z: ZmqMessage) -> Result<Self, Self::Error> {
123        if z.len() != 1 {
124            return Err("Message must have only 1 frame to convert to Vec<u8>");
125        }
126        Ok(z.into_vecdeque().pop_front().unwrap().to_vec())
127    }
128}
129
130impl TryFrom<ZmqMessage> for String {
131    type Error = &'static str;
132
133    fn try_from(z: ZmqMessage) -> Result<Self, Self::Error> {
134        if z.len() != 1 {
135            return Err("Message must have only 1 frame to convert to String");
136        }
137        match String::from_utf8(z.into_vecdeque().pop_front().unwrap().to_vec()) {
138            Ok(s) => Ok(s),
139            Err(_) => Err("Could not parse string from message"),
140        }
141    }
142}