Skip to main content

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