Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
LeaveGroupRequest.owned.rs

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