Skip to main content

kafka_client_engine/consumer/assigned_host/
control.rs

1//! Stable engine-owned scalar target for direct-consumer position control.
2
3use std::sync::Arc;
4
5use kafka_client_core::PartitionIndex;
6
7const MAX_TOPIC_NAME_BYTES: usize = 249;
8
9/// One topic-partition named at a direct-consumer control boundary.
10#[derive(Clone, Debug, Eq, PartialEq)]
11pub struct AssignedConsumerPartition {
12    pub(in crate::consumer) topic: Arc<str>,
13    partition: i32,
14}
15
16impl AssignedConsumerPartition {
17    /// Validates one stable topic-partition representation.
18    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    /// Returns the retained topic name.
42    pub fn topic(&self) -> &str {
43        &self.topic
44    }
45
46    /// Returns the validated Kafka partition.
47    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/// Stable reason one control target is not representable.
57#[derive(Clone, Copy, Debug, Eq, PartialEq)]
58pub enum AssignedConsumerPartitionInputErrorKind {
59    /// Kafka topic names cannot be empty.
60    EmptyTopic,
61    /// The topic exceeds Kafka's 249-byte name limit.
62    TopicTooLong,
63    /// Kafka partitions are nonnegative signed 32-bit values.
64    NegativePartition,
65}
66
67/// Rejection while constructing one direct-consumer control target.
68#[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    /// Returns the stable scalar-validation category.
79    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}