kafka_protocol/messages/
request_header.rs

1//! RequestHeader
2//!
3//! See the schema for this message [here](https://github.com/apache/kafka/blob/trunk/clients/src/main/resources/common/message/RequestHeader.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-2
21#[non_exhaustive]
22#[derive(Debug, Clone, PartialEq)]
23pub struct RequestHeader {
24    /// The API key of this request.
25    ///
26    /// Supported API versions: 0-2
27    pub request_api_key: i16,
28
29    /// The API version of this request.
30    ///
31    /// Supported API versions: 0-2
32    pub request_api_version: i16,
33
34    /// The correlation ID of this request.
35    ///
36    /// Supported API versions: 0-2
37    pub correlation_id: i32,
38
39    /// The client ID string.
40    ///
41    /// Supported API versions: 1-2
42    pub client_id: Option<StrBytes>,
43
44    /// Other tagged fields
45    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
46}
47
48impl RequestHeader {
49    /// Sets `request_api_key` to the passed value.
50    ///
51    /// The API key of this request.
52    ///
53    /// Supported API versions: 0-2
54    pub fn with_request_api_key(mut self, value: i16) -> Self {
55        self.request_api_key = value;
56        self
57    }
58    /// Sets `request_api_version` to the passed value.
59    ///
60    /// The API version of this request.
61    ///
62    /// Supported API versions: 0-2
63    pub fn with_request_api_version(mut self, value: i16) -> Self {
64        self.request_api_version = value;
65        self
66    }
67    /// Sets `correlation_id` to the passed value.
68    ///
69    /// The correlation ID of this request.
70    ///
71    /// Supported API versions: 0-2
72    pub fn with_correlation_id(mut self, value: i32) -> Self {
73        self.correlation_id = value;
74        self
75    }
76    /// Sets `client_id` to the passed value.
77    ///
78    /// The client ID string.
79    ///
80    /// Supported API versions: 1-2
81    pub fn with_client_id(mut self, value: Option<StrBytes>) -> Self {
82        self.client_id = value;
83        self
84    }
85    /// Sets unknown_tagged_fields to the passed value.
86    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
87        self.unknown_tagged_fields = value;
88        self
89    }
90    /// Inserts an entry into unknown_tagged_fields.
91    pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
92        self.unknown_tagged_fields.insert(key, value);
93        self
94    }
95}
96
97impl Encodable for RequestHeader {
98    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
99        types::Int16.encode(buf, &self.request_api_key)?;
100        types::Int16.encode(buf, &self.request_api_version)?;
101        types::Int32.encode(buf, &self.correlation_id)?;
102        if version >= 1 {
103            types::String.encode(buf, &self.client_id)?;
104        }
105        if version >= 2 {
106            let num_tagged_fields = self.unknown_tagged_fields.len();
107            if num_tagged_fields > std::u32::MAX as usize {
108                bail!(
109                    "Too many tagged fields to encode ({} fields)",
110                    num_tagged_fields
111                );
112            }
113            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
114
115            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
116        }
117        Ok(())
118    }
119    fn compute_size(&self, version: i16) -> Result<usize> {
120        let mut total_size = 0;
121        total_size += types::Int16.compute_size(&self.request_api_key)?;
122        total_size += types::Int16.compute_size(&self.request_api_version)?;
123        total_size += types::Int32.compute_size(&self.correlation_id)?;
124        if version >= 1 {
125            total_size += types::String.compute_size(&self.client_id)?;
126        }
127        if version >= 2 {
128            let num_tagged_fields = self.unknown_tagged_fields.len();
129            if num_tagged_fields > std::u32::MAX as usize {
130                bail!(
131                    "Too many tagged fields to encode ({} fields)",
132                    num_tagged_fields
133                );
134            }
135            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
136
137            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
138        }
139        Ok(total_size)
140    }
141}
142
143impl Decodable for RequestHeader {
144    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
145        let request_api_key = types::Int16.decode(buf)?;
146        let request_api_version = types::Int16.decode(buf)?;
147        let correlation_id = types::Int32.decode(buf)?;
148        let client_id = if version >= 1 {
149            types::String.decode(buf)?
150        } else {
151            Some(Default::default())
152        };
153        let mut unknown_tagged_fields = BTreeMap::new();
154        if version >= 2 {
155            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
156            for _ in 0..num_tagged_fields {
157                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
158                let size: u32 = types::UnsignedVarInt.decode(buf)?;
159                let unknown_value = buf.try_get_bytes(size as usize)?;
160                unknown_tagged_fields.insert(tag as i32, unknown_value);
161            }
162        }
163        Ok(Self {
164            request_api_key,
165            request_api_version,
166            correlation_id,
167            client_id,
168            unknown_tagged_fields,
169        })
170    }
171}
172
173impl Default for RequestHeader {
174    fn default() -> Self {
175        Self {
176            request_api_key: 0,
177            request_api_version: 0,
178            correlation_id: 0,
179            client_id: Some(Default::default()),
180            unknown_tagged_fields: BTreeMap::new(),
181        }
182    }
183}
184
185impl Message for RequestHeader {
186    const VERSIONS: VersionRange = VersionRange { min: 0, max: 2 };
187    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
188}