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