kafka_wire_protocol/schema/fetch_request/
v4.rs1use std::io::{Read, Result, Write};
4
5use serde::{Deserialize, Serialize};
6#[cfg(test)] use proptest_derive::Arbitrary;
7
8use crate::arrays::{read_array, write_array};
9use crate::markers::{ApiMessage, Request};
10use crate::readable_writable::{Readable, Writable};
11#[cfg(test)] use crate::test_utils::proptest_strategies;
12
13#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
15#[cfg_attr(test, derive(Arbitrary))]
16pub struct FetchRequest {
17 pub replica_id: i32,
19 pub max_wait_ms: i32,
21 pub min_bytes: i32,
23 pub max_bytes: i32,
25 pub isolation_level: i8,
27 #[cfg_attr(test, proptest(strategy = "proptest_strategies::vec()"))]
29 pub topics: Vec<FetchTopic>,
30}
31
32impl ApiMessage for FetchRequest {
33 fn api_key(&self) -> i16 {
34 1
35 }
36
37 fn version(&self) -> i16 {
38 4
39 }
40}
41
42impl Request for FetchRequest { }
43
44impl Default for FetchRequest {
45 fn default() -> Self {
46 FetchRequest {
47 replica_id: -1_i32,
48 max_wait_ms: 0_i32,
49 min_bytes: 0_i32,
50 max_bytes: 0x7fffffff_i32,
51 isolation_level: 0_i8,
52 topics: Vec::<FetchTopic>::new(),
53 }
54 }
55}
56
57impl FetchRequest {
58 pub fn new(replica_id: i32, max_wait_ms: i32, min_bytes: i32, max_bytes: i32, isolation_level: i8, topics: Vec<FetchTopic>) -> Self {
59 Self {
60 replica_id,
61 max_wait_ms,
62 min_bytes,
63 max_bytes,
64 isolation_level,
65 topics,
66 }
67 }
68}
69
70#[cfg(test)]
71mod tests_fetch_request_new_and_default {
72 use super::*;
73
74 #[test]
75 fn test() {
76 let d = FetchRequest::new(
77 -1_i32,
78 0_i32,
79 0_i32,
80 0x7fffffff_i32,
81 0_i8,
82 Vec::<FetchTopic>::new(),
83 );
84 assert_eq!(d, FetchRequest::default());
85 }
86}
87
88impl Readable for FetchRequest {
89 fn read(#[allow(unused)] input: &mut impl Read) -> Result<Self> {
90 let replica_id = i32::read(input)?;
91 let max_wait_ms = i32::read(input)?;
92 let min_bytes = i32::read(input)?;
93 let max_bytes = i32::read(input)?;
94 let isolation_level = i8::read(input)?;
95 let topics = read_array::<FetchTopic>(input, "topics", false)?;
96 Ok(FetchRequest {
97 replica_id, max_wait_ms, min_bytes, max_bytes, isolation_level, topics
98 })
99 }
100}
101
102impl Writable for FetchRequest {
103 fn write(&self, #[allow(unused)] output: &mut impl Write) -> Result<()> {
104 self.replica_id.write(output)?;
105 self.max_wait_ms.write(output)?;
106 self.min_bytes.write(output)?;
107 self.max_bytes.write(output)?;
108 self.isolation_level.write(output)?;
109 write_array(output, "self.topics", &self.topics, false)?;
110 Ok(())
111 }
112}
113
114#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
116#[cfg_attr(test, derive(Arbitrary))]
117pub struct FetchTopic {
118 #[cfg_attr(test, proptest(strategy = "proptest_strategies::string()"))]
120 pub topic: String,
121 #[cfg_attr(test, proptest(strategy = "proptest_strategies::vec()"))]
123 pub partitions: Vec<FetchPartition>,
124}
125
126impl Default for FetchTopic {
127 fn default() -> Self {
128 FetchTopic {
129 topic: String::from(""),
130 partitions: Vec::<FetchPartition>::new(),
131 }
132 }
133}
134
135impl FetchTopic {
136 pub fn new<S1: AsRef<str>>(topic: S1, partitions: Vec<FetchPartition>) -> Self {
137 Self {
138 topic: topic.as_ref().to_string(),
139 partitions,
140 }
141 }
142}
143
144#[cfg(test)]
145mod tests_fetch_topic_new_and_default {
146 use super::*;
147
148 #[test]
149 fn test() {
150 let d = FetchTopic::new(
151 String::from(""),
152 Vec::<FetchPartition>::new(),
153 );
154 assert_eq!(d, FetchTopic::default());
155 }
156}
157
158impl Readable for FetchTopic {
159 fn read(#[allow(unused)] input: &mut impl Read) -> Result<Self> {
160 let topic = String::read_ext(input, "topic", false)?;
161 let partitions = read_array::<FetchPartition>(input, "partitions", false)?;
162 Ok(FetchTopic {
163 topic, partitions
164 })
165 }
166}
167
168impl Writable for FetchTopic {
169 fn write(&self, #[allow(unused)] output: &mut impl Write) -> Result<()> {
170 self.topic.write_ext(output, "self.topic", false)?;
171 write_array(output, "self.partitions", &self.partitions, false)?;
172 Ok(())
173 }
174}
175
176#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
178#[cfg_attr(test, derive(Arbitrary))]
179pub struct FetchPartition {
180 pub partition: i32,
182 pub fetch_offset: i64,
184 pub partition_max_bytes: i32,
186}
187
188impl Default for FetchPartition {
189 fn default() -> Self {
190 FetchPartition {
191 partition: 0_i32,
192 fetch_offset: 0_i64,
193 partition_max_bytes: 0_i32,
194 }
195 }
196}
197
198impl FetchPartition {
199 pub fn new(partition: i32, fetch_offset: i64, partition_max_bytes: i32) -> Self {
200 Self {
201 partition,
202 fetch_offset,
203 partition_max_bytes,
204 }
205 }
206}
207
208#[cfg(test)]
209mod tests_fetch_partition_new_and_default {
210 use super::*;
211
212 #[test]
213 fn test() {
214 let d = FetchPartition::new(
215 0_i32,
216 0_i64,
217 0_i32,
218 );
219 assert_eq!(d, FetchPartition::default());
220 }
221}
222
223impl Readable for FetchPartition {
224 fn read(#[allow(unused)] input: &mut impl Read) -> Result<Self> {
225 let partition = i32::read(input)?;
226 let fetch_offset = i64::read(input)?;
227 let partition_max_bytes = i32::read(input)?;
228 Ok(FetchPartition {
229 partition, fetch_offset, partition_max_bytes
230 })
231 }
232}
233
234impl Writable for FetchPartition {
235 fn write(&self, #[allow(unused)] output: &mut impl Write) -> Result<()> {
236 self.partition.write(output)?;
237 self.fetch_offset.write(output)?;
238 self.partition_max_bytes.write(output)?;
239 Ok(())
240 }
241}
242
243#[cfg(test)]
244mod tests {
245 use super::*;
246 use proptest::prelude::*;
247
248 #[test]
249 fn test_java_default() {
250 crate::test_utils::test_java_default::<FetchRequest>("FetchRequest", 4);
251 }
252
253 proptest! {
254 #[test]
255 fn test_serde(data: FetchRequest) {
256 crate::test_utils::test_serde(&data)?;
257 }
258 }
259
260 proptest! {
261 #[test]
262 fn test_java_arbitrary(data: FetchRequest) {
263 crate::test_utils::test_java_arbitrary(&data, "FetchRequest", 4);
264 }
265 }
266}