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