Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
RequestHeader.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, compact_string_len, nullable_string_len,
8    put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
9    string_len,
10};
11use crate::primitives::string_bytes_borrowed::{
12    get_compact_nullable_string_borrowed, get_compact_string_borrowed,
13    get_nullable_string_borrowed, get_string_borrowed,
14};
15use crate::tagged_fields::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
16use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
17pub const MIN_VERSION: i16 = 1;
18pub const MAX_VERSION: i16 = 2;
19pub const FLEXIBLE_MIN: i16 = 2;
20
21#[inline]
22fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
23
24#[derive(Debug, Clone, PartialEq, Eq)]
25pub struct RequestHeader<'a> {
26    pub request_api_key: i16,
27    pub request_api_version: i16,
28    pub correlation_id: i32,
29    pub client_id: Option<&'a str>,
30    pub unknown_tagged_fields: UnknownTaggedFields,
31}
32
33impl<'a> Default for RequestHeader<'a> {
34    fn default() -> Self {
35        Self {
36            request_api_key: 0i16,
37            request_api_version: 0i16,
38            correlation_id: 0i32,
39            client_id: None,
40            unknown_tagged_fields: Default::default(),
41        }
42    }
43}
44
45impl<'a> RequestHeader<'a> {
46    pub fn to_owned(&self) -> crate::owned::request_header::RequestHeader {
47        crate::owned::request_header::RequestHeader {
48            request_api_key: (self.request_api_key),
49            request_api_version: (self.request_api_version),
50            correlation_id: (self.correlation_id),
51            client_id: (self.client_id).map(|s| s.to_string()),
52            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
53        }
54    }
55}
56
57impl<'a> Encode for RequestHeader<'a> {
58    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
59        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
60            return Err(ProtocolError::SchemaMismatch("RequestHeader version out of range"));
61        }
62        let flex = is_flexible(version);
63        if version >= 0 { put_i16(buf, self.request_api_key) }
64        if version >= 0 { put_i16(buf, self.request_api_version) }
65        if version >= 0 { put_i32(buf, self.correlation_id) }
66        if version >= 1 { { let flex = false; if flex { put_compact_nullable_string(buf, self.client_id) } else { put_nullable_string(buf, self.client_id) } } }
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 { n += 2; }
77        if version >= 0 { n += 2; }
78        if version >= 0 { n += 4; }
79        if version >= 1 { n += { let flex = false; if flex { compact_nullable_string_len(self.client_id) } else { nullable_string_len(self.client_id) } }; }
80        if flex {
81            let known_pairs: Vec<(u32, usize)> = Vec::new();
82            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
83        }
84        n
85    }
86}
87
88impl<'de> DecodeBorrow<'de> for RequestHeader<'de> {
89    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
90        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
91            return Err(ProtocolError::SchemaMismatch("RequestHeader version out of range"));
92        }
93        let flex = is_flexible(version);
94        let mut out = Self::default();
95        if version >= 0 { out.request_api_key = get_i16(buf)?; }
96        if version >= 0 { out.request_api_version = get_i16(buf)?; }
97        if version >= 0 { out.correlation_id = get_i32(buf)?; }
98        if version >= 1 { out.client_id = { let flex = false; if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? } }; }
99        if flex {
100            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
101                Ok(false)
102            })?;
103        }
104        Ok(out)
105    }
106}