Skip to main content

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

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use crate::primitives::string_bytes::{
4    compact_string_len, get_compact_string_owned, get_string_owned, put_compact_string, put_string,
5    string_len,
6};
7use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
8use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
9use bytes::{Buf, 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 {
23    pub client_software_name: String,
24    pub client_software_version: String,
25    pub unknown_tagged_fields: UnknownTaggedFields,
26}
27impl Encode for ApiVersionsRequest {
28    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
29        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
30            return Err(ProtocolError::UnsupportedVersion {
31                api_key: API_KEY,
32                version,
33            });
34        }
35        let flex = is_flexible(version);
36        if version >= 3 {
37            if flex {
38                put_compact_string(buf, &self.client_software_name);
39            } else {
40                put_string(buf, &self.client_software_name);
41            }
42        }
43        if version >= 3 {
44            if flex {
45                put_compact_string(buf, &self.client_software_version);
46            } else {
47                put_string(buf, &self.client_software_version);
48            }
49        }
50        if flex {
51            let tagged = WriteTaggedFields::new();
52            tagged.write(buf, &self.unknown_tagged_fields);
53        }
54        Ok(())
55    }
56    fn encoded_len(&self, version: i16) -> usize {
57        let flex = is_flexible(version);
58        let mut n: usize = 0;
59        if version >= 3 {
60            n += if flex {
61                compact_string_len(&self.client_software_name)
62            } else {
63                string_len(&self.client_software_name)
64            };
65        }
66        if version >= 3 {
67            n += if flex {
68                compact_string_len(&self.client_software_version)
69            } else {
70                string_len(&self.client_software_version)
71            };
72        }
73        if flex {
74            let known_pairs: Vec<(u32, usize)> = Vec::new();
75            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
76        }
77        n
78    }
79}
80impl Decode<'_> for ApiVersionsRequest {
81    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
82        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
83            return Err(ProtocolError::UnsupportedVersion {
84                api_key: API_KEY,
85                version,
86            });
87        }
88        let flex = is_flexible(version);
89        let mut out = Self::default();
90        if version >= 3 {
91            out.client_software_name = if flex {
92                get_compact_string_owned(buf)?
93            } else {
94                get_string_owned(buf)?
95            };
96        }
97        if version >= 3 {
98            out.client_software_version = if flex {
99                get_compact_string_owned(buf)?
100            } else {
101                get_string_owned(buf)?
102            };
103        }
104        if flex {
105            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
106        }
107        Ok(out)
108    }
109}
110#[cfg(test)]
111impl ApiVersionsRequest {
112    #[must_use]
113    pub fn populated(version: i16) -> Self {
114        let mut m = Self::default();
115        if version >= 3 {
116            m.client_software_name = "x".to_string();
117        }
118        if version >= 3 {
119            m.client_software_version = "x".to_string();
120        }
121        m
122    }
123}
124
125/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
126/// Only includes fields valid for the given version.
127#[must_use]
128#[allow(unused_comparisons)]
129pub fn default_json(version: i16) -> ::serde_json::Value {
130    let mut obj = ::serde_json::Map::new();
131    if version >= 3 {
132        obj.insert(
133            "clientSoftwareName".to_string(),
134            ::serde_json::Value::String(String::new()),
135        );
136    }
137    if version >= 3 {
138        obj.insert(
139            "clientSoftwareVersion".to_string(),
140            ::serde_json::Value::String(String::new()),
141        );
142    }
143    ::serde_json::Value::Object(obj)
144}
145
146impl crate::ProtocolRequest for ApiVersionsRequest {
147    const API_KEY: i16 = API_KEY;
148    const MIN_VERSION: i16 = MIN_VERSION;
149    const MAX_VERSION: i16 = MAX_VERSION;
150    const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
151    type Response = super::api_versions_response::ApiVersionsResponse;
152}