kafka_client_engine/consumer/assigned_host/delivery/
record.rs1use crate::protocol::fetch::{FetchBatch, FetchHeader, FetchRecord};
4
5use super::AssignedConsumerDelivery;
6
7#[derive(Debug)]
9pub struct AssignedConsumerRecords<'batch> {
10 topic: &'batch str,
11 partition: i32,
12 batches: &'batch [FetchBatch],
13 batch_index: usize,
14 record_index: usize,
15}
16
17impl<'batch> AssignedConsumerRecords<'batch> {
18 pub(super) fn new(delivery: &'batch AssignedConsumerDelivery) -> Self {
19 let batches = delivery
20 .lease()
21 .outcome()
22 .outcome()
23 .data_batches()
24 .unwrap_or_default();
25 Self {
26 topic: delivery.topic(),
27 partition: delivery.partition(),
28 batches,
29 batch_index: 0,
30 record_index: 0,
31 }
32 }
33}
34
35impl<'batch> Iterator for AssignedConsumerRecords<'batch> {
36 type Item = AssignedConsumerRecord<'batch>;
37
38 fn next(&mut self) -> Option<Self::Item> {
39 loop {
40 let batch = self.batches.get(self.batch_index)?;
41 if batch.is_control {
42 self.batch_index += 1;
43 self.record_index = 0;
44 continue;
45 }
46 if let Some(record) = batch.records.get(self.record_index) {
47 self.record_index += 1;
48 return Some(AssignedConsumerRecord {
49 topic: self.topic,
50 partition: self.partition,
51 record,
52 });
53 }
54 self.batch_index += 1;
55 self.record_index = 0;
56 }
57 }
58}
59
60#[derive(Debug)]
62pub struct AssignedConsumerRecord<'batch> {
63 topic: &'batch str,
64 partition: i32,
65 record: &'batch FetchRecord,
66}
67
68impl AssignedConsumerRecord<'_> {
69 pub const fn topic(&self) -> &str {
71 self.topic
72 }
73
74 pub const fn partition(&self) -> i32 {
76 self.partition
77 }
78
79 pub const fn offset(&self) -> i64 {
81 self.record.offset
82 }
83
84 pub const fn timestamp_millis(&self) -> Option<i64> {
86 self.record.timestamp
87 }
88
89 pub fn key(&self) -> Option<&[u8]> {
91 self.record.key.as_deref()
92 }
93
94 pub fn value(&self) -> Option<&[u8]> {
96 self.record.value.as_deref()
97 }
98
99 pub fn headers(&self) -> impl ExactSizeIterator<Item = AssignedConsumerHeader<'_>> {
101 self.record
102 .headers
103 .iter()
104 .map(|header| AssignedConsumerHeader { header })
105 }
106}
107
108#[derive(Debug)]
110pub struct AssignedConsumerHeader<'record> {
111 header: &'record FetchHeader,
112}
113
114impl AssignedConsumerHeader<'_> {
115 pub fn key(&self) -> &[u8] {
117 &self.header.key
118 }
119
120 pub fn value(&self) -> Option<&[u8]> {
122 self.header.value.as_deref()
123 }
124}