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 bytes::{Buf, BufMut};
4use crate::primitives::string_bytes::{
5    compact_string_len, get_compact_string_owned, get_string_owned,
6    put_compact_string, put_string, string_len,
7};
8use crate::tagged_fields::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
9use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
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 { version >= FLEXIBLE_MIN }
18
19#[derive(Debug, Clone, PartialEq, Eq, Default)]
20pub struct ApiVersionsRequest {
21    pub client_software_name: String,
22    pub client_software_version: String,
23    pub unknown_tagged_fields: UnknownTaggedFields,
24}
25
26impl Encode for ApiVersionsRequest {
27    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
28        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
29            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
30        }
31        let flex = is_flexible(version);
32        if version >= 3 { if flex { put_compact_string(buf, &self.client_software_name) } else { put_string(buf, &self.client_software_name) } }
33        if version >= 3 { if flex { put_compact_string(buf, &self.client_software_version) } else { put_string(buf, &self.client_software_version) } }
34        if flex {
35            let tagged = WriteTaggedFields::new();
36            tagged.write(buf, &self.unknown_tagged_fields);
37        }
38        Ok(())
39    }
40    fn encoded_len(&self, version: i16) -> usize {
41        let flex = is_flexible(version);
42        let mut n: usize = 0;
43        if version >= 3 { n += if flex { compact_string_len(&self.client_software_name) } else { string_len(&self.client_software_name) }; }
44        if version >= 3 { n += if flex { compact_string_len(&self.client_software_version) } else { string_len(&self.client_software_version) }; }
45        if flex {
46            let known_pairs: Vec<(u32, usize)> = Vec::new();
47            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
48        }
49        n
50    }
51}
52
53impl<'de> Decode<'de> for ApiVersionsRequest {
54    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
55        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
56            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
57        }
58        let flex = is_flexible(version);
59        let mut out = Self::default();
60        if version >= 3 { out.client_software_name = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
61        if version >= 3 { out.client_software_version = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
62        if flex {
63            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
64                Ok(false)
65            })?;
66        }
67        Ok(out)
68    }
69}
70
71/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
72/// Only includes fields valid for the given version.
73#[must_use]
74#[allow(unused_comparisons)]
75pub fn default_json(version: i16) -> ::serde_json::Value {
76    let mut obj = ::serde_json::Map::new();
77    if version >= 3 {
78        obj.insert("clientSoftwareName".to_string(), ::serde_json::Value::String(String::new()));
79    }
80    if version >= 3 {
81        obj.insert("clientSoftwareVersion".to_string(), ::serde_json::Value::String(String::new()));
82    }
83    ::serde_json::Value::Object(obj)
84}
85
86impl crate::ProtocolRequest for ApiVersionsRequest {
87    const API_KEY: i16 = API_KEY;
88    const MIN_VERSION: i16 = MIN_VERSION;
89    const MAX_VERSION: i16 = MAX_VERSION;
90    const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
91    type Response = super::api_versions_response::ApiVersionsResponse;
92}