iggy_binary_protocol/requests/topics/
get_topic.rs1use crate::WireError;
19use crate::WireIdentifier;
20use crate::codec::{WireDecode, WireEncode};
21use bytes::BytesMut;
22
23#[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}