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, nullable_string_len, put_compact_nullable_string,
8    put_nullable_string,
9};
10use crate::primitives::string_bytes_borrowed::{
11    get_compact_nullable_string_borrowed, get_nullable_string_borrowed,
12};
13use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
14use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
15pub const MIN_VERSION: i16 = 1;
16pub const MAX_VERSION: i16 = 2;
17pub const FLEXIBLE_MIN: i16 = 2;
18
19#[inline]
20fn is_flexible(version: i16) -> bool {
21    version >= FLEXIBLE_MIN
22}
23
24#[derive(Debug, Clone, PartialEq, Eq, Default)]
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}
32impl RequestHeader<'_> {
33    pub fn to_owned(&self) -> crate::owned::request_header::RequestHeader {
34        crate::owned::request_header::RequestHeader {
35            request_api_key: (self.request_api_key),
36            request_api_version: (self.request_api_version),
37            correlation_id: (self.correlation_id),
38            client_id: (self.client_id).map(std::string::ToString::to_string),
39            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
40        }
41    }
42}
43impl Encode for RequestHeader<'_> {
44    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
45        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
46            return Err(ProtocolError::SchemaMismatch(
47                "RequestHeader version out of range",
48            ));
49        }
50        let flex = is_flexible(version);
51        if version >= 0 {
52            put_i16(buf, self.request_api_key);
53        }
54        if version >= 0 {
55            put_i16(buf, self.request_api_version);
56        }
57        if version >= 0 {
58            put_i32(buf, self.correlation_id);
59        }
60        if version >= 1 {
61            {
62                let flex = false;
63                if flex {
64                    put_compact_nullable_string(buf, self.client_id);
65                } else {
66                    put_nullable_string(buf, self.client_id);
67                }
68            }
69        }
70        if flex {
71            let tagged = WriteTaggedFields::new();
72            tagged.write(buf, &self.unknown_tagged_fields);
73        }
74        Ok(())
75    }
76    fn encoded_len(&self, version: i16) -> usize {
77        let flex = is_flexible(version);
78        let mut n: usize = 0;
79        if version >= 0 {
80            n += 2;
81        }
82        if version >= 0 {
83            n += 2;
84        }
85        if version >= 0 {
86            n += 4;
87        }
88        if version >= 1 {
89            n += {
90                let flex = false;
91                if flex {
92                    compact_nullable_string_len(self.client_id)
93                } else {
94                    nullable_string_len(self.client_id)
95                }
96            };
97        }
98        if flex {
99            let known_pairs: Vec<(u32, usize)> = Vec::new();
100            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
101        }
102        n
103    }
104}
105impl<'de> DecodeBorrow<'de> for RequestHeader<'de> {
106    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
107        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
108            return Err(ProtocolError::SchemaMismatch(
109                "RequestHeader version out of range",
110            ));
111        }
112        let flex = is_flexible(version);
113        let mut out = Self::default();
114        if version >= 0 {
115            out.request_api_key = get_i16(buf)?;
116        }
117        if version >= 0 {
118            out.request_api_version = get_i16(buf)?;
119        }
120        if version >= 0 {
121            out.correlation_id = get_i32(buf)?;
122        }
123        if version >= 1 {
124            out.client_id = {
125                let flex = false;
126                if flex {
127                    get_compact_nullable_string_borrowed(buf)?
128                } else {
129                    get_nullable_string_borrowed(buf)?
130                }
131            };
132        }
133        if flex {
134            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
135        }
136        Ok(out)
137    }
138}
139#[cfg(test)]
140impl RequestHeader<'_> {
141    #[must_use]
142    pub fn populated(version: i16) -> Self {
143        let mut m = Self::default();
144        if version >= 0 {
145            m.request_api_key = 1i16;
146        }
147        if version >= 0 {
148            m.request_api_version = 1i16;
149        }
150        if version >= 0 {
151            m.correlation_id = 1i32;
152        }
153        if version >= 1 {
154            m.client_id = Some("x");
155        }
156        m
157    }
158}