Skip to main content

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