Skip to main content

kafrust_protocol/api/
delete_topics.rs

1use crate::codec::{Decoder, Encoder};
2use crate::error::Result;
3use crate::header::RequestHeader;
4
5pub const API_KEY: i16 = 20;
6
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct DeleteTopicsRequestV3 {
9    pub correlation_id: i32,
10    pub client_id: Option<String>,
11    pub topic_names: Vec<String>,
12    pub timeout_ms: i32,
13}
14
15impl DeleteTopicsRequestV3 {
16    pub fn encode(&self) -> Result<Vec<u8>> {
17        let mut encoder = Encoder::new();
18        RequestHeader {
19            api_key: API_KEY,
20            api_version: 3,
21            correlation_id: self.correlation_id,
22            client_id: self.client_id.clone(),
23        }
24        .encode_v1(&mut encoder)?;
25        encoder.write_array(Some(&self.topic_names), |encoder, topic_name| {
26            encoder.write_string(topic_name)
27        })?;
28        encoder.write_i32(self.timeout_ms);
29        Ok(encoder.into_bytes())
30    }
31}
32
33#[derive(Debug, Clone, PartialEq, Eq)]
34pub struct DeleteTopicsResponseV3 {
35    pub throttle_time_ms: i32,
36    pub topics: Vec<DeleteTopicsTopicResultV3>,
37}
38
39impl DeleteTopicsResponseV3 {
40    pub fn decode_body(decoder: &mut Decoder<'_>) -> Result<Self> {
41        Ok(Self {
42            throttle_time_ms: decoder.read_i32()?,
43            topics: decoder
44                .read_array("delete topics results", DeleteTopicsTopicResultV3::decode)?
45                .unwrap_or_default(),
46        })
47    }
48}
49
50#[derive(Debug, Clone, PartialEq, Eq)]
51pub struct DeleteTopicsTopicResultV3 {
52    pub name: String,
53    pub error_code: i16,
54}
55
56impl DeleteTopicsTopicResultV3 {
57    fn decode(decoder: &mut Decoder<'_>) -> Result<Self> {
58        Ok(Self {
59            name: decoder.read_string()?,
60            error_code: decoder.read_i16()?,
61        })
62    }
63}
64
65#[cfg(test)]
66#[allow(clippy::unwrap_used)]
67mod tests {
68    use super::{DeleteTopicsRequestV3, DeleteTopicsResponseV3, API_KEY};
69    use crate::codec::Decoder;
70
71    #[test]
72    fn encodes_delete_topics_v3_request() {
73        let request = DeleteTopicsRequestV3 {
74            correlation_id: 11,
75            client_id: Some("kafrust".to_owned()),
76            topic_names: vec!["orders".to_owned(), "payments".to_owned()],
77            timeout_ms: 30_000,
78        };
79
80        assert_eq!(
81            request.encode().unwrap(),
82            [
83                0, 20, // API key
84                0, 3, // API version
85                0, 0, 0, 11, // correlation ID
86                0, 7, b'k', b'a', b'f', b'r', b'u', b's', b't', // client ID
87                0, 0, 0, 2, // topic count
88                0, 6, b'o', b'r', b'd', b'e', b'r', b's', // topic
89                0, 8, b'p', b'a', b'y', b'm', b'e', b'n', b't', b's', // topic
90                0, 0, 117, 48, // timeout
91            ]
92        );
93        assert_eq!(API_KEY, 20);
94    }
95
96    #[test]
97    fn decodes_delete_topics_v3_response() {
98        let bytes = [
99            0, 0, 0, 8, // throttle time
100            0, 0, 0, 2, // topic count
101            0, 6, b'o', b'r', b'd', b'e', b'r', b's', // topic
102            0, 0, // success
103            0, 8, b'p', b'a', b'y', b'm', b'e', b'n', b't', b's', // topic
104            0, 3, // unknown topic or partition
105        ];
106        let mut decoder = Decoder::new(&bytes);
107
108        let response = DeleteTopicsResponseV3::decode_body(&mut decoder).unwrap();
109
110        assert_eq!(response.throttle_time_ms, 8);
111        assert_eq!(response.topics.len(), 2);
112        assert_eq!(response.topics[0].name, "orders");
113        assert_eq!(response.topics[0].error_code, 0);
114        assert_eq!(response.topics[1].name, "payments");
115        assert_eq!(response.topics[1].error_code, 3);
116        assert!(decoder.is_empty());
117    }
118}