Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
DescribeLogDirsRequest.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_string_len, put_compact_string, put_string, string_len,
8};
9use crate::primitives::string_bytes_borrowed::{get_compact_string_borrowed, get_string_borrowed};
10use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
11use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
12
13pub const API_KEY: i16 = 35;
14pub const MIN_VERSION: i16 = 1;
15pub const MAX_VERSION: i16 = 5;
16pub const FLEXIBLE_MIN: i16 = 2;
17
18#[inline]
19fn is_flexible(version: i16) -> bool {
20    version >= FLEXIBLE_MIN
21}
22
23#[derive(Debug, Clone, PartialEq, Eq, Default)]
24pub struct DescribeLogDirsRequest<'a> {
25    pub topics: Option<Vec<DescribableLogDirTopic<'a>>>,
26    pub unknown_tagged_fields: UnknownTaggedFields,
27}
28impl DescribeLogDirsRequest<'_> {
29    pub fn to_owned(&self) -> crate::owned::describe_log_dirs_request::DescribeLogDirsRequest {
30        crate::owned::describe_log_dirs_request::DescribeLogDirsRequest {
31            topics: (self.topics)
32                .as_ref()
33                .map(|v| v.iter().map(DescribableLogDirTopic::to_owned).collect()),
34            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
35        }
36    }
37}
38impl Encode for DescribeLogDirsRequest<'_> {
39    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
40        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
41            return Err(ProtocolError::UnsupportedVersion {
42                api_key: API_KEY,
43                version,
44            });
45        }
46        let flex = is_flexible(version);
47        if version >= 0 {
48            {
49                let len = (self.topics).as_ref().map(Vec::len);
50                crate::primitives::array::put_nullable_array_len(buf, len, flex);
51                if let Some(v) = &self.topics {
52                    for it in v {
53                        it.encode(buf, version)?;
54                    }
55                }
56            }
57        }
58        if flex {
59            let tagged = WriteTaggedFields::new();
60            tagged.write(buf, &self.unknown_tagged_fields);
61        }
62        Ok(())
63    }
64    fn encoded_len(&self, version: i16) -> usize {
65        let flex = is_flexible(version);
66        let mut n: usize = 0;
67        if version >= 0 {
68            n += {
69                let opt: Option<&Vec<_>> = (self.topics).as_ref();
70                let prefix = crate::primitives::array::nullable_array_len_prefix_len(
71                    opt.map(std::vec::Vec::len),
72                    flex,
73                );
74                let body: usize =
75                    opt.map_or(0, |v| v.iter().map(|it| it.encoded_len(version)).sum());
76                prefix + body
77            };
78        }
79        if flex {
80            let known_pairs: Vec<(u32, usize)> = Vec::new();
81            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
82        }
83        n
84    }
85}
86impl<'de> DecodeBorrow<'de> for DescribeLogDirsRequest<'de> {
87    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
88        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
89            return Err(ProtocolError::UnsupportedVersion {
90                api_key: API_KEY,
91                version,
92            });
93        }
94        let flex = is_flexible(version);
95        let mut out = Self::default();
96        if version >= 0 {
97            out.topics = {
98                let opt = crate::primitives::array::get_nullable_array_len(buf, flex)?;
99                match opt {
100                    None => None,
101                    Some(n) => {
102                        let mut v = Vec::with_capacity(n);
103                        for _ in 0..n {
104                            v.push(DescribableLogDirTopic::decode_borrow(buf, version)?);
105                        }
106                        Some(v)
107                    }
108                }
109            };
110        }
111        if flex {
112            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
113        }
114        Ok(out)
115    }
116}
117#[cfg(test)]
118impl DescribeLogDirsRequest<'_> {
119    #[must_use]
120    pub fn populated(version: i16) -> Self {
121        let mut m = Self::default();
122        if version >= 0 {
123            m.topics = Some(vec![DescribableLogDirTopic::populated(version)]);
124        }
125        m
126    }
127}
128#[derive(Debug, Clone, PartialEq, Eq, Default)]
129pub struct DescribableLogDirTopic<'a> {
130    pub topic: &'a str,
131    pub partitions: Vec<i32>,
132    pub unknown_tagged_fields: UnknownTaggedFields,
133}
134impl DescribableLogDirTopic<'_> {
135    pub fn to_owned(&self) -> crate::owned::describe_log_dirs_request::DescribableLogDirTopic {
136        crate::owned::describe_log_dirs_request::DescribableLogDirTopic {
137            topic: (self.topic).to_string(),
138            partitions: (self.partitions).clone(),
139            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
140        }
141    }
142}
143impl Encode for DescribableLogDirTopic<'_> {
144    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
145        let flex = version >= 2;
146        if version >= 0 {
147            if flex {
148                put_compact_string(buf, self.topic);
149            } else {
150                put_string(buf, self.topic);
151            }
152        }
153        if version >= 0 {
154            {
155                crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
156                for it in &self.partitions {
157                    put_i32(buf, *it);
158                }
159            }
160        }
161        if flex {
162            let tagged = WriteTaggedFields::new();
163            tagged.write(buf, &self.unknown_tagged_fields);
164        }
165        Ok(())
166    }
167    fn encoded_len(&self, version: i16) -> usize {
168        let flex = version >= 2;
169        let mut n: usize = 0;
170        if version >= 0 {
171            n += if flex {
172                compact_string_len(self.topic)
173            } else {
174                string_len(self.topic)
175            };
176        }
177        if version >= 0 {
178            n += {
179                let prefix =
180                    crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex);
181                let body: usize = (self.partitions).iter().map(|_| 4).sum();
182                prefix + body
183            };
184        }
185        if flex {
186            let known_pairs: Vec<(u32, usize)> = Vec::new();
187            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
188        }
189        n
190    }
191}
192impl<'de> DecodeBorrow<'de> for DescribableLogDirTopic<'de> {
193    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
194        let flex = version >= 2;
195        let mut out = Self::default();
196        if version >= 0 {
197            out.topic = if flex {
198                get_compact_string_borrowed(buf)?
199            } else {
200                get_string_borrowed(buf)?
201            };
202        }
203        if version >= 0 {
204            out.partitions = {
205                let n = crate::primitives::array::get_array_len(buf, flex)?;
206                let mut v = Vec::with_capacity(n);
207                for _ in 0..n {
208                    v.push(get_i32(buf)?);
209                }
210                v
211            };
212        }
213        if flex {
214            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
215        }
216        Ok(out)
217    }
218}
219#[cfg(test)]
220impl DescribableLogDirTopic<'_> {
221    #[must_use]
222    pub fn populated(version: i16) -> Self {
223        let mut m = Self::default();
224        if version >= 0 {
225            m.topic = "x";
226        }
227        if version >= 0 {
228            m.partitions = vec![1i32];
229        }
230        m
231    }
232}