krabka_protocol/opt/rustwide/workdir/generated/
ApiVersionsRequest.borrowed.rs1use bytes::BufMut;
3
4use crate::{
5 DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields,
6 owned::api_versions_request,
7 primitives::{
8 fixed::{get_i32, put_i32},
9 string_bytes::{
10 compact_nullable_string_len, compact_string_len, nullable_string_len,
11 put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
12 string_len,
13 },
14 string_bytes_borrowed::{
15 get_compact_nullable_string_borrowed, get_compact_string_borrowed,
16 get_nullable_string_borrowed, get_string_borrowed,
17 },
18 },
19 tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
20};
21pub const API_KEY: i16 = 18;
22pub const MIN_VERSION: i16 = 0;
23pub const MAX_VERSION: i16 = 5;
24pub const LATEST_STABLE_VERSION: i16 = 5;
25pub const FLEXIBLE_MIN: i16 = 3;
26#[inline]
27#[must_use]
28pub fn is_flexible(version: i16) -> bool {
29 version >= FLEXIBLE_MIN
30}
31#[derive(Debug, Clone, PartialEq, Eq)]
32pub struct ApiVersionsRequest<'a> {
33 pub client_software_name: &'a str,
34 pub client_software_version: &'a str,
35 pub cluster_id: Option<&'a str>,
36 pub node_id: i32,
37 pub unknown_tagged_fields: UnknownTaggedFields,
38}
39impl Default for ApiVersionsRequest<'_> {
40 fn default() -> Self {
41 Self {
42 client_software_name: "",
43 client_software_version: "",
44 cluster_id: None,
45 node_id: -1i32,
46 unknown_tagged_fields: UnknownTaggedFields::default(),
47 }
48 }
49}
50impl ApiVersionsRequest<'_> {
51 #[must_use]
52 pub fn to_owned(&self) -> api_versions_request::ApiVersionsRequest {
53 api_versions_request::ApiVersionsRequest {
54 client_software_name: (self.client_software_name).to_owned(),
55 client_software_version: (self.client_software_version).to_owned(),
56 cluster_id: (self.cluster_id).map(str::to_owned),
57 node_id: (self.node_id),
58 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
59 }
60 }
61}
62impl Encode for ApiVersionsRequest<'_> {
63 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
64 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
65 return Err(ProtocolError::UnsupportedVersion {
66 api_key: API_KEY,
67 version,
68 });
69 }
70 let flex = is_flexible(version);
71 if version >= 3 {
72 if flex {
73 put_compact_string(buf, self.client_software_name)?;
74 } else {
75 put_string(buf, self.client_software_name)?;
76 }
77 }
78 if version >= 3 {
79 if flex {
80 put_compact_string(buf, self.client_software_version)?;
81 } else {
82 put_string(buf, self.client_software_version)?;
83 }
84 }
85 if version >= 5 {
86 if flex {
87 put_compact_nullable_string(buf, self.cluster_id)?;
88 } else {
89 put_nullable_string(buf, self.cluster_id)?;
90 }
91 }
92 if version >= 5 {
93 put_i32(buf, self.node_id);
94 }
95 if flex {
96 let tagged = WriteTaggedFields::new();
97 tagged.write(buf, &self.unknown_tagged_fields)?;
98 }
99 Ok(())
100 }
101 fn encoded_len(&self, version: i16) -> usize {
102 let flex = is_flexible(version);
103 let mut n: usize = 0;
104 if version >= 3 {
105 n += if flex {
106 compact_string_len(self.client_software_name)
107 } else {
108 string_len(self.client_software_name)
109 };
110 }
111 if version >= 3 {
112 n += if flex {
113 compact_string_len(self.client_software_version)
114 } else {
115 string_len(self.client_software_version)
116 };
117 }
118 if version >= 5 {
119 n += if flex {
120 compact_nullable_string_len(self.cluster_id)
121 } else {
122 nullable_string_len(self.cluster_id)
123 };
124 }
125 if version >= 5 {
126 n += 4;
127 }
128 if flex {
129 let known_pairs: Vec<(u32, usize)> = Vec::new();
130 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
131 }
132 n
133 }
134}
135impl<'de> DecodeBorrow<'de> for ApiVersionsRequest<'de> {
136 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
137 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
138 return Err(ProtocolError::UnsupportedVersion {
139 api_key: API_KEY,
140 version,
141 });
142 }
143 let flex = is_flexible(version);
144 let mut out = Self::default();
145 if version >= 3 {
146 out.client_software_name = if flex {
147 get_compact_string_borrowed(buf)?
148 } else {
149 get_string_borrowed(buf)?
150 };
151 }
152 if version >= 3 {
153 out.client_software_version = if flex {
154 get_compact_string_borrowed(buf)?
155 } else {
156 get_string_borrowed(buf)?
157 };
158 }
159 if version >= 5 {
160 out.cluster_id = if flex {
161 get_compact_nullable_string_borrowed(buf)?
162 } else {
163 get_nullable_string_borrowed(buf)?
164 };
165 }
166 if version >= 5 {
167 out.node_id = get_i32(buf)?;
168 }
169 if flex {
170 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
171 }
172 Ok(out)
173 }
174}
175#[cfg(test)]
176impl ApiVersionsRequest<'_> {
177 #[must_use]
178 pub fn populated(version: i16) -> Self {
179 let mut m = Self::default();
180 if version >= 3 {
181 m.client_software_name = "x";
182 }
183 if version >= 3 {
184 m.client_software_version = "x";
185 }
186 if version >= 5 {
187 m.cluster_id = Some("x");
188 }
189 if version >= 5 {
190 m.node_id = 1i32;
191 }
192 m
193 }
194}