Skip to main content

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

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use bytes::{Bytes, BufMut};
4
5use crate::primitives::fixed::{get_i32, put_i32};
6use crate::primitives::string_bytes::{
7    compact_string_len, put_compact_string, put_string, string_len,
8};
9use crate::primitives::string_bytes_borrowed::{
10    get_compact_string_borrowed, get_string_borrowed,
11};
12use crate::primitives::string_bytes::{put_compact_nullable_bytes, put_nullable_bytes};
13use crate::primitives::string_bytes_borrowed::{get_compact_nullable_bytes_borrowed, get_nullable_bytes_borrowed};
14use crate::{DecodeBorrow, 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 { version >= FLEXIBLE_MIN }
21
22#[derive(Debug, Clone, PartialEq, Eq)]
23pub struct ConsumerProtocolAssignment<'a> {
24    pub assigned_partitions: Vec<TopicPartition<'a>>,
25    pub user_data: Option<&'a [u8]>,
26    pub unknown_tagged_fields: UnknownTaggedFields,
27}
28
29impl<'a> Default for ConsumerProtocolAssignment<'a> {
30    fn default() -> Self {
31        Self {
32            assigned_partitions: Vec::new(),
33            user_data: None,
34            unknown_tagged_fields: Default::default(),
35        }
36    }
37}
38
39impl<'a> ConsumerProtocolAssignment<'a> {
40    pub fn to_owned(&self) -> crate::owned::consumer_protocol_assignment::ConsumerProtocolAssignment {
41        crate::owned::consumer_protocol_assignment::ConsumerProtocolAssignment {
42            assigned_partitions: (self.assigned_partitions).iter().map(|it| it.to_owned()).collect(),
43            user_data: (self.user_data).map(Bytes::copy_from_slice),
44            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
45        }
46    }
47}
48
49impl<'a> Encode for ConsumerProtocolAssignment<'a> {
50    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
51        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
52            return Err(ProtocolError::SchemaMismatch("ConsumerProtocolAssignment version out of range"));
53        }
54        let flex = is_flexible(version);
55        if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.assigned_partitions).len(), flex); for it in &self.assigned_partitions { it.encode(buf, version)?; } } }
56        if version >= 0 { if flex { put_compact_nullable_bytes(buf, self.user_data) } else { put_nullable_bytes(buf, self.user_data) } }
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 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.assigned_partitions).len(), flex); let body: usize = (self.assigned_partitions).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
63        if version >= 0 { n += match self.user_data { None => if flex { 1 } else { 4 }, Some(b) => if flex { crate::primitives::varint::uvarint_len(u32::try_from(b.len() + 1).unwrap()) + b.len() } else { 4 + b.len() } }; }
64        n
65    }
66}
67
68impl<'de> DecodeBorrow<'de> for ConsumerProtocolAssignment<'de> {
69    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
70        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
71            return Err(ProtocolError::SchemaMismatch("ConsumerProtocolAssignment version out of range"));
72        }
73        let flex = is_flexible(version);
74        let mut out = Self::default();
75        if version >= 0 { out.assigned_partitions = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(TopicPartition::decode_borrow(buf, version)?); } v }; }
76        if version >= 0 { out.user_data = if flex { get_compact_nullable_bytes_borrowed(buf)? } else { get_nullable_bytes_borrowed(buf)? }; }
77        Ok(out)
78    }
79}
80
81#[derive(Debug, Clone, PartialEq, Eq)]
82pub struct TopicPartition<'a> {
83    pub topic: &'a str,
84    pub partitions: Vec<i32>,
85    pub unknown_tagged_fields: UnknownTaggedFields,
86}
87
88impl<'a> Default for TopicPartition<'a> {
89    fn default() -> Self {
90        Self {
91            topic: "",
92            partitions: Vec::new(),
93            unknown_tagged_fields: Default::default(),
94        }
95    }
96}
97
98impl<'a> TopicPartition<'a> {
99    pub fn to_owned(&self) -> crate::owned::consumer_protocol_assignment::TopicPartition {
100        crate::owned::consumer_protocol_assignment::TopicPartition {
101            topic: (self.topic).to_string(),
102            partitions: (self.partitions).clone(),
103            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
104        }
105    }
106}
107
108impl<'a> Encode for TopicPartition<'a> {
109    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
110        let flex = version >= 32767;
111        if version >= 0 { if flex { put_compact_string(buf, self.topic) } else { put_string(buf, self.topic) } }
112        if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex); for it in &self.partitions { put_i32(buf, *it); } } }
113        Ok(())
114    }
115    fn encoded_len(&self, version: i16) -> usize {
116        let flex = version >= 32767;
117        let mut n: usize = 0;
118        if version >= 0 { n += if flex { compact_string_len(self.topic) } else { string_len(self.topic) }; }
119        if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex); let body: usize = (self.partitions).iter().map(|_| 4).sum(); prefix + body }; }
120        n
121    }
122}
123
124impl<'de> DecodeBorrow<'de> for TopicPartition<'de> {
125    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
126        let flex = version >= 32767;
127        let mut out = Self::default();
128        if version >= 0 { out.topic = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
129        if version >= 0 { out.partitions = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(get_i32(buf)?); } v }; }
130        Ok(out)
131    }
132}