odradek_protocol/messages/
describe_configs_request.rs1#![allow(
6 clippy::field_reassign_with_default,
7 clippy::nonminimal_bool,
8 clippy::derivable_impls,
9 clippy::borrow_deref_ref,
10 clippy::double_parens,
11 unused_parens
12)]
13
14use bytes::{Buf, BufMut};
15
16use crate::budget::{Budget, Limits};
17#[allow(unused_imports)]
18use crate::error::{DecodeError, EncodeError};
19use crate::wire::{self, RawTaggedField};
20
21pub fn is_flexible(version: i16) -> bool {
23 let _ = version;
24 version >= 4
25}
26
27#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct DescribeConfigsRequest {
31 pub resources: Vec<DescribeConfigsResource>,
33 pub include_synonyms: bool,
35 pub include_documentation: bool,
37 pub unknown_tagged_fields: Vec<RawTaggedField>,
39}
40
41impl Default for DescribeConfigsRequest {
42 fn default() -> Self {
43 Self {
44 resources: Vec::new(),
45 include_synonyms: false,
46 include_documentation: false,
47 unknown_tagged_fields: Vec::new(),
48 }
49 }
50}
51
52impl DescribeConfigsRequest {
53 pub const API_KEY: i16 = 32;
55 pub const MIN_VERSION: i16 = 1;
57 pub const MAX_VERSION: i16 = 4;
59
60 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
62 if is_flexible(version) {
63 wire::put_compact_array_len(buf, Some(self.resources.len()));
64 } else {
65 wire::put_array_len(buf, Some(self.resources.len()))?;
66 }
67 for item in &self.resources {
68 item.encode(buf, version)?;
69 }
70 buf.put_u8(u8::from(self.include_synonyms));
71 if version >= 3 {
72 buf.put_u8(u8::from(self.include_documentation));
73 }
74 if is_flexible(version) {
75 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
76 }
77 Ok(())
78 }
79
80 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
83 Self::decode_with_limits(buf, version, Limits::default())
84 }
85
86 pub fn decode_with_limits(
88 buf: &mut impl Buf,
89 version: i16,
90 limits: Limits,
91 ) -> Result<Self, DecodeError> {
92 let mut budget = limits.budget(buf.remaining());
93 Self::decode_with_budget(buf, version, &mut budget)
94 }
95
96 pub fn decode_with_budget(
98 buf: &mut impl Buf,
99 version: i16,
100 budget: &mut Budget,
101 ) -> Result<Self, DecodeError> {
102 let mut this = Self::default();
103 this.resources = {
104 let len = if is_flexible(version) {
105 wire::get_compact_array_len(buf)?
106 } else {
107 wire::get_array_len(buf)?
108 };
109 match len {
110 None => return Err(DecodeError::InvalidLength(-1)),
111 Some(n) => {
112 let mut items = Vec::new();
113 for _ in 0..n {
114 let mark = buf.remaining();
115 let item =
116 DescribeConfigsResource::decode_with_budget(buf, version, budget)?;
117 budget.progress(mark, buf.remaining())?;
118 budget.push(&mut items, item)?;
119 }
120 items
121 }
122 }
123 };
124 this.include_synonyms = wire::get_bool(buf)?;
125 if version >= 3 {
126 this.include_documentation = wire::get_bool(buf)?;
127 }
128 if is_flexible(version) {
129 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
130 }
131 Ok(this)
132 }
133}
134
135impl crate::Message for DescribeConfigsRequest {
136 const API_KEY: i16 = 32;
137 const MIN_VERSION: i16 = 1;
138 const MAX_VERSION: i16 = 4;
139
140 fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
141 DescribeConfigsRequest::encode(self, buf, version)
142 }
143 fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
144 DescribeConfigsRequest::decode(buf, version)
145 }
146 fn decode_with_limits(
147 buf: &mut impl Buf,
148 version: i16,
149 limits: Limits,
150 ) -> Result<Self, DecodeError> {
151 DescribeConfigsRequest::decode_with_limits(buf, version, limits)
152 }
153}
154
155#[derive(Debug, Clone, PartialEq)]
157#[non_exhaustive]
158pub struct DescribeConfigsResource {
159 pub resource_type: i8,
161 pub resource_name: String,
163 pub configuration_keys: Option<Vec<String>>,
165 pub unknown_tagged_fields: Vec<RawTaggedField>,
167}
168
169impl Default for DescribeConfigsResource {
170 fn default() -> Self {
171 Self {
172 resource_type: 0,
173 resource_name: String::new(),
174 configuration_keys: Some(Vec::new()),
175 unknown_tagged_fields: Vec::new(),
176 }
177 }
178}
179
180impl DescribeConfigsResource {
181 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
183 buf.put_i8(self.resource_type);
184 if is_flexible(version) {
185 wire::put_compact_string(buf, &self.resource_name);
186 } else {
187 wire::put_string(buf, &self.resource_name)?;
188 }
189 match &self.configuration_keys {
190 Some(items) => {
191 if is_flexible(version) {
192 wire::put_compact_array_len(buf, Some(items.len()));
193 } else {
194 wire::put_array_len(buf, Some(items.len()))?;
195 }
196 for item in items {
197 if is_flexible(version) {
198 wire::put_compact_string(buf, item);
199 } else {
200 wire::put_string(buf, item)?;
201 }
202 }
203 }
204 None => {
205 if is_flexible(version) {
206 wire::put_compact_array_len(buf, None);
207 } else {
208 wire::put_array_len(buf, None)?;
209 }
210 }
211 }
212 if is_flexible(version) {
213 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
214 }
215 Ok(())
216 }
217
218 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
221 Self::decode_with_limits(buf, version, Limits::default())
222 }
223
224 pub fn decode_with_limits(
226 buf: &mut impl Buf,
227 version: i16,
228 limits: Limits,
229 ) -> Result<Self, DecodeError> {
230 let mut budget = limits.budget(buf.remaining());
231 Self::decode_with_budget(buf, version, &mut budget)
232 }
233
234 pub fn decode_with_budget(
236 buf: &mut impl Buf,
237 version: i16,
238 budget: &mut Budget,
239 ) -> Result<Self, DecodeError> {
240 let mut this = Self::default();
241 this.resource_type = wire::get_i8(buf)?;
242 this.resource_name = if is_flexible(version) {
243 wire::get_compact_string(buf)?
244 } else {
245 wire::get_string(buf)?
246 };
247 this.configuration_keys = {
248 let len = if is_flexible(version) {
249 wire::get_compact_array_len(buf)?
250 } else {
251 wire::get_array_len(buf)?
252 };
253 match len {
254 None => None,
255 Some(n) => {
256 let mut items = Vec::new();
257 for _ in 0..n {
258 budget.push(
259 &mut items,
260 if is_flexible(version) {
261 wire::get_compact_string(buf)?
262 } else {
263 wire::get_string(buf)?
264 },
265 )?;
266 }
267 Some(items)
268 }
269 }
270 };
271 if is_flexible(version) {
272 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
273 }
274 Ok(this)
275 }
276}