Skip to main content

kafka_client_engine/consumer/group_control/
partition.rs

1//! Validated stable scalar target for classic-group batch control.
2
3use std::sync::Arc;
4
5const MAX_TOPIC_BYTES: usize = 249;
6
7/// One caller-owned classic-group control target.
8#[derive(Debug, Eq, PartialEq)]
9pub struct GroupConsumerPartition {
10    topic: Arc<str>,
11    partition: i32,
12}
13
14impl GroupConsumerPartition {
15    /// Validates one topic spelling and zero-based partition.
16    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    /// Returns the exact topic spelling.
37    pub fn topic(&self) -> &str {
38        &self.topic
39    }
40
41    /// Returns the zero-based Kafka partition.
42    pub const fn partition(&self) -> i32 {
43        self.partition
44    }
45}
46
47/// Stable scalar-input rejection.
48#[derive(Clone, Copy, Debug, Eq, PartialEq)]
49pub enum GroupConsumerPartitionInputErrorKind {
50    /// The topic spelling is empty.
51    EmptyTopic,
52    /// The topic spelling exceeds Kafka's bounded UTF-8 byte domain.
53    TopicTooLong,
54    /// The partition is outside Kafka's nonnegative partition domain.
55    NegativePartition,
56}
57
58/// Rejection of one invalid scalar control target.
59#[derive(Clone, Copy, Debug, Eq, PartialEq)]
60pub struct GroupConsumerPartitionInputError {
61    kind: GroupConsumerPartitionInputErrorKind,
62}
63
64impl GroupConsumerPartitionInputError {
65    /// Returns the stable invalid-input category.
66    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 {}