Skip to main content

iggy_binary_protocol/requests/consumer_groups/
get_consumer_group.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use crate::WireError;
19use crate::WireIdentifier;
20use crate::codec::{WireDecode, WireEncode};
21use bytes::BytesMut;
22
23/// `GetConsumerGroup` request.
24///
25/// Wire format: `[stream_id][topic_id][group_id]`
26#[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}