odradek_protocol/messages/
list_groups_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 >= 3
25}
26
27#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct ListGroupsRequest {
31 pub states_filter: Vec<String>,
33 pub types_filter: Vec<String>,
35 pub unknown_tagged_fields: Vec<RawTaggedField>,
37}
38
39impl Default for ListGroupsRequest {
40 fn default() -> Self {
41 Self {
42 states_filter: Vec::new(),
43 types_filter: Vec::new(),
44 unknown_tagged_fields: Vec::new(),
45 }
46 }
47}
48
49impl ListGroupsRequest {
50 pub const API_KEY: i16 = 16;
52 pub const MIN_VERSION: i16 = 0;
54 pub const MAX_VERSION: i16 = 5;
56
57 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
59 if version >= 4 {
60 if is_flexible(version) {
61 wire::put_compact_array_len(buf, Some(self.states_filter.len()));
62 } else {
63 wire::put_array_len(buf, Some(self.states_filter.len()))?;
64 }
65 for item in &self.states_filter {
66 if is_flexible(version) {
67 wire::put_compact_string(buf, item);
68 } else {
69 wire::put_string(buf, item)?;
70 }
71 }
72 }
73 if version >= 5 {
74 if is_flexible(version) {
75 wire::put_compact_array_len(buf, Some(self.types_filter.len()));
76 } else {
77 wire::put_array_len(buf, Some(self.types_filter.len()))?;
78 }
79 for item in &self.types_filter {
80 if is_flexible(version) {
81 wire::put_compact_string(buf, item);
82 } else {
83 wire::put_string(buf, item)?;
84 }
85 }
86 }
87 if is_flexible(version) {
88 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
89 }
90 Ok(())
91 }
92
93 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
96 Self::decode_with_limits(buf, version, Limits::default())
97 }
98
99 pub fn decode_with_limits(
101 buf: &mut impl Buf,
102 version: i16,
103 limits: Limits,
104 ) -> Result<Self, DecodeError> {
105 let mut budget = limits.budget(buf.remaining());
106 Self::decode_with_budget(buf, version, &mut budget)
107 }
108
109 pub fn decode_with_budget(
111 buf: &mut impl Buf,
112 version: i16,
113 budget: &mut Budget,
114 ) -> Result<Self, DecodeError> {
115 let mut this = Self::default();
116 if version >= 4 {
117 this.states_filter = {
118 let len = if is_flexible(version) {
119 wire::get_compact_array_len(buf)?
120 } else {
121 wire::get_array_len(buf)?
122 };
123 match len {
124 None => return Err(DecodeError::InvalidLength(-1)),
125 Some(n) => {
126 let mut items = Vec::new();
127 for _ in 0..n {
128 budget.push(
129 &mut items,
130 if is_flexible(version) {
131 wire::get_compact_string(buf)?
132 } else {
133 wire::get_string(buf)?
134 },
135 )?;
136 }
137 items
138 }
139 }
140 };
141 }
142 if version >= 5 {
143 this.types_filter = {
144 let len = if is_flexible(version) {
145 wire::get_compact_array_len(buf)?
146 } else {
147 wire::get_array_len(buf)?
148 };
149 match len {
150 None => return Err(DecodeError::InvalidLength(-1)),
151 Some(n) => {
152 let mut items = Vec::new();
153 for _ in 0..n {
154 budget.push(
155 &mut items,
156 if is_flexible(version) {
157 wire::get_compact_string(buf)?
158 } else {
159 wire::get_string(buf)?
160 },
161 )?;
162 }
163 items
164 }
165 }
166 };
167 }
168 if is_flexible(version) {
169 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
170 }
171 Ok(this)
172 }
173}
174
175impl crate::Message for ListGroupsRequest {
176 const API_KEY: i16 = 16;
177 const MIN_VERSION: i16 = 0;
178 const MAX_VERSION: i16 = 5;
179
180 fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
181 ListGroupsRequest::encode(self, buf, version)
182 }
183 fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
184 ListGroupsRequest::decode(buf, version)
185 }
186 fn decode_with_limits(
187 buf: &mut impl Buf,
188 version: i16,
189 limits: Limits,
190 ) -> Result<Self, DecodeError> {
191 ListGroupsRequest::decode_with_limits(buf, version, limits)
192 }
193}