kafka_client_engine/consumer/assigned_host/
control.rs1use std::sync::Arc;
4
5use kafka_client_core::PartitionIndex;
6
7const MAX_TOPIC_NAME_BYTES: usize = 249;
8
9#[derive(Clone, Debug, Eq, PartialEq)]
11pub struct AssignedConsumerPartition {
12 pub(in crate::consumer) topic: Arc<str>,
13 partition: i32,
14}
15
16impl AssignedConsumerPartition {
17 pub fn try_new(
19 topic: impl Into<Arc<str>>,
20 partition: i32,
21 ) -> Result<Self, AssignedConsumerPartitionInputError> {
22 let topic = topic.into();
23 if topic.is_empty() {
24 return Err(AssignedConsumerPartitionInputError::new(
25 AssignedConsumerPartitionInputErrorKind::EmptyTopic,
26 ));
27 }
28 if topic.len() > MAX_TOPIC_NAME_BYTES {
29 return Err(AssignedConsumerPartitionInputError::new(
30 AssignedConsumerPartitionInputErrorKind::TopicTooLong,
31 ));
32 }
33 if partition.is_negative() {
34 return Err(AssignedConsumerPartitionInputError::new(
35 AssignedConsumerPartitionInputErrorKind::NegativePartition,
36 ));
37 }
38 Ok(Self { topic, partition })
39 }
40
41 pub fn topic(&self) -> &str {
43 &self.topic
44 }
45
46 pub const fn partition(&self) -> i32 {
48 self.partition
49 }
50
51 pub(in crate::consumer) const fn partition_index(&self) -> PartitionIndex {
52 PartitionIndex::from_raw(self.partition.cast_unsigned())
53 }
54}
55
56#[derive(Clone, Copy, Debug, Eq, PartialEq)]
58pub enum AssignedConsumerPartitionInputErrorKind {
59 EmptyTopic,
61 TopicTooLong,
63 NegativePartition,
65}
66
67#[derive(Clone, Copy, Debug, Eq, PartialEq)]
69pub struct AssignedConsumerPartitionInputError {
70 kind: AssignedConsumerPartitionInputErrorKind,
71}
72
73impl AssignedConsumerPartitionInputError {
74 const fn new(kind: AssignedConsumerPartitionInputErrorKind) -> Self {
75 Self { kind }
76 }
77
78 pub const fn kind(&self) -> AssignedConsumerPartitionInputErrorKind {
80 self.kind
81 }
82}
83
84impl std::fmt::Display for AssignedConsumerPartitionInputError {
85 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86 write!(
87 formatter,
88 "invalid assigned-consumer partition: {:?}",
89 self.kind
90 )
91 }
92}
93
94impl std::error::Error for AssignedConsumerPartitionInputError {}
95
96#[derive(Clone, Copy, Debug, Eq, PartialEq)]
97pub(crate) enum AssignedConsumerControlInputError {
98 UnknownTopic,
99 NegativeOffset,
100}