crabka_protocol/opt/rustwide/workdir/generated/
RequestHeader.borrowed.rs1use crate::primitives::fixed::{get_i16, get_i32, put_i16, put_i32};
3use crate::primitives::string_bytes::{
4 compact_nullable_string_len, nullable_string_len, put_compact_nullable_string,
5 put_nullable_string,
6};
7use crate::primitives::string_bytes_borrowed::{
8 get_compact_nullable_string_borrowed, get_nullable_string_borrowed,
9};
10use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
11use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
12use bytes::BufMut;
13pub const MIN_VERSION: i16 = 1;
14pub const MAX_VERSION: i16 = 2;
15pub const FLEXIBLE_MIN: i16 = 2;
16#[inline]
17fn is_flexible(version: i16) -> bool {
18 version >= FLEXIBLE_MIN
19}
20#[derive(Debug, Clone, PartialEq, Eq, Default)]
21pub struct RequestHeader<'a> {
22 pub request_api_key: i16,
23 pub request_api_version: i16,
24 pub correlation_id: i32,
25 pub client_id: Option<&'a str>,
26 pub unknown_tagged_fields: UnknownTaggedFields,
27}
28impl RequestHeader<'_> {
29 pub fn to_owned(&self) -> crate::owned::request_header::RequestHeader {
30 crate::owned::request_header::RequestHeader {
31 request_api_key: (self.request_api_key),
32 request_api_version: (self.request_api_version),
33 correlation_id: (self.correlation_id),
34 client_id: (self.client_id).map(std::string::ToString::to_string),
35 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
36 }
37 }
38}
39impl Encode for RequestHeader<'_> {
40 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
41 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
42 return Err(ProtocolError::SchemaMismatch(
43 "RequestHeader version out of range",
44 ));
45 }
46 let flex = is_flexible(version);
47 if version >= 0 {
48 put_i16(buf, self.request_api_key);
49 }
50 if version >= 0 {
51 put_i16(buf, self.request_api_version);
52 }
53 if version >= 0 {
54 put_i32(buf, self.correlation_id);
55 }
56 if version >= 1 {
57 {
58 let flex = false;
59 if flex {
60 put_compact_nullable_string(buf, self.client_id);
61 } else {
62 put_nullable_string(buf, self.client_id);
63 }
64 }
65 }
66 if flex {
67 let tagged = WriteTaggedFields::new();
68 tagged.write(buf, &self.unknown_tagged_fields);
69 }
70 Ok(())
71 }
72 fn encoded_len(&self, version: i16) -> usize {
73 let flex = is_flexible(version);
74 let mut n: usize = 0;
75 if version >= 0 {
76 n += 2;
77 }
78 if version >= 0 {
79 n += 2;
80 }
81 if version >= 0 {
82 n += 4;
83 }
84 if version >= 1 {
85 n += {
86 let flex = false;
87 if flex {
88 compact_nullable_string_len(self.client_id)
89 } else {
90 nullable_string_len(self.client_id)
91 }
92 };
93 }
94 if flex {
95 let known_pairs: Vec<(u32, usize)> = Vec::new();
96 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
97 }
98 n
99 }
100}
101impl<'de> DecodeBorrow<'de> for RequestHeader<'de> {
102 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
103 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
104 return Err(ProtocolError::SchemaMismatch(
105 "RequestHeader version out of range",
106 ));
107 }
108 let flex = is_flexible(version);
109 let mut out = Self::default();
110 if version >= 0 {
111 out.request_api_key = get_i16(buf)?;
112 }
113 if version >= 0 {
114 out.request_api_version = get_i16(buf)?;
115 }
116 if version >= 0 {
117 out.correlation_id = get_i32(buf)?;
118 }
119 if version >= 1 {
120 out.client_id = {
121 let flex = false;
122 if flex {
123 get_compact_nullable_string_borrowed(buf)?
124 } else {
125 get_nullable_string_borrowed(buf)?
126 }
127 };
128 }
129 if flex {
130 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
131 }
132 Ok(out)
133 }
134}
135#[cfg(test)]
136impl RequestHeader<'_> {
137 #[must_use]
138 pub fn populated(version: i16) -> Self {
139 let mut m = Self::default();
140 if version >= 0 {
141 m.request_api_key = 1i16;
142 }
143 if version >= 0 {
144 m.request_api_version = 1i16;
145 }
146 if version >= 0 {
147 m.correlation_id = 1i32;
148 }
149 if version >= 1 {
150 m.client_id = Some("x");
151 }
152 m
153 }
154}