Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
ApiVersionsRequest.borrowed.rs

1// AUTO-GENERATED by krabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2use bytes::BufMut;
3
4use crate::{
5    DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields,
6    primitives::{
7        fixed::{get_i32, put_i32},
8        string_bytes::{
9            compact_nullable_string_len, compact_string_len, nullable_string_len,
10            put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
11            string_len,
12        },
13        string_bytes_borrowed::{
14            get_compact_nullable_string_borrowed, get_compact_string_borrowed,
15            get_nullable_string_borrowed, get_string_borrowed,
16        },
17    },
18    tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
19};
20pub const API_KEY: i16 = 18;
21pub const MIN_VERSION: i16 = 0;
22pub const MAX_VERSION: i16 = 5;
23pub const LATEST_STABLE_VERSION: i16 = 5;
24pub const FLEXIBLE_MIN: i16 = 3;
25#[inline]
26#[must_use]
27pub fn is_flexible(version: i16) -> bool {
28    version >= FLEXIBLE_MIN
29}
30#[derive(Debug, Clone, PartialEq, Eq)]
31pub struct ApiVersionsRequest<'a> {
32    pub client_software_name: &'a str,
33    pub client_software_version: &'a str,
34    pub cluster_id: Option<&'a str>,
35    pub node_id: i32,
36    pub unknown_tagged_fields: UnknownTaggedFields,
37}
38impl Default for ApiVersionsRequest<'_> {
39    fn default() -> Self {
40        Self {
41            client_software_name: "",
42            client_software_version: "",
43            cluster_id: None,
44            node_id: -1i32,
45            unknown_tagged_fields: UnknownTaggedFields::default(),
46        }
47    }
48}
49impl ApiVersionsRequest<'_> {
50    /// # Panics
51    ///
52    /// Panics if a records field contains an invalid encoded record batch.
53    #[must_use]
54    pub fn to_owned(&self) -> crate::owned::api_versions_request::ApiVersionsRequest {
55        crate::owned::api_versions_request::ApiVersionsRequest {
56            client_software_name: (self.client_software_name).to_string(),
57            client_software_version: (self.client_software_version).to_string(),
58            cluster_id: (self.cluster_id).map(std::string::ToString::to_string),
59            node_id: (self.node_id),
60            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
61        }
62    }
63}
64impl Encode for ApiVersionsRequest<'_> {
65    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
66        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
67            return Err(ProtocolError::UnsupportedVersion {
68                api_key: API_KEY,
69                version,
70            });
71        }
72        let flex = is_flexible(version);
73        if version >= 3 {
74            if flex {
75                let () = put_compact_string(buf, self.client_software_name);
76            } else {
77                let () = put_string(buf, self.client_software_name);
78            }
79        }
80        if version >= 3 {
81            if flex {
82                let () = put_compact_string(buf, self.client_software_version);
83            } else {
84                let () = put_string(buf, self.client_software_version);
85            }
86        }
87        if version >= 5 {
88            if flex {
89                let () = put_compact_nullable_string(buf, self.cluster_id);
90            } else {
91                let () = put_nullable_string(buf, self.cluster_id);
92            }
93        }
94        if version >= 5 {
95            put_i32(buf, self.node_id);
96        }
97        if flex {
98            let tagged = WriteTaggedFields::new();
99            tagged.write(buf, &self.unknown_tagged_fields);
100        }
101        Ok(())
102    }
103    fn encoded_len(&self, version: i16) -> usize {
104        let flex = is_flexible(version);
105        let mut n: usize = 0;
106        if version >= 3 {
107            n += if flex {
108                compact_string_len(self.client_software_name)
109            } else {
110                string_len(self.client_software_name)
111            };
112        }
113        if version >= 3 {
114            n += if flex {
115                compact_string_len(self.client_software_version)
116            } else {
117                string_len(self.client_software_version)
118            };
119        }
120        if version >= 5 {
121            n += if flex {
122                compact_nullable_string_len(self.cluster_id)
123            } else {
124                nullable_string_len(self.cluster_id)
125            };
126        }
127        if version >= 5 {
128            n += 4;
129        }
130        if flex {
131            let known_pairs: Vec<(u32, usize)> = Vec::new();
132            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
133        }
134        n
135    }
136}
137impl<'de> DecodeBorrow<'de> for ApiVersionsRequest<'de> {
138    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
139        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
140            return Err(ProtocolError::UnsupportedVersion {
141                api_key: API_KEY,
142                version,
143            });
144        }
145        let flex = is_flexible(version);
146        let mut out = Self::default();
147        if version >= 3 {
148            out.client_software_name = if flex {
149                get_compact_string_borrowed(buf)?
150            } else {
151                get_string_borrowed(buf)?
152            };
153        }
154        if version >= 3 {
155            out.client_software_version = if flex {
156                get_compact_string_borrowed(buf)?
157            } else {
158                get_string_borrowed(buf)?
159            };
160        }
161        if version >= 5 {
162            out.cluster_id = if flex {
163                get_compact_nullable_string_borrowed(buf)?
164            } else {
165                get_nullable_string_borrowed(buf)?
166            };
167        }
168        if version >= 5 {
169            out.node_id = get_i32(buf)?;
170        }
171        if flex {
172            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
173        }
174        Ok(out)
175    }
176}
177#[cfg(test)]
178impl ApiVersionsRequest<'_> {
179    #[must_use]
180    pub fn populated(version: i16) -> Self {
181        let mut m = Self::default();
182        if version >= 3 {
183            m.client_software_name = "x";
184        }
185        if version >= 3 {
186            m.client_software_version = "x";
187        }
188        if version >= 5 {
189            m.cluster_id = Some("x");
190        }
191        if version >= 5 {
192            m.node_id = 1i32;
193        }
194        m
195    }
196}