Skip to main content

iggy_binary_protocol/requests/partitions/
delete_partitions.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, read_u32_le};
21use bytes::{BufMut, BytesMut};
22
23/// `DeletePartitions` request.
24///
25/// Wire format: `[stream_id:WireIdentifier][topic_id:WireIdentifier][partitions_count:u32_le]`
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct DeletePartitionsRequest {
28    pub stream_id: WireIdentifier,
29    pub topic_id: WireIdentifier,
30    pub partitions_count: u32,
31}
32
33impl WireEncode for DeletePartitionsRequest {
34    fn encoded_size(&self) -> usize {
35        self.stream_id.encoded_size() + self.topic_id.encoded_size() + 4
36    }
37
38    fn encode(&self, buf: &mut BytesMut) {
39        self.stream_id.encode(buf);
40        self.topic_id.encode(buf);
41        buf.put_u32_le(self.partitions_count);
42    }
43}
44
45impl WireDecode for DeletePartitionsRequest {
46    fn decode(buf: &[u8]) -> Result<(Self, usize), WireError> {
47        let (stream_id, mut pos) = WireIdentifier::decode(buf)?;
48        let (topic_id, consumed) = WireIdentifier::decode(&buf[pos..])?;
49        pos += consumed;
50        let partitions_count = read_u32_le(buf, pos)?;
51        pos += 4;
52        Ok((
53            Self {
54                stream_id,
55                topic_id,
56                partitions_count,
57            },
58            pos,
59        ))
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn roundtrip() {
69        let req = DeletePartitionsRequest {
70            stream_id: WireIdentifier::numeric(1),
71            topic_id: WireIdentifier::numeric(2),
72            partitions_count: 3,
73        };
74        let bytes = req.to_bytes();
75        let (decoded, consumed) = DeletePartitionsRequest::decode(&bytes).unwrap();
76        assert_eq!(consumed, bytes.len());
77        assert_eq!(decoded, req);
78    }
79
80    #[test]
81    fn roundtrip_named() {
82        let req = DeletePartitionsRequest {
83            stream_id: WireIdentifier::named("stream").unwrap(),
84            topic_id: WireIdentifier::named("topic").unwrap(),
85            partitions_count: 2,
86        };
87        let bytes = req.to_bytes();
88        let (decoded, consumed) = DeletePartitionsRequest::decode(&bytes).unwrap();
89        assert_eq!(consumed, bytes.len());
90        assert_eq!(decoded, req);
91    }
92
93    #[test]
94    fn truncated_returns_error() {
95        let req = DeletePartitionsRequest {
96            stream_id: WireIdentifier::numeric(1),
97            topic_id: WireIdentifier::numeric(2),
98            partitions_count: 3,
99        };
100        let bytes = req.to_bytes();
101        for i in 0..bytes.len() {
102            assert!(
103                DeletePartitionsRequest::decode(&bytes[..i]).is_err(),
104                "expected error for truncation at byte {i}"
105            );
106        }
107    }
108
109    #[test]
110    fn encoded_size_matches_output() {
111        let req = DeletePartitionsRequest {
112            stream_id: WireIdentifier::numeric(1),
113            topic_id: WireIdentifier::numeric(2),
114            partitions_count: 5,
115        };
116        assert_eq!(req.encoded_size(), req.to_bytes().len());
117    }
118}