kafka_protocol/messages/
response_header.rs1#![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#[non_exhaustive]
22#[derive(Debug, Clone, PartialEq)]
23pub struct ResponseHeader {
24 pub correlation_id: i32,
28
29 pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
31}
32
33impl ResponseHeader {
34 pub fn with_correlation_id(mut self, value: i32) -> Self {
40 self.correlation_id = value;
41 self
42 }
43 pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
45 self.unknown_tagged_fields = value;
46 self
47 }
48 pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
50 self.unknown_tagged_fields.insert(key, value);
51 self
52 }
53}
54
55impl Encodable for ResponseHeader {
56 fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
57 types::Int32.encode(buf, &self.correlation_id)?;
58 if version >= 1 {
59 let num_tagged_fields = self.unknown_tagged_fields.len();
60 if num_tagged_fields > std::u32::MAX as usize {
61 bail!(
62 "Too many tagged fields to encode ({} fields)",
63 num_tagged_fields
64 );
65 }
66 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
67
68 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
69 }
70 Ok(())
71 }
72 fn compute_size(&self, version: i16) -> Result<usize> {
73 let mut total_size = 0;
74 total_size += types::Int32.compute_size(&self.correlation_id)?;
75 if version >= 1 {
76 let num_tagged_fields = self.unknown_tagged_fields.len();
77 if num_tagged_fields > std::u32::MAX as usize {
78 bail!(
79 "Too many tagged fields to encode ({} fields)",
80 num_tagged_fields
81 );
82 }
83 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
84
85 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
86 }
87 Ok(total_size)
88 }
89}
90
91impl Decodable for ResponseHeader {
92 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
93 let correlation_id = types::Int32.decode(buf)?;
94 let mut unknown_tagged_fields = BTreeMap::new();
95 if version >= 1 {
96 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
97 for _ in 0..num_tagged_fields {
98 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
99 let size: u32 = types::UnsignedVarInt.decode(buf)?;
100 let unknown_value = buf.try_get_bytes(size as usize)?;
101 unknown_tagged_fields.insert(tag as i32, unknown_value);
102 }
103 }
104 Ok(Self {
105 correlation_id,
106 unknown_tagged_fields,
107 })
108 }
109}
110
111impl Default for ResponseHeader {
112 fn default() -> Self {
113 Self {
114 correlation_id: 0,
115 unknown_tagged_fields: BTreeMap::new(),
116 }
117 }
118}
119
120impl Message for ResponseHeader {
121 const VERSIONS: VersionRange = VersionRange { min: 0, max: 1 };
122 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
123}