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        if version < 0 || version > 2 {
100            bail!("specified version not supported by this message type");
101        }
102        types::Int16.encode(buf, &self.request_api_key)?;
103        types::Int16.encode(buf, &self.request_api_version)?;
104        types::Int32.encode(buf, &self.correlation_id)?;
105        if version >= 1 {
106            types::String.encode(buf, &self.client_id)?;
107        }
108        if version >= 2 {
109            let num_tagged_fields = self.unknown_tagged_fields.len();
110            if num_tagged_fields > std::u32::MAX as usize {
111                bail!(
112                    "Too many tagged fields to encode ({} fields)",
113                    num_tagged_fields
114                );
115            }
116            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
117
118            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
119        }
120        Ok(())
121    }
122    fn compute_size(&self, version: i16) -> Result<usize> {
123        let mut total_size = 0;
124        total_size += types::Int16.compute_size(&self.request_api_key)?;
125        total_size += types::Int16.compute_size(&self.request_api_version)?;
126        total_size += types::Int32.compute_size(&self.correlation_id)?;
127        if version >= 1 {
128            total_size += types::String.compute_size(&self.client_id)?;
129        }
130        if version >= 2 {
131            let num_tagged_fields = self.unknown_tagged_fields.len();
132            if num_tagged_fields > std::u32::MAX as usize {
133                bail!(
134                    "Too many tagged fields to encode ({} fields)",
135                    num_tagged_fields
136                );
137            }
138            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
139
140            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
141        }
142        Ok(total_size)
143    }
144}
145
146impl Decodable for RequestHeader {
147    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
148        if version < 0 || version > 2 {
149            bail!("specified version not supported by this message type");
150        }
151        let request_api_key = types::Int16.decode(buf)?;
152        let request_api_version = types::Int16.decode(buf)?;
153        let correlation_id = types::Int32.decode(buf)?;
154        let client_id = if version >= 1 {
155            types::String.decode(buf)?
156        } else {
157            Some(Default::default())
158        };
159        let mut unknown_tagged_fields = BTreeMap::new();
160        if version >= 2 {
161            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
162            for _ in 0..num_tagged_fields {
163                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
164                let size: u32 = types::UnsignedVarInt.decode(buf)?;
165                let unknown_value = buf.try_get_bytes(size as usize)?;
166                unknown_tagged_fields.insert(tag as i32, unknown_value);
167            }
168        }
169        Ok(Self {
170            request_api_key,
171            request_api_version,
172            correlation_id,
173            client_id,
174            unknown_tagged_fields,
175        })
176    }
177}
178
179impl Default for RequestHeader {
180    fn default() -> Self {
181        Self {
182            request_api_key: 0,
183            request_api_version: 0,
184            correlation_id: 0,
185            client_id: Some(Default::default()),
186            unknown_tagged_fields: BTreeMap::new(),
187        }
188    }
189}
190
191impl Message for RequestHeader {
192    const VERSIONS: VersionRange = VersionRange { min: 0, max: 2 };
193    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
194}