Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
ConsumerProtocolSubscription.owned.rs

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use bytes::{Buf, BufMut};
4
5use crate::primitives::fixed::{get_i32, put_i32};
6use crate::primitives::string_bytes::{
7    compact_nullable_string_len, compact_string_len, get_compact_nullable_string_owned,
8    get_compact_string_owned, get_nullable_string_owned, get_string_owned, nullable_string_len,
9    put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
10    string_len,
11};
12use crate::primitives::string_bytes::{compact_nullable_bytes_len, get_compact_nullable_bytes_owned, get_nullable_bytes_owned, nullable_bytes_len, put_compact_nullable_bytes, put_nullable_bytes};
13use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
14pub const MIN_VERSION: i16 = 0;
15pub const MAX_VERSION: i16 = 3;
16pub const FLEXIBLE_MIN: i16 = 32767;
17
18#[inline]
19fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
20
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub struct ConsumerProtocolSubscription {
23    pub topics: Vec<String>,
24    pub user_data: Option<::bytes::Bytes>,
25    pub owned_partitions: Vec<TopicPartition>,
26    pub generation_id: i32,
27    pub rack_id: Option<String>,
28    pub unknown_tagged_fields: UnknownTaggedFields,
29}
30
31impl Default for ConsumerProtocolSubscription {
32    fn default() -> Self {
33        Self {
34            topics: Vec::new(),
35            user_data: None,
36            owned_partitions: Vec::new(),
37            generation_id: -1i32,
38            rack_id: None,
39            unknown_tagged_fields: Default::default(),
40        }
41    }
42}
43
44impl Encode for ConsumerProtocolSubscription {
45    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
46        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
47            return Err(ProtocolError::SchemaMismatch("ConsumerProtocolSubscription version out of range"));
48        }
49        let flex = is_flexible(version);
50        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) }; } } }
51        if version >= 0 { if flex { put_compact_nullable_bytes(buf, self.user_data.as_deref()) } else { put_nullable_bytes(buf, self.user_data.as_deref()) } }
52        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)?; } } }
53        if version >= 2 { put_i32(buf, self.generation_id) }
54        if version >= 3 { if flex { put_compact_nullable_string(buf, self.rack_id.as_deref()) } else { put_nullable_string(buf, self.rack_id.as_deref()) } }
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 { 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 }; }
61        if version >= 0 { n += if flex { compact_nullable_bytes_len(self.user_data.as_deref()) } else { nullable_bytes_len(self.user_data.as_deref()) }; }
62        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 }; }
63        if version >= 2 { n += 4; }
64        if version >= 3 { n += if flex { compact_nullable_string_len(self.rack_id.as_deref()) } else { nullable_string_len(self.rack_id.as_deref()) }; }
65        n
66    }
67}
68
69impl<'de> Decode<'de> for ConsumerProtocolSubscription {
70    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
71        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
72            return Err(ProtocolError::SchemaMismatch("ConsumerProtocolSubscription version out of range"));
73        }
74        let flex = is_flexible(version);
75        let mut out = Self::default();
76        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_owned(buf)? } else { get_string_owned(buf)? }); } v }; }
77        if version >= 0 { out.user_data = if flex { get_compact_nullable_bytes_owned(buf)? } else { get_nullable_bytes_owned(buf)? }; }
78        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(buf, version)?); } v }; }
79        if version >= 2 { out.generation_id = get_i32(buf)?; }
80        if version >= 3 { out.rack_id = if flex { get_compact_nullable_string_owned(buf)? } else { get_nullable_string_owned(buf)? }; }
81        Ok(out)
82    }
83}
84
85#[derive(Debug, Clone, PartialEq, Eq, Default)]
86pub struct TopicPartition {
87    pub topic: String,
88    pub partitions: Vec<i32>,
89    pub unknown_tagged_fields: UnknownTaggedFields,
90}
91
92impl Encode for TopicPartition {
93    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
94        let flex = version >= 32767;
95        if version >= 1 { if flex { put_compact_string(buf, &self.topic) } else { put_string(buf, &self.topic) } }
96        if version >= 1 { { crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex); for it in &self.partitions { put_i32(buf, *it); } } }
97        Ok(())
98    }
99    fn encoded_len(&self, version: i16) -> usize {
100        let flex = version >= 32767;
101        let mut n: usize = 0;
102        if version >= 1 { n += if flex { compact_string_len(&self.topic) } else { string_len(&self.topic) }; }
103        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 }; }
104        n
105    }
106}
107
108impl<'de> Decode<'de> for TopicPartition {
109    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
110        let flex = version >= 32767;
111        let mut out = Self::default();
112        if version >= 1 { out.topic = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
113        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 }; }
114        Ok(out)
115    }
116}
117
118/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
119/// Only includes fields valid for the given version.
120#[must_use]
121#[allow(unused_comparisons)]
122pub fn default_json(version: i16) -> ::serde_json::Value {
123    let mut obj = ::serde_json::Map::new();
124    obj.insert("topics".to_string(), ::serde_json::Value::Array(vec![]));
125    obj.insert("userData".to_string(), ::serde_json::Value::Null);
126    if version >= 1 {
127        obj.insert("ownedPartitions".to_string(), ::serde_json::Value::Array(vec![]));
128    }
129    if version >= 2 {
130        obj.insert("generationId".to_string(), ::serde_json::json!(-1));
131    }
132    if version >= 3 {
133        obj.insert("rackId".to_string(), ::serde_json::Value::Null);
134    }
135    ::serde_json::Value::Object(obj)
136}