iggy_binary_protocol/requests/consumer_groups/
get_consumer_group.rs1use crate::WireError;
19use crate::WireIdentifier;
20use crate::codec::{WireDecode, WireEncode};
21use bytes::BytesMut;
22
23#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct GetConsumerGroupRequest {
28 pub stream_id: WireIdentifier,
29 pub topic_id: WireIdentifier,
30 pub group_id: WireIdentifier,
31}
32
33impl WireEncode for GetConsumerGroupRequest {
34 fn encoded_size(&self) -> usize {
35 self.stream_id.encoded_size() + self.topic_id.encoded_size() + self.group_id.encoded_size()
36 }
37
38 fn encode(&self, buf: &mut BytesMut) {
39 self.stream_id.encode(buf);
40 self.topic_id.encode(buf);
41 self.group_id.encode(buf);
42 }
43}
44
45impl WireDecode for GetConsumerGroupRequest {
46 fn decode(buf: &[u8]) -> Result<(Self, usize), WireError> {
47 let mut pos = 0;
48 let (stream_id, n) = WireIdentifier::decode(&buf[pos..])?;
49 pos += n;
50 let (topic_id, n) = WireIdentifier::decode(&buf[pos..])?;
51 pos += n;
52 let (group_id, n) = WireIdentifier::decode(&buf[pos..])?;
53 pos += n;
54 Ok((
55 Self {
56 stream_id,
57 topic_id,
58 group_id,
59 },
60 pos,
61 ))
62 }
63}
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68
69 #[test]
70 fn roundtrip_numeric() {
71 let req = GetConsumerGroupRequest {
72 stream_id: WireIdentifier::numeric(1),
73 topic_id: WireIdentifier::numeric(2),
74 group_id: WireIdentifier::numeric(3),
75 };
76 let bytes = req.to_bytes();
77 let (decoded, consumed) = GetConsumerGroupRequest::decode(&bytes).unwrap();
78 assert_eq!(consumed, bytes.len());
79 assert_eq!(decoded, req);
80 }
81
82 #[test]
83 fn roundtrip_named() {
84 let req = GetConsumerGroupRequest {
85 stream_id: WireIdentifier::named("stream-1").unwrap(),
86 topic_id: WireIdentifier::named("topic-1").unwrap(),
87 group_id: WireIdentifier::named("group-1").unwrap(),
88 };
89 let bytes = req.to_bytes();
90 let (decoded, consumed) = GetConsumerGroupRequest::decode(&bytes).unwrap();
91 assert_eq!(consumed, bytes.len());
92 assert_eq!(decoded, req);
93 }
94
95 #[test]
96 fn truncated_returns_error() {
97 let req = GetConsumerGroupRequest {
98 stream_id: WireIdentifier::numeric(1),
99 topic_id: WireIdentifier::numeric(2),
100 group_id: WireIdentifier::numeric(3),
101 };
102 let bytes = req.to_bytes();
103 for i in 0..bytes.len() {
104 assert!(
105 GetConsumerGroupRequest::decode(&bytes[..i]).is_err(),
106 "expected error for truncation at byte {i}"
107 );
108 }
109 }
110}