crabka_protocol/opt/rustwide/workdir/generated/
LeaveGroupRequest.owned.rs1use crate::primitives::string_bytes::{
4 compact_nullable_string_len, compact_string_len, get_compact_nullable_string_owned,
5 get_compact_string_owned, get_nullable_string_owned, get_string_owned, nullable_string_len,
6 put_compact_nullable_string, put_compact_string, put_nullable_string, put_string, string_len,
7};
8use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
9use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
10use bytes::{Buf, BufMut};
11pub const API_KEY: i16 = 13;
12pub const MIN_VERSION: i16 = 0;
13pub const MAX_VERSION: i16 = 5;
14pub const FLEXIBLE_MIN: i16 = 4;
15#[inline]
16fn is_flexible(version: i16) -> bool {
17 version >= FLEXIBLE_MIN
18}
19#[derive(Debug, Clone, PartialEq, Eq, Default)]
20pub struct LeaveGroupRequest {
21 pub group_id: String,
22 pub member_id: String,
23 pub members: Vec<MemberIdentity>,
24 pub unknown_tagged_fields: UnknownTaggedFields,
25}
26impl Encode for LeaveGroupRequest {
27 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
28 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
29 return Err(ProtocolError::UnsupportedVersion {
30 api_key: API_KEY,
31 version,
32 });
33 }
34 let flex = is_flexible(version);
35 if version >= 0 {
36 if flex {
37 put_compact_string(buf, &self.group_id);
38 } else {
39 put_string(buf, &self.group_id);
40 }
41 }
42 if (0..=2).contains(&version) {
43 if flex {
44 put_compact_string(buf, &self.member_id);
45 } else {
46 put_string(buf, &self.member_id);
47 }
48 }
49 if version >= 3 {
50 {
51 crate::primitives::array::put_array_len(buf, (self.members).len(), flex);
52 for it in &self.members {
53 it.encode(buf, version)?;
54 }
55 }
56 }
57 if flex {
58 let tagged = WriteTaggedFields::new();
59 tagged.write(buf, &self.unknown_tagged_fields);
60 }
61 Ok(())
62 }
63 fn encoded_len(&self, version: i16) -> usize {
64 let flex = is_flexible(version);
65 let mut n: usize = 0;
66 if version >= 0 {
67 n += if flex {
68 compact_string_len(&self.group_id)
69 } else {
70 string_len(&self.group_id)
71 };
72 }
73 if (0..=2).contains(&version) {
74 n += if flex {
75 compact_string_len(&self.member_id)
76 } else {
77 string_len(&self.member_id)
78 };
79 }
80 if version >= 3 {
81 n += {
82 let prefix =
83 crate::primitives::array::array_len_prefix_len((self.members).len(), flex);
84 let body: usize = (self.members)
85 .iter()
86 .map(|it| it.encoded_len(version))
87 .sum();
88 prefix + body
89 };
90 }
91 if flex {
92 let known_pairs: Vec<(u32, usize)> = Vec::new();
93 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
94 }
95 n
96 }
97}
98impl Decode<'_> for LeaveGroupRequest {
99 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
100 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
101 return Err(ProtocolError::UnsupportedVersion {
102 api_key: API_KEY,
103 version,
104 });
105 }
106 let flex = is_flexible(version);
107 let mut out = Self::default();
108 if version >= 0 {
109 out.group_id = if flex {
110 get_compact_string_owned(buf)?
111 } else {
112 get_string_owned(buf)?
113 };
114 }
115 if (0..=2).contains(&version) {
116 out.member_id = if flex {
117 get_compact_string_owned(buf)?
118 } else {
119 get_string_owned(buf)?
120 };
121 }
122 if version >= 3 {
123 out.members = {
124 let n = crate::primitives::array::get_array_len(buf, flex)?;
125 let mut v = Vec::with_capacity(n);
126 for _ in 0..n {
127 v.push(MemberIdentity::decode(buf, version)?);
128 }
129 v
130 };
131 }
132 if flex {
133 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
134 }
135 Ok(out)
136 }
137}
138#[cfg(test)]
139impl LeaveGroupRequest {
140 #[must_use]
141 pub fn populated(version: i16) -> Self {
142 let mut m = Self::default();
143 if version >= 0 {
144 m.group_id = "x".to_string();
145 }
146 if (0..=2).contains(&version) {
147 m.member_id = "x".to_string();
148 }
149 if version >= 3 {
150 m.members = vec![MemberIdentity::populated(version)];
151 }
152 m
153 }
154}
155#[derive(Debug, Clone, PartialEq, Eq, Default)]
156pub struct MemberIdentity {
157 pub member_id: String,
158 pub group_instance_id: Option<String>,
159 pub reason: Option<String>,
160 pub unknown_tagged_fields: UnknownTaggedFields,
161}
162impl Encode for MemberIdentity {
163 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
164 let flex = version >= 4;
165 if version >= 3 {
166 if flex {
167 put_compact_string(buf, &self.member_id);
168 } else {
169 put_string(buf, &self.member_id);
170 }
171 }
172 if version >= 3 {
173 if flex {
174 put_compact_nullable_string(buf, self.group_instance_id.as_deref());
175 } else {
176 put_nullable_string(buf, self.group_instance_id.as_deref());
177 }
178 }
179 if version >= 5 {
180 if flex {
181 put_compact_nullable_string(buf, self.reason.as_deref());
182 } else {
183 put_nullable_string(buf, self.reason.as_deref());
184 }
185 }
186 if flex {
187 let tagged = WriteTaggedFields::new();
188 tagged.write(buf, &self.unknown_tagged_fields);
189 }
190 Ok(())
191 }
192 fn encoded_len(&self, version: i16) -> usize {
193 let flex = version >= 4;
194 let mut n: usize = 0;
195 if version >= 3 {
196 n += if flex {
197 compact_string_len(&self.member_id)
198 } else {
199 string_len(&self.member_id)
200 };
201 }
202 if version >= 3 {
203 n += if flex {
204 compact_nullable_string_len(self.group_instance_id.as_deref())
205 } else {
206 nullable_string_len(self.group_instance_id.as_deref())
207 };
208 }
209 if version >= 5 {
210 n += if flex {
211 compact_nullable_string_len(self.reason.as_deref())
212 } else {
213 nullable_string_len(self.reason.as_deref())
214 };
215 }
216 if flex {
217 let known_pairs: Vec<(u32, usize)> = Vec::new();
218 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
219 }
220 n
221 }
222}
223impl Decode<'_> for MemberIdentity {
224 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
225 let flex = version >= 4;
226 let mut out = Self::default();
227 if version >= 3 {
228 out.member_id = if flex {
229 get_compact_string_owned(buf)?
230 } else {
231 get_string_owned(buf)?
232 };
233 }
234 if version >= 3 {
235 out.group_instance_id = if flex {
236 get_compact_nullable_string_owned(buf)?
237 } else {
238 get_nullable_string_owned(buf)?
239 };
240 }
241 if version >= 5 {
242 out.reason = if flex {
243 get_compact_nullable_string_owned(buf)?
244 } else {
245 get_nullable_string_owned(buf)?
246 };
247 }
248 if flex {
249 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
250 }
251 Ok(out)
252 }
253}
254#[cfg(test)]
255impl MemberIdentity {
256 #[must_use]
257 pub fn populated(version: i16) -> Self {
258 let mut m = Self::default();
259 if version >= 3 {
260 m.member_id = "x".to_string();
261 }
262 if version >= 3 {
263 m.group_instance_id = Some("x".to_string());
264 }
265 if version >= 5 {
266 m.reason = Some("x".to_string());
267 }
268 m
269 }
270}
271#[must_use]
274#[allow(unused_comparisons)]
275pub fn default_json(version: i16) -> ::serde_json::Value {
276 let mut obj = ::serde_json::Map::new();
277 obj.insert(
278 "groupId".to_string(),
279 ::serde_json::Value::String(String::new()),
280 );
281 if version <= 2 {
282 obj.insert(
283 "memberId".to_string(),
284 ::serde_json::Value::String(String::new()),
285 );
286 }
287 if version >= 3 {
288 obj.insert("members".to_string(), ::serde_json::Value::Array(vec![]));
289 }
290 ::serde_json::Value::Object(obj)
291}
292impl crate::ProtocolRequest for LeaveGroupRequest {
293 const API_KEY: i16 = API_KEY;
294 const MIN_VERSION: i16 = MIN_VERSION;
295 const MAX_VERSION: i16 = MAX_VERSION;
296 const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
297 type Response = super::leave_group_response::LeaveGroupResponse;
298}