kafka_api/schemata/
request_header.rs

1// Copyright 2024 tison <wander4096@gmail.com>
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use byteorder::ReadBytesExt;
16
17use crate::codec::*;
18use crate::IoResult;
19
20// Version 0 of the RequestHeader is only used by v0 of ControlledShutdownRequest.
21//
22// Version 1 is the first version with ClientId.
23//
24// Version 2 is the first flexible version.
25
26#[derive(Debug, Default, Clone)]
27pub struct RequestHeader {
28    /// The API key of this request.
29    pub request_api_key: i16,
30    /// The API version of this request.
31    pub request_api_version: i16,
32    /// The correlation ID of this request.
33    pub correlation_id: i32,
34    /// The client ID string.
35    pub client_id: String,
36    /// Unknown tagged fields.
37    pub unknown_tagged_fields: Vec<RawTaggedField>,
38}
39
40impl Decodable for RequestHeader {
41    fn read<B: ReadBytesExt>(buf: &mut B, version: i16) -> IoResult<Self> {
42        let mut res = RequestHeader {
43            request_api_key: Int16.decode(buf)?,
44            request_api_version: Int16.decode(buf)?,
45            correlation_id: Int32.decode(buf)?,
46            ..Default::default()
47        };
48        if version >= 1 {
49            res.client_id = NullableString(false).decode(buf)?.unwrap_or_default();
50        }
51        if version >= 2 {
52            res.unknown_tagged_fields = RawTaggedFieldList.decode(buf)?;
53        }
54        Ok(res)
55    }
56}