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