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 if version < 0 || version > 1 {
58 bail!("specified version not supported by this message type");
59 }
60 types::Int32.encode(buf, &self.correlation_id)?;
61 if version >= 1 {
62 let num_tagged_fields = self.unknown_tagged_fields.len();
63 if num_tagged_fields > std::u32::MAX as usize {
64 bail!(
65 "Too many tagged fields to encode ({} fields)",
66 num_tagged_fields
67 );
68 }
69 types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
70
71 write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
72 }
73 Ok(())
74 }
75 fn compute_size(&self, version: i16) -> Result<usize> {
76 let mut total_size = 0;
77 total_size += types::Int32.compute_size(&self.correlation_id)?;
78 if version >= 1 {
79 let num_tagged_fields = self.unknown_tagged_fields.len();
80 if num_tagged_fields > std::u32::MAX as usize {
81 bail!(
82 "Too many tagged fields to encode ({} fields)",
83 num_tagged_fields
84 );
85 }
86 total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
87
88 total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
89 }
90 Ok(total_size)
91 }
92}
93
94impl Decodable for ResponseHeader {
95 fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
96 if version < 0 || version > 1 {
97 bail!("specified version not supported by this message type");
98 }
99 let correlation_id = types::Int32.decode(buf)?;
100 let mut unknown_tagged_fields = BTreeMap::new();
101 if version >= 1 {
102 let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
103 for _ in 0..num_tagged_fields {
104 let tag: u32 = types::UnsignedVarInt.decode(buf)?;
105 let size: u32 = types::UnsignedVarInt.decode(buf)?;
106 let unknown_value = buf.try_get_bytes(size as usize)?;
107 unknown_tagged_fields.insert(tag as i32, unknown_value);
108 }
109 }
110 Ok(Self {
111 correlation_id,
112 unknown_tagged_fields,
113 })
114 }
115}
116
117impl Default for ResponseHeader {
118 fn default() -> Self {
119 Self {
120 correlation_id: 0,
121 unknown_tagged_fields: BTreeMap::new(),
122 }
123 }
124}
125
126impl Message for ResponseHeader {
127 const VERSIONS: VersionRange = VersionRange { min: 0, max: 1 };
128 const DEPRECATED_VERSIONS: Option<VersionRange> = None;
129}