Skip to main content

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

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use crate::primitives::string_bytes::{
4    compact_string_len, put_compact_string, put_string, string_len,
5};
6use crate::primitives::string_bytes_borrowed::{get_compact_string_borrowed, get_string_borrowed};
7use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
8use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
9use bytes::BufMut;
10
11pub const API_KEY: i16 = 18;
12pub const MIN_VERSION: i16 = 0;
13pub const MAX_VERSION: i16 = 4;
14pub const FLEXIBLE_MIN: i16 = 3;
15
16#[inline]
17fn is_flexible(version: i16) -> bool {
18    version >= FLEXIBLE_MIN
19}
20
21#[derive(Debug, Clone, PartialEq, Eq, Default)]
22pub struct ApiVersionsRequest<'a> {
23    pub client_software_name: &'a str,
24    pub client_software_version: &'a str,
25    pub unknown_tagged_fields: UnknownTaggedFields,
26}
27impl ApiVersionsRequest<'_> {
28    pub fn to_owned(&self) -> crate::owned::api_versions_request::ApiVersionsRequest {
29        crate::owned::api_versions_request::ApiVersionsRequest {
30            client_software_name: (self.client_software_name).to_string(),
31            client_software_version: (self.client_software_version).to_string(),
32            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
33        }
34    }
35}
36impl Encode for ApiVersionsRequest<'_> {
37    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
38        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
39            return Err(ProtocolError::UnsupportedVersion {
40                api_key: API_KEY,
41                version,
42            });
43        }
44        let flex = is_flexible(version);
45        if version >= 3 {
46            if flex {
47                put_compact_string(buf, self.client_software_name);
48            } else {
49                put_string(buf, self.client_software_name);
50            }
51        }
52        if version >= 3 {
53            if flex {
54                put_compact_string(buf, self.client_software_version);
55            } else {
56                put_string(buf, self.client_software_version);
57            }
58        }
59        if flex {
60            let tagged = WriteTaggedFields::new();
61            tagged.write(buf, &self.unknown_tagged_fields);
62        }
63        Ok(())
64    }
65    fn encoded_len(&self, version: i16) -> usize {
66        let flex = is_flexible(version);
67        let mut n: usize = 0;
68        if version >= 3 {
69            n += if flex {
70                compact_string_len(self.client_software_name)
71            } else {
72                string_len(self.client_software_name)
73            };
74        }
75        if version >= 3 {
76            n += if flex {
77                compact_string_len(self.client_software_version)
78            } else {
79                string_len(self.client_software_version)
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 ApiVersionsRequest<'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 >= 3 {
100            out.client_software_name = if flex {
101                get_compact_string_borrowed(buf)?
102            } else {
103                get_string_borrowed(buf)?
104            };
105        }
106        if version >= 3 {
107            out.client_software_version = if flex {
108                get_compact_string_borrowed(buf)?
109            } else {
110                get_string_borrowed(buf)?
111            };
112        }
113        if flex {
114            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
115        }
116        Ok(out)
117    }
118}
119#[cfg(test)]
120impl ApiVersionsRequest<'_> {
121    #[must_use]
122    pub fn populated(version: i16) -> Self {
123        let mut m = Self::default();
124        if version >= 3 {
125            m.client_software_name = "x";
126        }
127        if version >= 3 {
128            m.client_software_version = "x";
129        }
130        m
131    }
132}