odradek_protocol/messages/
leave_group_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 LeaveGroupRequest {
31 pub group_id: String,
33 pub member_id: String,
35 pub members: Vec<MemberIdentity>,
37 pub unknown_tagged_fields: Vec<RawTaggedField>,
39}
40
41impl Default for LeaveGroupRequest {
42 fn default() -> Self {
43 Self {
44 group_id: String::new(),
45 member_id: String::new(),
46 members: Vec::new(),
47 unknown_tagged_fields: Vec::new(),
48 }
49 }
50}
51
52impl LeaveGroupRequest {
53 pub const API_KEY: i16 = 13;
55 pub const MIN_VERSION: i16 = 0;
57 pub const MAX_VERSION: i16 = 5;
59
60 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
62 if is_flexible(version) {
63 wire::put_compact_string(buf, &self.group_id);
64 } else {
65 wire::put_string(buf, &self.group_id)?;
66 }
67 if version <= 2 {
68 if is_flexible(version) {
69 wire::put_compact_string(buf, &self.member_id);
70 } else {
71 wire::put_string(buf, &self.member_id)?;
72 }
73 }
74 if version >= 3 {
75 if is_flexible(version) {
76 wire::put_compact_array_len(buf, Some(self.members.len()));
77 } else {
78 wire::put_array_len(buf, Some(self.members.len()))?;
79 }
80 for item in &self.members {
81 item.encode(buf, version)?;
82 }
83 }
84 if is_flexible(version) {
85 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
86 }
87 Ok(())
88 }
89
90 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
93 Self::decode_with_limits(buf, version, Limits::default())
94 }
95
96 pub fn decode_with_limits(
98 buf: &mut impl Buf,
99 version: i16,
100 limits: Limits,
101 ) -> Result<Self, DecodeError> {
102 let mut budget = limits.budget(buf.remaining());
103 Self::decode_with_budget(buf, version, &mut budget)
104 }
105
106 pub fn decode_with_budget(
108 buf: &mut impl Buf,
109 version: i16,
110 budget: &mut Budget,
111 ) -> Result<Self, DecodeError> {
112 let mut this = Self::default();
113 this.group_id = if is_flexible(version) {
114 wire::get_compact_string(buf)?
115 } else {
116 wire::get_string(buf)?
117 };
118 if version <= 2 {
119 this.member_id = if is_flexible(version) {
120 wire::get_compact_string(buf)?
121 } else {
122 wire::get_string(buf)?
123 };
124 }
125 if version >= 3 {
126 this.members = {
127 let len = if is_flexible(version) {
128 wire::get_compact_array_len(buf)?
129 } else {
130 wire::get_array_len(buf)?
131 };
132 match len {
133 None => return Err(DecodeError::InvalidLength(-1)),
134 Some(n) => {
135 let mut items = Vec::new();
136 for _ in 0..n {
137 let mark = buf.remaining();
138 let item = MemberIdentity::decode_with_budget(buf, version, budget)?;
139 budget.progress(mark, buf.remaining())?;
140 budget.push(&mut items, item)?;
141 }
142 items
143 }
144 }
145 };
146 }
147 if is_flexible(version) {
148 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
149 }
150 Ok(this)
151 }
152}
153
154impl crate::Message for LeaveGroupRequest {
155 const API_KEY: i16 = 13;
156 const MIN_VERSION: i16 = 0;
157 const MAX_VERSION: i16 = 5;
158
159 fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
160 LeaveGroupRequest::encode(self, buf, version)
161 }
162 fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
163 LeaveGroupRequest::decode(buf, version)
164 }
165 fn decode_with_limits(
166 buf: &mut impl Buf,
167 version: i16,
168 limits: Limits,
169 ) -> Result<Self, DecodeError> {
170 LeaveGroupRequest::decode_with_limits(buf, version, limits)
171 }
172}
173
174#[derive(Debug, Clone, PartialEq)]
176#[non_exhaustive]
177pub struct MemberIdentity {
178 pub member_id: String,
180 pub group_instance_id: Option<String>,
182 pub reason: Option<String>,
184 pub unknown_tagged_fields: Vec<RawTaggedField>,
186}
187
188impl Default for MemberIdentity {
189 fn default() -> Self {
190 Self {
191 member_id: String::new(),
192 group_instance_id: None,
193 reason: None,
194 unknown_tagged_fields: Vec::new(),
195 }
196 }
197}
198
199impl MemberIdentity {
200 pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
202 if version >= 3 {
203 if is_flexible(version) {
204 wire::put_compact_string(buf, &self.member_id);
205 } else {
206 wire::put_string(buf, &self.member_id)?;
207 }
208 }
209 if version >= 3 {
210 if is_flexible(version) {
211 wire::put_compact_nullable_string(buf, self.group_instance_id.as_deref());
212 } else {
213 wire::put_nullable_string(buf, self.group_instance_id.as_deref())?;
214 }
215 }
216 if version >= 5 {
217 if is_flexible(version) {
218 wire::put_compact_nullable_string(buf, self.reason.as_deref());
219 } else {
220 wire::put_nullable_string(buf, self.reason.as_deref())?;
221 }
222 }
223 if is_flexible(version) {
224 wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
225 }
226 Ok(())
227 }
228
229 pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
232 Self::decode_with_limits(buf, version, Limits::default())
233 }
234
235 pub fn decode_with_limits(
237 buf: &mut impl Buf,
238 version: i16,
239 limits: Limits,
240 ) -> Result<Self, DecodeError> {
241 let mut budget = limits.budget(buf.remaining());
242 Self::decode_with_budget(buf, version, &mut budget)
243 }
244
245 pub fn decode_with_budget(
247 buf: &mut impl Buf,
248 version: i16,
249 budget: &mut Budget,
250 ) -> Result<Self, DecodeError> {
251 let mut this = Self::default();
252 if version >= 3 {
253 this.member_id = if is_flexible(version) {
254 wire::get_compact_string(buf)?
255 } else {
256 wire::get_string(buf)?
257 };
258 }
259 if version >= 3 {
260 this.group_instance_id = if is_flexible(version) {
261 wire::get_compact_nullable_string(buf)?
262 } else {
263 wire::get_nullable_string(buf)?
264 };
265 }
266 if version >= 5 {
267 this.reason = if is_flexible(version) {
268 wire::get_compact_nullable_string(buf)?
269 } else {
270 wire::get_nullable_string(buf)?
271 };
272 }
273 if is_flexible(version) {
274 this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
275 }
276 Ok(this)
277 }
278}