Skip to main content

iggy_binary_protocol/requests/topics/
get_topic.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/// `GetTopic` request. Wire format: `[stream_id:WireIdentifier][topic_id:WireIdentifier]`
24#[derive(Debug, Clone, PartialEq, Eq)]
25pub struct GetTopicRequest {
26    pub stream_id: WireIdentifier,
27    pub topic_id: WireIdentifier,
28}
29
30impl WireEncode for GetTopicRequest {
31    fn encoded_size(&self) -> usize {
32        self.stream_id.encoded_size() + self.topic_id.encoded_size()
33    }
34
35    fn encode(&self, buf: &mut BytesMut) {
36        self.stream_id.encode(buf);
37        self.topic_id.encode(buf);
38    }
39}
40
41impl WireDecode for GetTopicRequest {
42    fn decode(buf: &[u8]) -> Result<(Self, usize), WireError> {
43        let (stream_id, mut pos) = WireIdentifier::decode(buf)?;
44        let (topic_id, consumed) = WireIdentifier::decode(&buf[pos..])?;
45        pos += consumed;
46        Ok((
47            Self {
48                stream_id,
49                topic_id,
50            },
51            pos,
52        ))
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn roundtrip_numeric() {
62        let req = GetTopicRequest {
63            stream_id: WireIdentifier::numeric(1),
64            topic_id: WireIdentifier::numeric(2),
65        };
66        let bytes = req.to_bytes();
67        let (decoded, consumed) = GetTopicRequest::decode(&bytes).unwrap();
68        assert_eq!(consumed, bytes.len());
69        assert_eq!(decoded, req);
70    }
71
72    #[test]
73    fn roundtrip_named() {
74        let req = GetTopicRequest {
75            stream_id: WireIdentifier::named("my-stream").unwrap(),
76            topic_id: WireIdentifier::named("my-topic").unwrap(),
77        };
78        let bytes = req.to_bytes();
79        let (decoded, consumed) = GetTopicRequest::decode(&bytes).unwrap();
80        assert_eq!(consumed, bytes.len());
81        assert_eq!(decoded, req);
82    }
83
84    #[test]
85    fn truncated_returns_error() {
86        let req = GetTopicRequest {
87            stream_id: WireIdentifier::numeric(1),
88            topic_id: WireIdentifier::numeric(2),
89        };
90        let bytes = req.to_bytes();
91        for i in 0..bytes.len() {
92            assert!(
93                GetTopicRequest::decode(&bytes[..i]).is_err(),
94                "expected error for truncation at byte {i}"
95            );
96        }
97    }
98}