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: 1-2
21#[non_exhaustive]
22#[derive(Debug, Clone, PartialEq)]
23pub struct RequestHeader {
24    /// The API key of this request.
25    ///
26    /// Supported API versions: 1-2
27    pub request_api_key: i16,
28
29    /// The API version of this request.
30    ///
31    /// Supported API versions: 1-2
32    pub request_api_version: i16,
33
34    /// The correlation ID of this request.
35    ///
36    /// Supported API versions: 1-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: 1-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: 1-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: 1-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 < 1 || 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        types::String.encode(buf, &self.client_id)?;
106        if version >= 2 {
107            let num_tagged_fields = self.unknown_tagged_fields.len();
108            if num_tagged_fields > std::u32::MAX as usize {
109                bail!(
110                    "Too many tagged fields to encode ({} fields)",
111                    num_tagged_fields
112                );
113            }
114            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
115
116            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
117        }
118        Ok(())
119    }
120    fn compute_size(&self, version: i16) -> Result<usize> {
121        let mut total_size = 0;
122        total_size += types::Int16.compute_size(&self.request_api_key)?;
123        total_size += types::Int16.compute_size(&self.request_api_version)?;
124        total_size += types::Int32.compute_size(&self.correlation_id)?;
125        total_size += types::String.compute_size(&self.client_id)?;
126        if version >= 2 {
127            let num_tagged_fields = self.unknown_tagged_fields.len();
128            if num_tagged_fields > std::u32::MAX as usize {
129                bail!(
130                    "Too many tagged fields to encode ({} fields)",
131                    num_tagged_fields
132                );
133            }
134            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
135
136            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
137        }
138        Ok(total_size)
139    }
140}
141
142impl Decodable for RequestHeader {
143    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
144        if version < 1 || version > 2 {
145            bail!("specified version not supported by this message type");
146        }
147        let request_api_key = types::Int16.decode(buf)?;
148        let request_api_version = types::Int16.decode(buf)?;
149        let correlation_id = types::Int32.decode(buf)?;
150        let client_id = types::String.decode(buf)?;
151        let mut unknown_tagged_fields = BTreeMap::new();
152        if version >= 2 {
153            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
154            for _ in 0..num_tagged_fields {
155                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
156                let size: u32 = types::UnsignedVarInt.decode(buf)?;
157                let unknown_value = buf.try_get_bytes(size as usize)?;
158                unknown_tagged_fields.insert(tag as i32, unknown_value);
159            }
160        }
161        Ok(Self {
162            request_api_key,
163            request_api_version,
164            correlation_id,
165            client_id,
166            unknown_tagged_fields,
167        })
168    }
169}
170
171impl Default for RequestHeader {
172    fn default() -> Self {
173        Self {
174            request_api_key: 0,
175            request_api_version: 0,
176            correlation_id: 0,
177            client_id: Some(Default::default()),
178            unknown_tagged_fields: BTreeMap::new(),
179        }
180    }
181}
182
183impl Message for RequestHeader {
184    const VERSIONS: VersionRange = VersionRange { min: 1, max: 2 };
185    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
186}