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: 0-3
21#[non_exhaustive]
22#[derive(Debug, Clone, PartialEq)]
23pub struct AclCreation {
24    /// The type of the resource.
25    ///
26    /// Supported API versions: 0-3
27    pub resource_type: i8,
28
29    /// The resource name for the ACL.
30    ///
31    /// Supported API versions: 0-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: 0-3
42    pub principal: StrBytes,
43
44    /// The host for the ACL.
45    ///
46    /// Supported API versions: 0-3
47    pub host: StrBytes,
48
49    /// The operation type for the ACL (read, write, etc.).
50    ///
51    /// Supported API versions: 0-3
52    pub operation: i8,
53
54    /// The permission type for the ACL (allow, deny, etc.).
55    ///
56    /// Supported API versions: 0-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: 0-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: 0-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: 0-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: 0-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: 0-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: 0-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        types::Int8.encode(buf, &self.resource_type)?;
143        if version >= 2 {
144            types::CompactString.encode(buf, &self.resource_name)?;
145        } else {
146            types::String.encode(buf, &self.resource_name)?;
147        }
148        if version >= 1 {
149            types::Int8.encode(buf, &self.resource_pattern_type)?;
150        } else {
151            if self.resource_pattern_type != 3 {
152                bail!("A field is set that is not available on the selected protocol version");
153            }
154        }
155        if version >= 2 {
156            types::CompactString.encode(buf, &self.principal)?;
157        } else {
158            types::String.encode(buf, &self.principal)?;
159        }
160        if version >= 2 {
161            types::CompactString.encode(buf, &self.host)?;
162        } else {
163            types::String.encode(buf, &self.host)?;
164        }
165        types::Int8.encode(buf, &self.operation)?;
166        types::Int8.encode(buf, &self.permission_type)?;
167        if version >= 2 {
168            let num_tagged_fields = self.unknown_tagged_fields.len();
169            if num_tagged_fields > std::u32::MAX as usize {
170                bail!(
171                    "Too many tagged fields to encode ({} fields)",
172                    num_tagged_fields
173                );
174            }
175            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
176
177            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
178        }
179        Ok(())
180    }
181    fn compute_size(&self, version: i16) -> Result<usize> {
182        let mut total_size = 0;
183        total_size += types::Int8.compute_size(&self.resource_type)?;
184        if version >= 2 {
185            total_size += types::CompactString.compute_size(&self.resource_name)?;
186        } else {
187            total_size += types::String.compute_size(&self.resource_name)?;
188        }
189        if version >= 1 {
190            total_size += types::Int8.compute_size(&self.resource_pattern_type)?;
191        } else {
192            if self.resource_pattern_type != 3 {
193                bail!("A field is set that is not available on the selected protocol version");
194            }
195        }
196        if version >= 2 {
197            total_size += types::CompactString.compute_size(&self.principal)?;
198        } else {
199            total_size += types::String.compute_size(&self.principal)?;
200        }
201        if version >= 2 {
202            total_size += types::CompactString.compute_size(&self.host)?;
203        } else {
204            total_size += types::String.compute_size(&self.host)?;
205        }
206        total_size += types::Int8.compute_size(&self.operation)?;
207        total_size += types::Int8.compute_size(&self.permission_type)?;
208        if version >= 2 {
209            let num_tagged_fields = self.unknown_tagged_fields.len();
210            if num_tagged_fields > std::u32::MAX as usize {
211                bail!(
212                    "Too many tagged fields to encode ({} fields)",
213                    num_tagged_fields
214                );
215            }
216            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
217
218            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
219        }
220        Ok(total_size)
221    }
222}
223
224#[cfg(feature = "broker")]
225impl Decodable for AclCreation {
226    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
227        let resource_type = types::Int8.decode(buf)?;
228        let resource_name = if version >= 2 {
229            types::CompactString.decode(buf)?
230        } else {
231            types::String.decode(buf)?
232        };
233        let resource_pattern_type = if version >= 1 {
234            types::Int8.decode(buf)?
235        } else {
236            3
237        };
238        let principal = if version >= 2 {
239            types::CompactString.decode(buf)?
240        } else {
241            types::String.decode(buf)?
242        };
243        let host = if version >= 2 {
244            types::CompactString.decode(buf)?
245        } else {
246            types::String.decode(buf)?
247        };
248        let operation = types::Int8.decode(buf)?;
249        let permission_type = types::Int8.decode(buf)?;
250        let mut unknown_tagged_fields = BTreeMap::new();
251        if version >= 2 {
252            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
253            for _ in 0..num_tagged_fields {
254                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
255                let size: u32 = types::UnsignedVarInt.decode(buf)?;
256                let unknown_value = buf.try_get_bytes(size as usize)?;
257                unknown_tagged_fields.insert(tag as i32, unknown_value);
258            }
259        }
260        Ok(Self {
261            resource_type,
262            resource_name,
263            resource_pattern_type,
264            principal,
265            host,
266            operation,
267            permission_type,
268            unknown_tagged_fields,
269        })
270    }
271}
272
273impl Default for AclCreation {
274    fn default() -> Self {
275        Self {
276            resource_type: 0,
277            resource_name: Default::default(),
278            resource_pattern_type: 3,
279            principal: Default::default(),
280            host: Default::default(),
281            operation: 0,
282            permission_type: 0,
283            unknown_tagged_fields: BTreeMap::new(),
284        }
285    }
286}
287
288impl Message for AclCreation {
289    const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
290    const DEPRECATED_VERSIONS: Option<VersionRange> = Some(VersionRange { min: 0, max: 0 });
291}
292
293/// Valid versions: 0-3
294#[non_exhaustive]
295#[derive(Debug, Clone, PartialEq)]
296pub struct CreateAclsRequest {
297    /// The ACLs that we want to create.
298    ///
299    /// Supported API versions: 0-3
300    pub creations: Vec<AclCreation>,
301
302    /// Other tagged fields
303    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
304}
305
306impl CreateAclsRequest {
307    /// Sets `creations` to the passed value.
308    ///
309    /// The ACLs that we want to create.
310    ///
311    /// Supported API versions: 0-3
312    pub fn with_creations(mut self, value: Vec<AclCreation>) -> Self {
313        self.creations = value;
314        self
315    }
316    /// Sets unknown_tagged_fields to the passed value.
317    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
318        self.unknown_tagged_fields = value;
319        self
320    }
321    /// Inserts an entry into unknown_tagged_fields.
322    pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
323        self.unknown_tagged_fields.insert(key, value);
324        self
325    }
326}
327
328#[cfg(feature = "client")]
329impl Encodable for CreateAclsRequest {
330    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
331        if version >= 2 {
332            types::CompactArray(types::Struct { version }).encode(buf, &self.creations)?;
333        } else {
334            types::Array(types::Struct { version }).encode(buf, &self.creations)?;
335        }
336        if version >= 2 {
337            let num_tagged_fields = self.unknown_tagged_fields.len();
338            if num_tagged_fields > std::u32::MAX as usize {
339                bail!(
340                    "Too many tagged fields to encode ({} fields)",
341                    num_tagged_fields
342                );
343            }
344            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
345
346            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
347        }
348        Ok(())
349    }
350    fn compute_size(&self, version: i16) -> Result<usize> {
351        let mut total_size = 0;
352        if version >= 2 {
353            total_size +=
354                types::CompactArray(types::Struct { version }).compute_size(&self.creations)?;
355        } else {
356            total_size += types::Array(types::Struct { version }).compute_size(&self.creations)?;
357        }
358        if version >= 2 {
359            let num_tagged_fields = self.unknown_tagged_fields.len();
360            if num_tagged_fields > std::u32::MAX as usize {
361                bail!(
362                    "Too many tagged fields to encode ({} fields)",
363                    num_tagged_fields
364                );
365            }
366            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
367
368            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
369        }
370        Ok(total_size)
371    }
372}
373
374#[cfg(feature = "broker")]
375impl Decodable for CreateAclsRequest {
376    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
377        let creations = if version >= 2 {
378            types::CompactArray(types::Struct { version }).decode(buf)?
379        } else {
380            types::Array(types::Struct { version }).decode(buf)?
381        };
382        let mut unknown_tagged_fields = BTreeMap::new();
383        if version >= 2 {
384            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
385            for _ in 0..num_tagged_fields {
386                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
387                let size: u32 = types::UnsignedVarInt.decode(buf)?;
388                let unknown_value = buf.try_get_bytes(size as usize)?;
389                unknown_tagged_fields.insert(tag as i32, unknown_value);
390            }
391        }
392        Ok(Self {
393            creations,
394            unknown_tagged_fields,
395        })
396    }
397}
398
399impl Default for CreateAclsRequest {
400    fn default() -> Self {
401        Self {
402            creations: Default::default(),
403            unknown_tagged_fields: BTreeMap::new(),
404        }
405    }
406}
407
408impl Message for CreateAclsRequest {
409    const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
410    const DEPRECATED_VERSIONS: Option<VersionRange> = Some(VersionRange { min: 0, max: 0 });
411}
412
413impl HeaderVersion for CreateAclsRequest {
414    fn header_version(version: i16) -> i16 {
415        if version >= 2 {
416            2
417        } else {
418            1
419        }
420    }
421}