kafrust_protocol/api/
delete_groups.rs1use crate::codec::{Decoder, Encoder};
2use crate::error::Result;
3use crate::header::RequestHeader;
4
5pub const API_KEY: i16 = 42;
6
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct DeleteGroupsRequestV1 {
9 pub correlation_id: i32,
10 pub client_id: Option<String>,
11 pub group_ids: Vec<String>,
12}
13
14impl DeleteGroupsRequestV1 {
15 pub fn encode(&self) -> Result<Vec<u8>> {
16 let mut encoder = Encoder::new();
17 RequestHeader {
18 api_key: API_KEY,
19 api_version: 1,
20 correlation_id: self.correlation_id,
21 client_id: self.client_id.clone(),
22 }
23 .encode_v1(&mut encoder)?;
24 encoder.write_array(Some(&self.group_ids), |encoder, group_id| {
25 encoder.write_string(group_id)
26 })?;
27 Ok(encoder.into_bytes())
28 }
29}
30
31#[derive(Debug, Clone, PartialEq, Eq)]
32pub struct DeleteGroupsResponseV1 {
33 pub throttle_time_ms: i32,
34 pub results: Vec<DeleteGroupResultV1>,
35}
36
37impl DeleteGroupsResponseV1 {
38 pub fn decode_body(decoder: &mut Decoder<'_>) -> Result<Self> {
39 Ok(Self {
40 throttle_time_ms: decoder.read_i32()?,
41 results: decoder
42 .read_array("delete group results", DeleteGroupResultV1::decode)?
43 .unwrap_or_default(),
44 })
45 }
46}
47
48#[derive(Debug, Clone, PartialEq, Eq)]
49pub struct DeleteGroupResultV1 {
50 pub group_id: String,
51 pub error_code: i16,
52}
53
54impl DeleteGroupResultV1 {
55 fn decode(decoder: &mut Decoder<'_>) -> Result<Self> {
56 Ok(Self {
57 group_id: decoder.read_string()?,
58 error_code: decoder.read_i16()?,
59 })
60 }
61}
62
63#[cfg(test)]
64#[allow(clippy::unwrap_used)]
65mod tests {
66 use super::{DeleteGroupsRequestV1, DeleteGroupsResponseV1, API_KEY};
67 use crate::codec::{Decoder, Encoder};
68
69 #[test]
70 fn encodes_delete_groups_v1_request() {
71 let request = DeleteGroupsRequestV1 {
72 correlation_id: 19,
73 client_id: Some("kafrust".to_owned()),
74 group_ids: vec!["orders".to_owned()],
75 };
76
77 assert_eq!(&request.encode().unwrap()[0..4], &[0, 42, 0, 1]);
78 assert_eq!(API_KEY, 42);
79 }
80
81 #[test]
82 fn decodes_delete_groups_v1_response() {
83 let mut bytes = Encoder::new();
84 bytes.write_i32(6);
85 bytes.write_i32(2);
86 bytes.write_string("orders").unwrap();
87 bytes.write_i16(0);
88 bytes.write_string("active").unwrap();
89 bytes.write_i16(68);
90 let bytes = bytes.into_bytes();
91 let mut decoder = Decoder::new(&bytes);
92
93 let response = DeleteGroupsResponseV1::decode_body(&mut decoder).unwrap();
94
95 assert_eq!(response.throttle_time_ms, 6);
96 assert_eq!(response.results.len(), 2);
97 assert_eq!(response.results[0].group_id, "orders");
98 assert_eq!(response.results[0].error_code, 0);
99 assert_eq!(response.results[1].group_id, "active");
100 assert_eq!(response.results[1].error_code, 68);
101 assert!(decoder.is_empty());
102 }
103}