Skip to main content

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

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2use crate::primitives::fixed::{get_i16, get_i32, put_i16, put_i32};
3use crate::primitives::string_bytes::{
4    compact_nullable_string_len, nullable_string_len, put_compact_nullable_string,
5    put_nullable_string,
6};
7use crate::primitives::string_bytes_borrowed::{
8    get_compact_nullable_string_borrowed, get_nullable_string_borrowed,
9};
10use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
11use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
12use bytes::BufMut;
13pub const API_KEY: i16 = 80;
14pub const MIN_VERSION: i16 = 0;
15pub const MAX_VERSION: i16 = 1;
16pub const FLEXIBLE_MIN: i16 = 0;
17#[inline]
18fn is_flexible(version: i16) -> bool {
19    version >= FLEXIBLE_MIN
20}
21#[derive(Debug, Clone, PartialEq, Eq, Default)]
22pub struct AddRaftVoterResponse<'a> {
23    pub throttle_time_ms: i32,
24    pub error_code: i16,
25    pub error_message: Option<&'a str>,
26    pub unknown_tagged_fields: UnknownTaggedFields,
27}
28impl AddRaftVoterResponse<'_> {
29    pub fn to_owned(&self) -> crate::owned::add_raft_voter_response::AddRaftVoterResponse {
30        crate::owned::add_raft_voter_response::AddRaftVoterResponse {
31            throttle_time_ms: (self.throttle_time_ms),
32            error_code: (self.error_code),
33            error_message: (self.error_message).map(std::string::ToString::to_string),
34            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
35        }
36    }
37}
38impl Encode for AddRaftVoterResponse<'_> {
39    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
40        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
41            return Err(ProtocolError::UnsupportedVersion {
42                api_key: API_KEY,
43                version,
44            });
45        }
46        let flex = is_flexible(version);
47        if version >= 0 {
48            put_i32(buf, self.throttle_time_ms);
49        }
50        if version >= 0 {
51            put_i16(buf, self.error_code);
52        }
53        if version >= 0 {
54            if flex {
55                put_compact_nullable_string(buf, self.error_message);
56            } else {
57                put_nullable_string(buf, self.error_message);
58            }
59        }
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 {
70            n += 4;
71        }
72        if version >= 0 {
73            n += 2;
74        }
75        if version >= 0 {
76            n += if flex {
77                compact_nullable_string_len(self.error_message)
78            } else {
79                nullable_string_len(self.error_message)
80            };
81        }
82        if flex {
83            let known_pairs: Vec<(u32, usize)> = Vec::new();
84            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
85        }
86        n
87    }
88}
89impl<'de> DecodeBorrow<'de> for AddRaftVoterResponse<'de> {
90    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
91        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
92            return Err(ProtocolError::UnsupportedVersion {
93                api_key: API_KEY,
94                version,
95            });
96        }
97        let flex = is_flexible(version);
98        let mut out = Self::default();
99        if version >= 0 {
100            out.throttle_time_ms = get_i32(buf)?;
101        }
102        if version >= 0 {
103            out.error_code = get_i16(buf)?;
104        }
105        if version >= 0 {
106            out.error_message = if flex {
107                get_compact_nullable_string_borrowed(buf)?
108            } else {
109                get_nullable_string_borrowed(buf)?
110            };
111        }
112        if flex {
113            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
114        }
115        Ok(out)
116    }
117}
118#[cfg(test)]
119impl AddRaftVoterResponse<'_> {
120    #[must_use]
121    pub fn populated(version: i16) -> Self {
122        let mut m = Self::default();
123        if version >= 0 {
124            m.throttle_time_ms = 1i32;
125        }
126        if version >= 0 {
127            m.error_code = 1i16;
128        }
129        if version >= 0 {
130            m.error_message = Some("x");
131        }
132        m
133    }
134}