1use serde::{Deserialize, Serialize};
3use crate::request::Request;
4use crate::response::Response;
5
6#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
8#[serde(untagged)]
9pub enum Message {
10 Request(Request),
12 Response(Response),
14}
15
16#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
18pub struct Batch(Vec<Message>);
19
20impl Batch {
21 pub fn new() -> Self {
23 Batch(Vec::new())
24 }
25
26 pub fn with_capacity(capacity: usize) -> Self {
28 Batch(Vec::with_capacity(capacity))
29 }
30
31 pub fn add_request(&mut self, request: Request) {
33 self.0.push(Message::Request(request));
34 }
35
36 pub fn add_response(&mut self, response: Response) {
38 self.0.push(Message::Response(response));
39 }
40
41 pub fn is_empty(&self) -> bool {
43 self.0.is_empty()
44 }
45
46 pub fn len(&self) -> usize {
48 self.0.len()
49 }
50
51 pub fn as_vec(&self) -> &Vec<Message> {
53 &self.0
54 }
55
56 pub fn as_vec_mut(&mut self) -> &mut Vec<Message> {
58 &mut self.0
59 }
60}
61
62impl Message {
63 pub fn is_request(&self) -> bool {
65 matches!(self, Message::Request(_))
66 }
67
68 pub fn is_response(&self) -> bool {
70 matches!(self, Message::Response(_))
71 }
72
73 pub fn as_request(&self) -> Option<&Request> {
75 match self {
76 Message::Request(req) => Some(req),
77 Message::Response(_) => None,
78 }
79 }
80
81 pub fn as_response(&self) -> Option<&Response> {
83 match self {
84 Message::Request(_) => None,
85 Message::Response(res) => Some(res),
86 }
87 }
88}
89
90impl std::ops::Deref for Batch {
92 type Target = Vec<Message>;
93
94 fn deref(&self) -> &Self::Target {
95 &self.0
96 }
97}
98
99impl std::ops::DerefMut for Batch {
100 fn deref_mut(&mut self) -> &mut Self::Target {
101 &mut self.0
102 }
103}
104
105impl IntoIterator for Batch {
107 type Item = Message;
108 type IntoIter = std::vec::IntoIter<Message>;
109
110 fn into_iter(self) -> Self::IntoIter {
111 self.0.into_iter()
112 }
113}
114
115impl<'a> IntoIterator for &'a Batch {
116 type Item = &'a Message;
117 type IntoIter = std::slice::Iter<'a, Message>;
118
119 fn into_iter(self) -> Self::IntoIter {
120 self.0.iter()
121 }
122}
123
124impl<'a> IntoIterator for &'a mut Batch {
125 type Item = &'a mut Message;
126 type IntoIter = std::slice::IterMut<'a, Message>;
127
128 fn into_iter(self) -> Self::IntoIter {
129 self.0.iter_mut()
130 }
131}
132
133impl From<Vec<Message>> for Batch {
135 fn from(vec: Vec<Message>) -> Self {
136 Batch(vec)
137 }
138}