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