Skip to main content

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

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