kafka_client_engine/consumer/group_control/
partition.rs1use std::sync::Arc;
4
5const MAX_TOPIC_BYTES: usize = 249;
6
7#[derive(Debug, Eq, PartialEq)]
9pub struct GroupConsumerPartition {
10 topic: Arc<str>,
11 partition: i32,
12}
13
14impl GroupConsumerPartition {
15 pub fn try_new(
17 topic: impl Into<Arc<str>>,
18 partition: i32,
19 ) -> Result<Self, GroupConsumerPartitionInputError> {
20 let topic = topic.into();
21 let kind = if topic.is_empty() {
22 Some(GroupConsumerPartitionInputErrorKind::EmptyTopic)
23 } else if topic.len() > MAX_TOPIC_BYTES {
24 Some(GroupConsumerPartitionInputErrorKind::TopicTooLong)
25 } else if partition < 0 {
26 Some(GroupConsumerPartitionInputErrorKind::NegativePartition)
27 } else {
28 None
29 };
30 match kind {
31 Some(kind) => Err(GroupConsumerPartitionInputError { kind }),
32 None => Ok(Self { topic, partition }),
33 }
34 }
35
36 pub fn topic(&self) -> &str {
38 &self.topic
39 }
40
41 pub const fn partition(&self) -> i32 {
43 self.partition
44 }
45}
46
47#[derive(Clone, Copy, Debug, Eq, PartialEq)]
49pub enum GroupConsumerPartitionInputErrorKind {
50 EmptyTopic,
52 TopicTooLong,
54 NegativePartition,
56}
57
58#[derive(Clone, Copy, Debug, Eq, PartialEq)]
60pub struct GroupConsumerPartitionInputError {
61 kind: GroupConsumerPartitionInputErrorKind,
62}
63
64impl GroupConsumerPartitionInputError {
65 pub const fn kind(self) -> GroupConsumerPartitionInputErrorKind {
67 self.kind
68 }
69}
70
71impl core::fmt::Display for GroupConsumerPartitionInputError {
72 fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
73 write!(
74 formatter,
75 "invalid classic-group control partition: {:?}",
76 self.kind
77 )
78 }
79}
80
81impl std::error::Error for GroupConsumerPartitionInputError {}