Skip to main content

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

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use bytes::{Buf, BufMut};
4
5use crate::primitives::fixed::{get_i32, put_i32};
6use crate::primitives::string_bytes::{
7    compact_nullable_bytes_len, get_compact_nullable_bytes_owned, get_nullable_bytes_owned,
8    nullable_bytes_len, put_compact_nullable_bytes, put_nullable_bytes,
9};
10use crate::primitives::string_bytes::{
11    compact_string_len, get_compact_string_owned, get_string_owned, put_compact_string, put_string,
12    string_len,
13};
14use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
15pub const MIN_VERSION: i16 = 0;
16pub const MAX_VERSION: i16 = 3;
17pub const FLEXIBLE_MIN: i16 = 32767;
18
19#[inline]
20fn is_flexible(version: i16) -> bool {
21    version >= FLEXIBLE_MIN
22}
23
24#[derive(Debug, Clone, PartialEq, Eq, Default)]
25pub struct ConsumerProtocolAssignment {
26    pub assigned_partitions: Vec<TopicPartition>,
27    pub user_data: Option<::bytes::Bytes>,
28    pub unknown_tagged_fields: UnknownTaggedFields,
29}
30impl Encode for ConsumerProtocolAssignment {
31    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
32        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
33            return Err(ProtocolError::SchemaMismatch(
34                "ConsumerProtocolAssignment version out of range",
35            ));
36        }
37        let flex = is_flexible(version);
38        if version >= 0 {
39            {
40                crate::primitives::array::put_array_len(
41                    buf,
42                    (self.assigned_partitions).len(),
43                    flex,
44                );
45                for it in &self.assigned_partitions {
46                    it.encode(buf, version)?;
47                }
48            }
49        }
50        if version >= 0 {
51            if flex {
52                put_compact_nullable_bytes(buf, self.user_data.as_deref());
53            } else {
54                put_nullable_bytes(buf, self.user_data.as_deref());
55            }
56        }
57        Ok(())
58    }
59    fn encoded_len(&self, version: i16) -> usize {
60        let flex = is_flexible(version);
61        let mut n: usize = 0;
62        if version >= 0 {
63            n += {
64                let prefix = crate::primitives::array::array_len_prefix_len(
65                    (self.assigned_partitions).len(),
66                    flex,
67                );
68                let body: usize = (self.assigned_partitions)
69                    .iter()
70                    .map(|it| it.encoded_len(version))
71                    .sum();
72                prefix + body
73            };
74        }
75        if version >= 0 {
76            n += if flex {
77                compact_nullable_bytes_len(self.user_data.as_deref())
78            } else {
79                nullable_bytes_len(self.user_data.as_deref())
80            };
81        }
82        n
83    }
84}
85impl Decode<'_> for ConsumerProtocolAssignment {
86    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
87        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
88            return Err(ProtocolError::SchemaMismatch(
89                "ConsumerProtocolAssignment version out of range",
90            ));
91        }
92        let flex = is_flexible(version);
93        let mut out = Self::default();
94        if version >= 0 {
95            out.assigned_partitions = {
96                let n = crate::primitives::array::get_array_len(buf, flex)?;
97                let mut v = Vec::with_capacity(n);
98                for _ in 0..n {
99                    v.push(TopicPartition::decode(buf, version)?);
100                }
101                v
102            };
103        }
104        if version >= 0 {
105            out.user_data = if flex {
106                get_compact_nullable_bytes_owned(buf)?
107            } else {
108                get_nullable_bytes_owned(buf)?
109            };
110        }
111        Ok(out)
112    }
113}
114#[cfg(test)]
115impl ConsumerProtocolAssignment {
116    #[must_use]
117    pub fn populated(version: i16) -> Self {
118        let mut m = Self::default();
119        if version >= 0 {
120            m.assigned_partitions = vec![TopicPartition::populated(version)];
121        }
122        if version >= 0 {
123            m.user_data = Some(::bytes::Bytes::from_static(b"x"));
124        }
125        m
126    }
127}
128#[derive(Debug, Clone, PartialEq, Eq, Default)]
129pub struct TopicPartition {
130    pub topic: String,
131    pub partitions: Vec<i32>,
132    pub unknown_tagged_fields: UnknownTaggedFields,
133}
134impl Encode for TopicPartition {
135    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
136        let flex = version >= 32767;
137        if version >= 0 {
138            if flex {
139                put_compact_string(buf, &self.topic);
140            } else {
141                put_string(buf, &self.topic);
142            }
143        }
144        if version >= 0 {
145            {
146                crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
147                for it in &self.partitions {
148                    put_i32(buf, *it);
149                }
150            }
151        }
152        Ok(())
153    }
154    fn encoded_len(&self, version: i16) -> usize {
155        let flex = version >= 32767;
156        let mut n: usize = 0;
157        if version >= 0 {
158            n += if flex {
159                compact_string_len(&self.topic)
160            } else {
161                string_len(&self.topic)
162            };
163        }
164        if version >= 0 {
165            n += {
166                let prefix =
167                    crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex);
168                let body: usize = (self.partitions).iter().map(|_| 4).sum();
169                prefix + body
170            };
171        }
172        n
173    }
174}
175impl Decode<'_> for TopicPartition {
176    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
177        let flex = version >= 32767;
178        let mut out = Self::default();
179        if version >= 0 {
180            out.topic = if flex {
181                get_compact_string_owned(buf)?
182            } else {
183                get_string_owned(buf)?
184            };
185        }
186        if version >= 0 {
187            out.partitions = {
188                let n = crate::primitives::array::get_array_len(buf, flex)?;
189                let mut v = Vec::with_capacity(n);
190                for _ in 0..n {
191                    v.push(get_i32(buf)?);
192                }
193                v
194            };
195        }
196        Ok(out)
197    }
198}
199#[cfg(test)]
200impl TopicPartition {
201    #[must_use]
202    pub fn populated(version: i16) -> Self {
203        let mut m = Self::default();
204        if version >= 0 {
205            m.topic = "x".to_string();
206        }
207        if version >= 0 {
208            m.partitions = vec![1i32];
209        }
210        m
211    }
212}
213
214/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
215/// Only includes fields valid for the given version.
216#[must_use]
217#[allow(unused_comparisons)]
218pub fn default_json(version: i16) -> ::serde_json::Value {
219    let mut obj = ::serde_json::Map::new();
220    obj.insert(
221        "assignedPartitions".to_string(),
222        ::serde_json::Value::Array(vec![]),
223    );
224    obj.insert("userData".to_string(), ::serde_json::Value::Null);
225    ::serde_json::Value::Object(obj)
226}