crabka_protocol/opt/rustwide/workdir/generated/
ApiVersionsRequest.borrowed.rs1use bytes::BufMut;
4use crate::primitives::string_bytes::{
5 compact_string_len, put_compact_string, put_string, string_len,
6};
7use crate::primitives::string_bytes_borrowed::{
8 get_compact_string_borrowed, get_string_borrowed,
9};
10use crate::tagged_fields::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
11use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
12
13pub const API_KEY: i16 = 18;
14pub const MIN_VERSION: i16 = 0;
15pub const MAX_VERSION: i16 = 4;
16pub const FLEXIBLE_MIN: i16 = 3;
17
18#[inline]
19fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
20
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub struct ApiVersionsRequest<'a> {
23 pub client_software_name: &'a str,
24 pub client_software_version: &'a str,
25 pub unknown_tagged_fields: UnknownTaggedFields,
26}
27
28impl<'a> Default for ApiVersionsRequest<'a> {
29 fn default() -> Self {
30 Self {
31 client_software_name: "",
32 client_software_version: "",
33 unknown_tagged_fields: Default::default(),
34 }
35 }
36}
37
38impl<'a> ApiVersionsRequest<'a> {
39 pub fn to_owned(&self) -> crate::owned::api_versions_request::ApiVersionsRequest {
40 crate::owned::api_versions_request::ApiVersionsRequest {
41 client_software_name: (self.client_software_name).to_string(),
42 client_software_version: (self.client_software_version).to_string(),
43 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
44 }
45 }
46}
47
48impl<'a> Encode for ApiVersionsRequest<'a> {
49 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
50 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
51 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
52 }
53 let flex = is_flexible(version);
54 if version >= 3 { if flex { put_compact_string(buf, self.client_software_name) } else { put_string(buf, self.client_software_name) } }
55 if version >= 3 { if flex { put_compact_string(buf, self.client_software_version) } else { put_string(buf, self.client_software_version) } }
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 >= 3 { n += if flex { compact_string_len(self.client_software_name) } else { string_len(self.client_software_name) }; }
66 if version >= 3 { n += if flex { compact_string_len(self.client_software_version) } else { string_len(self.client_software_version) }; }
67 if flex {
68 let known_pairs: Vec<(u32, usize)> = Vec::new();
69 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
70 }
71 n
72 }
73}
74
75impl<'de> DecodeBorrow<'de> for ApiVersionsRequest<'de> {
76 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
77 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
78 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
79 }
80 let flex = is_flexible(version);
81 let mut out = Self::default();
82 if version >= 3 { out.client_software_name = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
83 if version >= 3 { out.client_software_version = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
84 if flex {
85 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
86 Ok(false)
87 })?;
88 }
89 Ok(out)
90 }
91}