Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
AddRaftVoterRequest.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_bool, get_i32, get_u16, put_bool, put_i32, put_u16};
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::tagged_fields::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
13use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
14
15pub const API_KEY: i16 = 80;
16pub const MIN_VERSION: i16 = 0;
17pub const MAX_VERSION: i16 = 1;
18pub const FLEXIBLE_MIN: i16 = 0;
19
20#[inline]
21fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
22
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub struct AddRaftVoterRequest {
25    pub cluster_id: Option<String>,
26    pub timeout_ms: i32,
27    pub voter_id: i32,
28    pub voter_directory_id: crate::primitives::uuid::Uuid,
29    pub listeners: Vec<Listener>,
30    pub ack_when_committed: bool,
31    pub unknown_tagged_fields: UnknownTaggedFields,
32}
33
34impl Default for AddRaftVoterRequest {
35    fn default() -> Self {
36        Self {
37            cluster_id: None,
38            timeout_ms: 0i32,
39            voter_id: 0i32,
40            voter_directory_id: Default::default(),
41            listeners: Vec::new(),
42            ack_when_committed: true,
43            unknown_tagged_fields: Default::default(),
44        }
45    }
46}
47
48impl Encode for AddRaftVoterRequest {
49    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
50        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
51            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
52        }
53        let flex = is_flexible(version);
54        if version >= 0 { if flex { put_compact_nullable_string(buf, self.cluster_id.as_deref()) } else { put_nullable_string(buf, self.cluster_id.as_deref()) } }
55        if version >= 0 { put_i32(buf, self.timeout_ms) }
56        if version >= 0 { put_i32(buf, self.voter_id) }
57        if version >= 0 { crate::primitives::uuid::put_uuid(buf, self.voter_directory_id) }
58        if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.listeners).len(), flex); for it in &self.listeners { it.encode(buf, version)?; } } }
59        if version >= 1 { put_bool(buf, self.ack_when_committed) }
60        if flex {
61            let tagged = WriteTaggedFields::new();
62            tagged.write(buf, &self.unknown_tagged_fields);
63        }
64        Ok(())
65    }
66    fn encoded_len(&self, version: i16) -> usize {
67        let flex = is_flexible(version);
68        let mut n: usize = 0;
69        if version >= 0 { n += if flex { compact_nullable_string_len(self.cluster_id.as_deref()) } else { nullable_string_len(self.cluster_id.as_deref()) }; }
70        if version >= 0 { n += 4; }
71        if version >= 0 { n += 4; }
72        if version >= 0 { n += 16; }
73        if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.listeners).len(), flex); let body: usize = (self.listeners).iter().map(|it| it.encoded_len(version)).sum(); prefix + body }; }
74        if version >= 1 { n += 1; }
75        if flex {
76            let known_pairs: Vec<(u32, usize)> = Vec::new();
77            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
78        }
79        n
80    }
81}
82
83impl<'de> Decode<'de> for AddRaftVoterRequest {
84    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
85        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
86            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
87        }
88        let flex = is_flexible(version);
89        let mut out = Self::default();
90        if version >= 0 { out.cluster_id = if flex { get_compact_nullable_string_owned(buf)? } else { get_nullable_string_owned(buf)? }; }
91        if version >= 0 { out.timeout_ms = get_i32(buf)?; }
92        if version >= 0 { out.voter_id = get_i32(buf)?; }
93        if version >= 0 { out.voter_directory_id = crate::primitives::uuid::get_uuid(buf)?; }
94        if version >= 0 { out.listeners = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(Listener::decode(buf, version)?); } v }; }
95        if version >= 1 { out.ack_when_committed = get_bool(buf)?; }
96        if flex {
97            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
98                Ok(false)
99            })?;
100        }
101        Ok(out)
102    }
103}
104
105#[derive(Debug, Clone, PartialEq, Eq, Default)]
106pub struct Listener {
107    pub name: String,
108    pub host: String,
109    pub port: u16,
110    pub unknown_tagged_fields: UnknownTaggedFields,
111}
112
113impl Encode for Listener {
114    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
115        let flex = version >= 0;
116        if version >= 0 { if flex { put_compact_string(buf, &self.name) } else { put_string(buf, &self.name) } }
117        if version >= 0 { if flex { put_compact_string(buf, &self.host) } else { put_string(buf, &self.host) } }
118        if version >= 0 { put_u16(buf, self.port) }
119        if flex {
120            let tagged = WriteTaggedFields::new();
121            tagged.write(buf, &self.unknown_tagged_fields);
122        }
123        Ok(())
124    }
125    fn encoded_len(&self, version: i16) -> usize {
126        let flex = version >= 0;
127        let mut n: usize = 0;
128        if version >= 0 { n += if flex { compact_string_len(&self.name) } else { string_len(&self.name) }; }
129        if version >= 0 { n += if flex { compact_string_len(&self.host) } else { string_len(&self.host) }; }
130        if version >= 0 { n += 2; }
131        if flex {
132            let known_pairs: Vec<(u32, usize)> = Vec::new();
133            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
134        }
135        n
136    }
137}
138
139impl<'de> Decode<'de> for Listener {
140    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
141        let flex = version >= 0;
142        let mut out = Self::default();
143        if version >= 0 { out.name = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
144        if version >= 0 { out.host = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
145        if version >= 0 { out.port = get_u16(buf)?; }
146        if flex {
147            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
148                Ok(false)
149            })?;
150        }
151        Ok(out)
152    }
153}
154
155/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
156/// Only includes fields valid for the given version.
157#[must_use]
158#[allow(unused_comparisons)]
159pub fn default_json(version: i16) -> ::serde_json::Value {
160    let mut obj = ::serde_json::Map::new();
161    obj.insert("clusterId".to_string(), ::serde_json::Value::Null);
162    obj.insert("timeoutMs".to_string(), ::serde_json::json!(0));
163    obj.insert("voterId".to_string(), ::serde_json::json!(0));
164    obj.insert("voterDirectoryId".to_string(), ::serde_json::Value::String("AAAAAAAAAAAAAAAAAAAAAA".to_string()));
165    obj.insert("listeners".to_string(), ::serde_json::Value::Array(vec![]));
166    if version >= 1 {
167        obj.insert("ackWhenCommitted".to_string(), ::serde_json::Value::Bool(true));
168    }
169    ::serde_json::Value::Object(obj)
170}
171
172impl crate::ProtocolRequest for AddRaftVoterRequest {
173    const API_KEY: i16 = API_KEY;
174    const MIN_VERSION: i16 = MIN_VERSION;
175    const MAX_VERSION: i16 = MAX_VERSION;
176    const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
177    type Response = super::add_raft_voter_response::AddRaftVoterResponse;
178}