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, compact_string_len, get_compact_nullable_string_owned,
8 get_compact_string_owned, get_nullable_string_owned, get_string_owned, nullable_string_len,
9 put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
10 string_len,
11};
12use crate::tagged_fields::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
13use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
14pub const MIN_VERSION: i16 = 1;
15pub const MAX_VERSION: i16 = 2;
16pub const FLEXIBLE_MIN: i16 = 2;
17
18#[inline]
19fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
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}
29
30impl Encode for RequestHeader {
31 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
32 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
33 return Err(ProtocolError::SchemaMismatch("RequestHeader version out of range"));
34 }
35 let flex = is_flexible(version);
36 if version >= 0 { put_i16(buf, self.request_api_key) }
37 if version >= 0 { put_i16(buf, self.request_api_version) }
38 if version >= 0 { put_i32(buf, self.correlation_id) }
39 if version >= 1 { { let flex = false; if flex { put_compact_nullable_string(buf, self.client_id.as_deref()) } else { put_nullable_string(buf, self.client_id.as_deref()) } } }
40 if flex {
41 let tagged = WriteTaggedFields::new();
42 tagged.write(buf, &self.unknown_tagged_fields);
43 }
44 Ok(())
45 }
46 fn encoded_len(&self, version: i16) -> usize {
47 let flex = is_flexible(version);
48 let mut n: usize = 0;
49 if version >= 0 { n += 2; }
50 if version >= 0 { n += 2; }
51 if version >= 0 { n += 4; }
52 if version >= 1 { n += { let flex = false; if flex { compact_nullable_string_len(self.client_id.as_deref()) } else { nullable_string_len(self.client_id.as_deref()) } }; }
53 if flex {
54 let known_pairs: Vec<(u32, usize)> = Vec::new();
55 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
56 }
57 n
58 }
59}
60
61impl<'de> Decode<'de> for RequestHeader {
62 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
63 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
64 return Err(ProtocolError::SchemaMismatch("RequestHeader version out of range"));
65 }
66 let flex = is_flexible(version);
67 let mut out = Self::default();
68 if version >= 0 { out.request_api_key = get_i16(buf)?; }
69 if version >= 0 { out.request_api_version = get_i16(buf)?; }
70 if version >= 0 { out.correlation_id = get_i32(buf)?; }
71 if version >= 1 { out.client_id = { let flex = false; if flex { get_compact_nullable_string_owned(buf)? } else { get_nullable_string_owned(buf)? } }; }
72 if flex {
73 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
74 Ok(false)
75 })?;
76 }
77 Ok(out)
78 }
79}
80
81#[must_use]
84#[allow(unused_comparisons)]
85pub fn default_json(version: i16) -> ::serde_json::Value {
86 let mut obj = ::serde_json::Map::new();
87 obj.insert("requestApiKey".to_string(), ::serde_json::json!(0));
88 obj.insert("requestApiVersion".to_string(), ::serde_json::json!(0));
89 obj.insert("correlationId".to_string(), ::serde_json::json!(0));
90 if version >= 1 {
91 obj.insert("clientId".to_string(), ::serde_json::Value::Null);
92 }
93 ::serde_json::Value::Object(obj)
94}