Skip to main content

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

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