Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
ConsumerProtocolSubscription.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_nullable_string_len, compact_string_len, nullable_string_len,
8    put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
9    string_len,
10};
11use crate::primitives::string_bytes_borrowed::{
12    get_compact_nullable_string_borrowed, get_compact_string_borrowed,
13    get_nullable_string_borrowed, get_string_borrowed,
14};
15use crate::primitives::string_bytes::{put_compact_nullable_bytes, put_nullable_bytes};
16use crate::primitives::string_bytes_borrowed::{get_compact_nullable_bytes_borrowed, get_nullable_bytes_borrowed};
17use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
18pub const MIN_VERSION: i16 = 0;
19pub const MAX_VERSION: i16 = 3;
20pub const FLEXIBLE_MIN: i16 = 32767;
21
22#[inline]
23fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
24
25#[derive(Debug, Clone, PartialEq, Eq)]
26pub struct ConsumerProtocolSubscription<'a> {
27    pub topics: Vec<&'a str>,
28    pub user_data: Option<&'a [u8]>,
29    pub owned_partitions: Vec<TopicPartition<'a>>,
30    pub generation_id: i32,
31    pub rack_id: Option<&'a str>,
32    pub unknown_tagged_fields: UnknownTaggedFields,
33}
34
35impl<'a> Default for ConsumerProtocolSubscription<'a> {
36    fn default() -> Self {
37        Self {
38            topics: Vec::new(),
39            user_data: None,
40            owned_partitions: Vec::new(),
41            generation_id: -1i32,
42            rack_id: None,
43            unknown_tagged_fields: Default::default(),
44        }
45    }
46}
47
48impl<'a> ConsumerProtocolSubscription<'a> {
49    pub fn to_owned(&self) -> crate::owned::consumer_protocol_subscription::ConsumerProtocolSubscription {
50        crate::owned::consumer_protocol_subscription::ConsumerProtocolSubscription {
51            topics: (self.topics).iter().map(|s| s.to_string()).collect(),
52            user_data: (self.user_data).map(Bytes::copy_from_slice),
53            owned_partitions: (self.owned_partitions).iter().map(|it| it.to_owned()).collect(),
54            generation_id: (self.generation_id),
55            rack_id: (self.rack_id).map(|s| s.to_string()),
56            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
57        }
58    }
59}
60
61impl<'a> Encode for ConsumerProtocolSubscription<'a> {
62    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
63        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
64            return Err(ProtocolError::SchemaMismatch("ConsumerProtocolSubscription version out of range"));
65        }
66        let flex = is_flexible(version);
67        if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.topics).len(), flex); for it in &self.topics { if flex { put_compact_string(buf, *it) } else { put_string(buf, *it) }; } } }
68        if version >= 0 { if flex { put_compact_nullable_bytes(buf, self.user_data) } else { put_nullable_bytes(buf, self.user_data) } }
69        if version >= 1 { { crate::primitives::array::put_array_len(buf, (self.owned_partitions).len(), flex); for it in &self.owned_partitions { it.encode(buf, version)?; } } }
70        if version >= 2 { put_i32(buf, self.generation_id) }
71        if version >= 3 { if flex { put_compact_nullable_string(buf, self.rack_id) } else { put_nullable_string(buf, self.rack_id) } }
72        Ok(())
73    }
74    fn encoded_len(&self, version: i16) -> usize {
75        let flex = is_flexible(version);
76        let mut n: usize = 0;
77        if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.topics).len(), flex); let body: usize = (self.topics).iter().map(|it| if flex { compact_string_len(*it) } else { string_len(*it) }).sum(); prefix + body }; }
78        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() } }; }
79        if version >= 1 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.owned_partitions).len(), flex); let body: usize = (self.owned_partitions).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
80        if version >= 2 { n += 4; }
81        if version >= 3 { n += if flex { compact_nullable_string_len(self.rack_id) } else { nullable_string_len(self.rack_id) }; }
82        n
83    }
84}
85
86impl<'de> DecodeBorrow<'de> for ConsumerProtocolSubscription<'de> {
87    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
88        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
89            return Err(ProtocolError::SchemaMismatch("ConsumerProtocolSubscription version out of range"));
90        }
91        let flex = is_flexible(version);
92        let mut out = Self::default();
93        if version >= 0 { out.topics = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }); } v }; }
94        if version >= 0 { out.user_data = if flex { get_compact_nullable_bytes_borrowed(buf)? } else { get_nullable_bytes_borrowed(buf)? }; }
95        if version >= 1 { out.owned_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 }; }
96        if version >= 2 { out.generation_id = get_i32(buf)?; }
97        if version >= 3 { out.rack_id = if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? }; }
98        Ok(out)
99    }
100}
101
102#[derive(Debug, Clone, PartialEq, Eq)]
103pub struct TopicPartition<'a> {
104    pub topic: &'a str,
105    pub partitions: Vec<i32>,
106    pub unknown_tagged_fields: UnknownTaggedFields,
107}
108
109impl<'a> Default for TopicPartition<'a> {
110    fn default() -> Self {
111        Self {
112            topic: "",
113            partitions: Vec::new(),
114            unknown_tagged_fields: Default::default(),
115        }
116    }
117}
118
119impl<'a> TopicPartition<'a> {
120    pub fn to_owned(&self) -> crate::owned::consumer_protocol_subscription::TopicPartition {
121        crate::owned::consumer_protocol_subscription::TopicPartition {
122            topic: (self.topic).to_string(),
123            partitions: (self.partitions).clone(),
124            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
125        }
126    }
127}
128
129impl<'a> Encode for TopicPartition<'a> {
130    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
131        let flex = version >= 32767;
132        if version >= 1 { if flex { put_compact_string(buf, self.topic) } else { put_string(buf, self.topic) } }
133        if version >= 1 { { crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex); for it in &self.partitions { put_i32(buf, *it); } } }
134        Ok(())
135    }
136    fn encoded_len(&self, version: i16) -> usize {
137        let flex = version >= 32767;
138        let mut n: usize = 0;
139        if version >= 1 { n += if flex { compact_string_len(self.topic) } else { string_len(self.topic) }; }
140        if version >= 1 { 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 }; }
141        n
142    }
143}
144
145impl<'de> DecodeBorrow<'de> for TopicPartition<'de> {
146    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
147        let flex = version >= 32767;
148        let mut out = Self::default();
149        if version >= 1 { out.topic = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
150        if version >= 1 { 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 }; }
151        Ok(out)
152    }
153}