Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
HeartbeatRequest.borrowed.rs

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use bytes::BufMut;
4
5use crate::primitives::fixed::{get_i32, 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};
17
18pub const API_KEY: i16 = 12;
19pub const MIN_VERSION: i16 = 0;
20pub const MAX_VERSION: i16 = 4;
21pub const FLEXIBLE_MIN: i16 = 4;
22
23#[inline]
24fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
25
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct HeartbeatRequest<'a> {
28    pub group_id: &'a str,
29    pub generation_id: i32,
30    pub member_id: &'a str,
31    pub group_instance_id: Option<&'a str>,
32    pub unknown_tagged_fields: UnknownTaggedFields,
33}
34
35impl<'a> Default for HeartbeatRequest<'a> {
36    fn default() -> Self {
37        Self {
38            group_id: "",
39            generation_id: 0i32,
40            member_id: "",
41            group_instance_id: None,
42            unknown_tagged_fields: Default::default(),
43        }
44    }
45}
46
47impl<'a> HeartbeatRequest<'a> {
48    pub fn to_owned(&self) -> crate::owned::heartbeat_request::HeartbeatRequest {
49        crate::owned::heartbeat_request::HeartbeatRequest {
50            group_id: (self.group_id).to_string(),
51            generation_id: (self.generation_id),
52            member_id: (self.member_id).to_string(),
53            group_instance_id: (self.group_instance_id).map(|s| s.to_string()),
54            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
55        }
56    }
57}
58
59impl<'a> Encode for HeartbeatRequest<'a> {
60    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
61        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
62            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
63        }
64        let flex = is_flexible(version);
65        if version >= 0 { if flex { put_compact_string(buf, self.group_id) } else { put_string(buf, self.group_id) } }
66        if version >= 0 { put_i32(buf, self.generation_id) }
67        if version >= 0 { if flex { put_compact_string(buf, self.member_id) } else { put_string(buf, self.member_id) } }
68        if version >= 3 { if flex { put_compact_nullable_string(buf, self.group_instance_id) } else { put_nullable_string(buf, self.group_instance_id) } }
69        if flex {
70            let tagged = WriteTaggedFields::new();
71            tagged.write(buf, &self.unknown_tagged_fields);
72        }
73        Ok(())
74    }
75    fn encoded_len(&self, version: i16) -> usize {
76        let flex = is_flexible(version);
77        let mut n: usize = 0;
78        if version >= 0 { n += if flex { compact_string_len(self.group_id) } else { string_len(self.group_id) }; }
79        if version >= 0 { n += 4; }
80        if version >= 0 { n += if flex { compact_string_len(self.member_id) } else { string_len(self.member_id) }; }
81        if version >= 3 { n += if flex { compact_nullable_string_len(self.group_instance_id) } else { nullable_string_len(self.group_instance_id) }; }
82        if flex {
83            let known_pairs: Vec<(u32, usize)> = Vec::new();
84            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
85        }
86        n
87    }
88}
89
90impl<'de> DecodeBorrow<'de> for HeartbeatRequest<'de> {
91    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
92        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
93            return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
94        }
95        let flex = is_flexible(version);
96        let mut out = Self::default();
97        if version >= 0 { out.group_id = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
98        if version >= 0 { out.generation_id = get_i32(buf)?; }
99        if version >= 0 { out.member_id = if flex { get_compact_string_borrowed(buf)? } else { get_string_borrowed(buf)? }; }
100        if version >= 3 { out.group_instance_id = if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? }; }
101        if flex {
102            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
103                Ok(false)
104            })?;
105        }
106        Ok(out)
107    }
108}