Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
ResponseHeader.borrowed.rs

1// AUTO-GENERATED by krabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2use bytes::BufMut;
3
4use crate::{
5    DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields,
6    primitives::fixed::{get_i32, put_i32},
7    tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
8};
9pub const MIN_VERSION: i16 = 0;
10pub const MAX_VERSION: i16 = 1;
11pub const FLEXIBLE_MIN: i16 = 1;
12#[inline]
13#[must_use]
14pub fn is_flexible(version: i16) -> bool {
15    version >= FLEXIBLE_MIN
16}
17#[derive(Debug, Clone, PartialEq, Eq, Default)]
18pub struct ResponseHeader {
19    pub correlation_id: i32,
20    pub unknown_tagged_fields: UnknownTaggedFields,
21}
22impl ResponseHeader {
23    /// # Panics
24    ///
25    /// Panics if a records field contains an invalid encoded record batch.
26    #[must_use]
27    pub fn to_owned(&self) -> crate::owned::response_header::ResponseHeader {
28        crate::owned::response_header::ResponseHeader {
29            correlation_id: (self.correlation_id),
30            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
31        }
32    }
33}
34impl Encode for ResponseHeader {
35    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
36        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
37            return Err(ProtocolError::SchemaMismatch(
38                "ResponseHeader version out of range",
39            ));
40        }
41        let flex = is_flexible(version);
42        if version >= 0 {
43            put_i32(buf, self.correlation_id);
44        }
45        if flex {
46            let tagged = WriteTaggedFields::new();
47            tagged.write(buf, &self.unknown_tagged_fields);
48        }
49        Ok(())
50    }
51    fn encoded_len(&self, version: i16) -> usize {
52        let flex = is_flexible(version);
53        let mut n: usize = 0;
54        if version >= 0 {
55            n += 4;
56        }
57        if flex {
58            let known_pairs: Vec<(u32, usize)> = Vec::new();
59            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
60        }
61        n
62    }
63}
64impl<'de> DecodeBorrow<'de> for ResponseHeader {
65    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
66        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
67            return Err(ProtocolError::SchemaMismatch(
68                "ResponseHeader version out of range",
69            ));
70        }
71        let flex = is_flexible(version);
72        let mut out = Self::default();
73        if version >= 0 {
74            out.correlation_id = get_i32(buf)?;
75        }
76        if flex {
77            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
78        }
79        Ok(out)
80    }
81}
82#[cfg(test)]
83impl ResponseHeader {
84    #[must_use]
85    pub fn populated(version: i16) -> Self {
86        let mut m = Self::default();
87        if version >= 0 {
88            m.correlation_id = 1i32;
89        }
90        m
91    }
92}