odradek_protocol/messages/
request_header.rs1#![allow(
6 clippy::field_reassign_with_default,
7 clippy::nonminimal_bool,
8 clippy::derivable_impls,
9 clippy::borrow_deref_ref,
10 clippy::double_parens,
11 unused_parens
12)]
13
14use bytes::{Buf, BufMut};
15
16use crate::budget::{Budget, Limits};
17#[allow(unused_imports)]
18use crate::error::{DecodeError, EncodeError};
19use crate::wire::{self, RawTaggedField};
20
21pub fn is_flexible(version: i16) -> bool {
23 let _ = version;
24 version >= 2
25}
26
27#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct RequestHeader {
31 pub request_api_key: i16,
33 pub request_api_version: i16,
35 pub correlation_id: i32,
37 pub client_id: Option<String>,
39 pub unknown_tagged_fields: Vec<RawTaggedField>,
41}
42
43impl Default for RequestHeader {
44 fn default() -> Self {
45 Self {
46 request_api_key: 0,
47 request_api_version: 0,
48 correlation_id: 0,
49 client_id: Some(String::new()),
50 unknown_tagged_fields: Vec::new(),
51 }
52 }
53}
54
55impl RequestHeader {
56 pub const MIN_VERSION: i16 = 1;
58 pub const MAX_VERSION: i16 = 2;
60
61 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
63 buf.put_i16(self.request_api_key);
64 buf.put_i16(self.request_api_version);
65 buf.put_i32(self.correlation_id);
66 wire::put_nullable_string(buf, self.client_id.as_deref())?;
67 if is_flexible(version) {
68 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
69 }
70 Ok(())
71 }
72
73 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
76 Self::decode_with_limits(buf, version, Limits::default())
77 }
78
79 pub fn decode_with_limits(
81 buf: &mut impl Buf,
82 version: i16,
83 limits: Limits,
84 ) -> Result<Self, DecodeError> {
85 let mut budget = limits.budget(buf.remaining());
86 Self::decode_with_budget(buf, version, &mut budget)
87 }
88
89 pub fn decode_with_budget(
91 buf: &mut impl Buf,
92 version: i16,
93 budget: &mut Budget,
94 ) -> Result<Self, DecodeError> {
95 let mut this = Self::default();
96 this.request_api_key = wire::get_i16(buf)?;
97 this.request_api_version = wire::get_i16(buf)?;
98 this.correlation_id = wire::get_i32(buf)?;
99 this.client_id = wire::get_nullable_string(buf)?;
100 if is_flexible(version) {
101 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
102 }
103 Ok(this)
104 }
105}