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