kafka_protocol/messages/
create_acls_request.rs

1//! CreateAclsRequest
2//!
3//! See the schema for this message [here](https://github.com/apache/kafka/blob/trunk/clients/src/main/resources/common/message/CreateAclsRequest.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: 1-3
21#[non_exhaustive]
22#[derive(Debug, Clone, PartialEq)]
23pub struct AclCreation {
24    /// The type of the resource.
25    ///
26    /// Supported API versions: 1-3
27    pub resource_type: i8,
28
29    /// The resource name for the ACL.
30    ///
31    /// Supported API versions: 1-3
32    pub resource_name: StrBytes,
33
34    /// The pattern type for the ACL.
35    ///
36    /// Supported API versions: 1-3
37    pub resource_pattern_type: i8,
38
39    /// The principal for the ACL.
40    ///
41    /// Supported API versions: 1-3
42    pub principal: StrBytes,
43
44    /// The host for the ACL.
45    ///
46    /// Supported API versions: 1-3
47    pub host: StrBytes,
48
49    /// The operation type for the ACL (read, write, etc.).
50    ///
51    /// Supported API versions: 1-3
52    pub operation: i8,
53
54    /// The permission type for the ACL (allow, deny, etc.).
55    ///
56    /// Supported API versions: 1-3
57    pub permission_type: i8,
58
59    /// Other tagged fields
60    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
61}
62
63impl AclCreation {
64    /// Sets `resource_type` to the passed value.
65    ///
66    /// The type of the resource.
67    ///
68    /// Supported API versions: 1-3
69    pub fn with_resource_type(mut self, value: i8) -> Self {
70        self.resource_type = value;
71        self
72    }
73    /// Sets `resource_name` to the passed value.
74    ///
75    /// The resource name for the ACL.
76    ///
77    /// Supported API versions: 1-3
78    pub fn with_resource_name(mut self, value: StrBytes) -> Self {
79        self.resource_name = value;
80        self
81    }
82    /// Sets `resource_pattern_type` to the passed value.
83    ///
84    /// The pattern type for the ACL.
85    ///
86    /// Supported API versions: 1-3
87    pub fn with_resource_pattern_type(mut self, value: i8) -> Self {
88        self.resource_pattern_type = value;
89        self
90    }
91    /// Sets `principal` to the passed value.
92    ///
93    /// The principal for the ACL.
94    ///
95    /// Supported API versions: 1-3
96    pub fn with_principal(mut self, value: StrBytes) -> Self {
97        self.principal = value;
98        self
99    }
100    /// Sets `host` to the passed value.
101    ///
102    /// The host for the ACL.
103    ///
104    /// Supported API versions: 1-3
105    pub fn with_host(mut self, value: StrBytes) -> Self {
106        self.host = value;
107        self
108    }
109    /// Sets `operation` to the passed value.
110    ///
111    /// The operation type for the ACL (read, write, etc.).
112    ///
113    /// Supported API versions: 1-3
114    pub fn with_operation(mut self, value: i8) -> Self {
115        self.operation = value;
116        self
117    }
118    /// Sets `permission_type` to the passed value.
119    ///
120    /// The permission type for the ACL (allow, deny, etc.).
121    ///
122    /// Supported API versions: 1-3
123    pub fn with_permission_type(mut self, value: i8) -> Self {
124        self.permission_type = value;
125        self
126    }
127    /// Sets unknown_tagged_fields to the passed value.
128    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
129        self.unknown_tagged_fields = value;
130        self
131    }
132    /// Inserts an entry into unknown_tagged_fields.
133    pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
134        self.unknown_tagged_fields.insert(key, value);
135        self
136    }
137}
138
139#[cfg(feature = "client")]
140impl Encodable for AclCreation {
141    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
142        if version < 1 || version > 3 {
143            bail!("specified version not supported by this message type");
144        }
145        types::Int8.encode(buf, &self.resource_type)?;
146        if version >= 2 {
147            types::CompactString.encode(buf, &self.resource_name)?;
148        } else {
149            types::String.encode(buf, &self.resource_name)?;
150        }
151        types::Int8.encode(buf, &self.resource_pattern_type)?;
152        if version >= 2 {
153            types::CompactString.encode(buf, &self.principal)?;
154        } else {
155            types::String.encode(buf, &self.principal)?;
156        }
157        if version >= 2 {
158            types::CompactString.encode(buf, &self.host)?;
159        } else {
160            types::String.encode(buf, &self.host)?;
161        }
162        types::Int8.encode(buf, &self.operation)?;
163        types::Int8.encode(buf, &self.permission_type)?;
164        if version >= 2 {
165            let num_tagged_fields = self.unknown_tagged_fields.len();
166            if num_tagged_fields > std::u32::MAX as usize {
167                bail!(
168                    "Too many tagged fields to encode ({} fields)",
169                    num_tagged_fields
170                );
171            }
172            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
173
174            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
175        }
176        Ok(())
177    }
178    fn compute_size(&self, version: i16) -> Result<usize> {
179        let mut total_size = 0;
180        total_size += types::Int8.compute_size(&self.resource_type)?;
181        if version >= 2 {
182            total_size += types::CompactString.compute_size(&self.resource_name)?;
183        } else {
184            total_size += types::String.compute_size(&self.resource_name)?;
185        }
186        total_size += types::Int8.compute_size(&self.resource_pattern_type)?;
187        if version >= 2 {
188            total_size += types::CompactString.compute_size(&self.principal)?;
189        } else {
190            total_size += types::String.compute_size(&self.principal)?;
191        }
192        if version >= 2 {
193            total_size += types::CompactString.compute_size(&self.host)?;
194        } else {
195            total_size += types::String.compute_size(&self.host)?;
196        }
197        total_size += types::Int8.compute_size(&self.operation)?;
198        total_size += types::Int8.compute_size(&self.permission_type)?;
199        if version >= 2 {
200            let num_tagged_fields = self.unknown_tagged_fields.len();
201            if num_tagged_fields > std::u32::MAX as usize {
202                bail!(
203                    "Too many tagged fields to encode ({} fields)",
204                    num_tagged_fields
205                );
206            }
207            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
208
209            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
210        }
211        Ok(total_size)
212    }
213}
214
215#[cfg(feature = "broker")]
216impl Decodable for AclCreation {
217    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
218        if version < 1 || version > 3 {
219            bail!("specified version not supported by this message type");
220        }
221        let resource_type = types::Int8.decode(buf)?;
222        let resource_name = if version >= 2 {
223            types::CompactString.decode(buf)?
224        } else {
225            types::String.decode(buf)?
226        };
227        let resource_pattern_type = types::Int8.decode(buf)?;
228        let principal = if version >= 2 {
229            types::CompactString.decode(buf)?
230        } else {
231            types::String.decode(buf)?
232        };
233        let host = if version >= 2 {
234            types::CompactString.decode(buf)?
235        } else {
236            types::String.decode(buf)?
237        };
238        let operation = types::Int8.decode(buf)?;
239        let permission_type = types::Int8.decode(buf)?;
240        let mut unknown_tagged_fields = BTreeMap::new();
241        if version >= 2 {
242            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
243            for _ in 0..num_tagged_fields {
244                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
245                let size: u32 = types::UnsignedVarInt.decode(buf)?;
246                let unknown_value = buf.try_get_bytes(size as usize)?;
247                unknown_tagged_fields.insert(tag as i32, unknown_value);
248            }
249        }
250        Ok(Self {
251            resource_type,
252            resource_name,
253            resource_pattern_type,
254            principal,
255            host,
256            operation,
257            permission_type,
258            unknown_tagged_fields,
259        })
260    }
261}
262
263impl Default for AclCreation {
264    fn default() -> Self {
265        Self {
266            resource_type: 0,
267            resource_name: Default::default(),
268            resource_pattern_type: 3,
269            principal: Default::default(),
270            host: Default::default(),
271            operation: 0,
272            permission_type: 0,
273            unknown_tagged_fields: BTreeMap::new(),
274        }
275    }
276}
277
278impl Message for AclCreation {
279    const VERSIONS: VersionRange = VersionRange { min: 1, max: 3 };
280    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
281}
282
283/// Valid versions: 1-3
284#[non_exhaustive]
285#[derive(Debug, Clone, PartialEq)]
286pub struct CreateAclsRequest {
287    /// The ACLs that we want to create.
288    ///
289    /// Supported API versions: 1-3
290    pub creations: Vec<AclCreation>,
291
292    /// Other tagged fields
293    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
294}
295
296impl CreateAclsRequest {
297    /// Sets `creations` to the passed value.
298    ///
299    /// The ACLs that we want to create.
300    ///
301    /// Supported API versions: 1-3
302    pub fn with_creations(mut self, value: Vec<AclCreation>) -> Self {
303        self.creations = value;
304        self
305    }
306    /// Sets unknown_tagged_fields to the passed value.
307    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
308        self.unknown_tagged_fields = value;
309        self
310    }
311    /// Inserts an entry into unknown_tagged_fields.
312    pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
313        self.unknown_tagged_fields.insert(key, value);
314        self
315    }
316}
317
318#[cfg(feature = "client")]
319impl Encodable for CreateAclsRequest {
320    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
321        if version < 1 || version > 3 {
322            bail!("specified version not supported by this message type");
323        }
324        if version >= 2 {
325            types::CompactArray(types::Struct { version }).encode(buf, &self.creations)?;
326        } else {
327            types::Array(types::Struct { version }).encode(buf, &self.creations)?;
328        }
329        if version >= 2 {
330            let num_tagged_fields = self.unknown_tagged_fields.len();
331            if num_tagged_fields > std::u32::MAX as usize {
332                bail!(
333                    "Too many tagged fields to encode ({} fields)",
334                    num_tagged_fields
335                );
336            }
337            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
338
339            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
340        }
341        Ok(())
342    }
343    fn compute_size(&self, version: i16) -> Result<usize> {
344        let mut total_size = 0;
345        if version >= 2 {
346            total_size +=
347                types::CompactArray(types::Struct { version }).compute_size(&self.creations)?;
348        } else {
349            total_size += types::Array(types::Struct { version }).compute_size(&self.creations)?;
350        }
351        if version >= 2 {
352            let num_tagged_fields = self.unknown_tagged_fields.len();
353            if num_tagged_fields > std::u32::MAX as usize {
354                bail!(
355                    "Too many tagged fields to encode ({} fields)",
356                    num_tagged_fields
357                );
358            }
359            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
360
361            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
362        }
363        Ok(total_size)
364    }
365}
366
367#[cfg(feature = "broker")]
368impl Decodable for CreateAclsRequest {
369    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
370        if version < 1 || version > 3 {
371            bail!("specified version not supported by this message type");
372        }
373        let creations = if version >= 2 {
374            types::CompactArray(types::Struct { version }).decode(buf)?
375        } else {
376            types::Array(types::Struct { version }).decode(buf)?
377        };
378        let mut unknown_tagged_fields = BTreeMap::new();
379        if version >= 2 {
380            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
381            for _ in 0..num_tagged_fields {
382                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
383                let size: u32 = types::UnsignedVarInt.decode(buf)?;
384                let unknown_value = buf.try_get_bytes(size as usize)?;
385                unknown_tagged_fields.insert(tag as i32, unknown_value);
386            }
387        }
388        Ok(Self {
389            creations,
390            unknown_tagged_fields,
391        })
392    }
393}
394
395impl Default for CreateAclsRequest {
396    fn default() -> Self {
397        Self {
398            creations: Default::default(),
399            unknown_tagged_fields: BTreeMap::new(),
400        }
401    }
402}
403
404impl Message for CreateAclsRequest {
405    const VERSIONS: VersionRange = VersionRange { min: 1, max: 3 };
406    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
407}
408
409impl HeaderVersion for CreateAclsRequest {
410    fn header_version(version: i16) -> i16 {
411        if version >= 2 {
412            2
413        } else {
414            1
415        }
416    }
417}