kafka_protocol/messages/
list_groups_request.rs

1//! ListGroupsRequest
2//!
3//! See the schema for this message [here](https://github.com/apache/kafka/blob/trunk/clients/src/main/resources/common/message/ListGroupsRequest.json).
4// WARNING: the items of this module are generated and should not be edited directly
5#![allow(unused)]
6
7use std::borrow::Borrow;
8use std::collections::BTreeMap;
9
10use anyhow::{bail, Result};
11use bytes::Bytes;
12use uuid::Uuid;
13
14use crate::protocol::{
15    buf::{ByteBuf, ByteBufMut},
16    compute_unknown_tagged_fields_size, types, write_unknown_tagged_fields, Decodable, Decoder,
17    Encodable, Encoder, HeaderVersion, Message, StrBytes, VersionRange,
18};
19
20/// Valid versions: 0-5
21#[non_exhaustive]
22#[derive(Debug, Clone, PartialEq)]
23pub struct ListGroupsRequest {
24    /// The states of the groups we want to list. If empty, all groups are returned with their state.
25    ///
26    /// Supported API versions: 4-5
27    pub states_filter: Vec<StrBytes>,
28
29    /// The types of the groups we want to list. If empty, all groups are returned with their type.
30    ///
31    /// Supported API versions: 5
32    pub types_filter: Vec<StrBytes>,
33
34    /// Other tagged fields
35    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
36}
37
38impl ListGroupsRequest {
39    /// Sets `states_filter` to the passed value.
40    ///
41    /// The states of the groups we want to list. If empty, all groups are returned with their state.
42    ///
43    /// Supported API versions: 4-5
44    pub fn with_states_filter(mut self, value: Vec<StrBytes>) -> Self {
45        self.states_filter = value;
46        self
47    }
48    /// Sets `types_filter` to the passed value.
49    ///
50    /// The types of the groups we want to list. If empty, all groups are returned with their type.
51    ///
52    /// Supported API versions: 5
53    pub fn with_types_filter(mut self, value: Vec<StrBytes>) -> Self {
54        self.types_filter = value;
55        self
56    }
57    /// Sets unknown_tagged_fields to the passed value.
58    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
59        self.unknown_tagged_fields = value;
60        self
61    }
62    /// Inserts an entry into unknown_tagged_fields.
63    pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
64        self.unknown_tagged_fields.insert(key, value);
65        self
66    }
67}
68
69#[cfg(feature = "client")]
70impl Encodable for ListGroupsRequest {
71    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
72        if version >= 4 {
73            types::CompactArray(types::CompactString).encode(buf, &self.states_filter)?;
74        } else {
75            if !self.states_filter.is_empty() {
76                bail!("A field is set that is not available on the selected protocol version");
77            }
78        }
79        if version >= 5 {
80            types::CompactArray(types::CompactString).encode(buf, &self.types_filter)?;
81        } else {
82            if !self.types_filter.is_empty() {
83                bail!("A field is set that is not available on the selected protocol version");
84            }
85        }
86        if version >= 3 {
87            let num_tagged_fields = self.unknown_tagged_fields.len();
88            if num_tagged_fields > std::u32::MAX as usize {
89                bail!(
90                    "Too many tagged fields to encode ({} fields)",
91                    num_tagged_fields
92                );
93            }
94            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
95
96            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
97        }
98        Ok(())
99    }
100    fn compute_size(&self, version: i16) -> Result<usize> {
101        let mut total_size = 0;
102        if version >= 4 {
103            total_size +=
104                types::CompactArray(types::CompactString).compute_size(&self.states_filter)?;
105        } else {
106            if !self.states_filter.is_empty() {
107                bail!("A field is set that is not available on the selected protocol version");
108            }
109        }
110        if version >= 5 {
111            total_size +=
112                types::CompactArray(types::CompactString).compute_size(&self.types_filter)?;
113        } else {
114            if !self.types_filter.is_empty() {
115                bail!("A field is set that is not available on the selected protocol version");
116            }
117        }
118        if version >= 3 {
119            let num_tagged_fields = self.unknown_tagged_fields.len();
120            if num_tagged_fields > std::u32::MAX as usize {
121                bail!(
122                    "Too many tagged fields to encode ({} fields)",
123                    num_tagged_fields
124                );
125            }
126            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
127
128            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
129        }
130        Ok(total_size)
131    }
132}
133
134#[cfg(feature = "broker")]
135impl Decodable for ListGroupsRequest {
136    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
137        let states_filter = if version >= 4 {
138            types::CompactArray(types::CompactString).decode(buf)?
139        } else {
140            Default::default()
141        };
142        let types_filter = if version >= 5 {
143            types::CompactArray(types::CompactString).decode(buf)?
144        } else {
145            Default::default()
146        };
147        let mut unknown_tagged_fields = BTreeMap::new();
148        if version >= 3 {
149            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
150            for _ in 0..num_tagged_fields {
151                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
152                let size: u32 = types::UnsignedVarInt.decode(buf)?;
153                let unknown_value = buf.try_get_bytes(size as usize)?;
154                unknown_tagged_fields.insert(tag as i32, unknown_value);
155            }
156        }
157        Ok(Self {
158            states_filter,
159            types_filter,
160            unknown_tagged_fields,
161        })
162    }
163}
164
165impl Default for ListGroupsRequest {
166    fn default() -> Self {
167        Self {
168            states_filter: Default::default(),
169            types_filter: Default::default(),
170            unknown_tagged_fields: BTreeMap::new(),
171        }
172    }
173}
174
175impl Message for ListGroupsRequest {
176    const VERSIONS: VersionRange = VersionRange { min: 0, max: 5 };
177    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
178}
179
180impl HeaderVersion for ListGroupsRequest {
181    fn header_version(version: i16) -> i16 {
182        if version >= 3 {
183            2
184        } else {
185            1
186        }
187    }
188}