crabka_protocol/opt/rustwide/workdir/generated/
ApiVersionsRequest.borrowed.rs1use crate::primitives::string_bytes::{
3 compact_string_len, put_compact_string, put_string, string_len,
4};
5use crate::primitives::string_bytes_borrowed::{get_compact_string_borrowed, get_string_borrowed};
6use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
7use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
8use bytes::BufMut;
9pub const API_KEY: i16 = 18;
10pub const MIN_VERSION: i16 = 0;
11pub const MAX_VERSION: i16 = 4;
12pub const FLEXIBLE_MIN: i16 = 3;
13#[inline]
14fn is_flexible(version: i16) -> bool {
15 version >= FLEXIBLE_MIN
16}
17#[derive(Debug, Clone, PartialEq, Eq, Default)]
18pub struct ApiVersionsRequest<'a> {
19 pub client_software_name: &'a str,
20 pub client_software_version: &'a str,
21 pub unknown_tagged_fields: UnknownTaggedFields,
22}
23impl ApiVersionsRequest<'_> {
24 pub fn to_owned(&self) -> crate::owned::api_versions_request::ApiVersionsRequest {
25 crate::owned::api_versions_request::ApiVersionsRequest {
26 client_software_name: (self.client_software_name).to_string(),
27 client_software_version: (self.client_software_version).to_string(),
28 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
29 }
30 }
31}
32impl Encode for ApiVersionsRequest<'_> {
33 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
34 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
35 return Err(ProtocolError::UnsupportedVersion {
36 api_key: API_KEY,
37 version,
38 });
39 }
40 let flex = is_flexible(version);
41 if version >= 3 {
42 if flex {
43 put_compact_string(buf, self.client_software_name);
44 } else {
45 put_string(buf, self.client_software_name);
46 }
47 }
48 if version >= 3 {
49 if flex {
50 put_compact_string(buf, self.client_software_version);
51 } else {
52 put_string(buf, self.client_software_version);
53 }
54 }
55 if flex {
56 let tagged = WriteTaggedFields::new();
57 tagged.write(buf, &self.unknown_tagged_fields);
58 }
59 Ok(())
60 }
61 fn encoded_len(&self, version: i16) -> usize {
62 let flex = is_flexible(version);
63 let mut n: usize = 0;
64 if version >= 3 {
65 n += if flex {
66 compact_string_len(self.client_software_name)
67 } else {
68 string_len(self.client_software_name)
69 };
70 }
71 if version >= 3 {
72 n += if flex {
73 compact_string_len(self.client_software_version)
74 } else {
75 string_len(self.client_software_version)
76 };
77 }
78 if flex {
79 let known_pairs: Vec<(u32, usize)> = Vec::new();
80 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
81 }
82 n
83 }
84}
85impl<'de> DecodeBorrow<'de> for ApiVersionsRequest<'de> {
86 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
87 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
88 return Err(ProtocolError::UnsupportedVersion {
89 api_key: API_KEY,
90 version,
91 });
92 }
93 let flex = is_flexible(version);
94 let mut out = Self::default();
95 if version >= 3 {
96 out.client_software_name = if flex {
97 get_compact_string_borrowed(buf)?
98 } else {
99 get_string_borrowed(buf)?
100 };
101 }
102 if version >= 3 {
103 out.client_software_version = if flex {
104 get_compact_string_borrowed(buf)?
105 } else {
106 get_string_borrowed(buf)?
107 };
108 }
109 if flex {
110 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
111 }
112 Ok(out)
113 }
114}
115#[cfg(test)]
116impl ApiVersionsRequest<'_> {
117 #[must_use]
118 pub fn populated(version: i16) -> Self {
119 let mut m = Self::default();
120 if version >= 3 {
121 m.client_software_name = "x";
122 }
123 if version >= 3 {
124 m.client_software_version = "x";
125 }
126 m
127 }
128}