use std::sync::Arc;
const MAX_TOPIC_BYTES: usize = 249;
#[derive(Debug, Eq, PartialEq)]
pub struct GroupConsumerPartition {
topic: Arc<str>,
partition: i32,
}
impl GroupConsumerPartition {
pub fn try_new(
topic: impl Into<Arc<str>>,
partition: i32,
) -> Result<Self, GroupConsumerPartitionInputError> {
let topic = topic.into();
let kind = if topic.is_empty() {
Some(GroupConsumerPartitionInputErrorKind::EmptyTopic)
} else if topic.len() > MAX_TOPIC_BYTES {
Some(GroupConsumerPartitionInputErrorKind::TopicTooLong)
} else if partition < 0 {
Some(GroupConsumerPartitionInputErrorKind::NegativePartition)
} else {
None
};
match kind {
Some(kind) => Err(GroupConsumerPartitionInputError { kind }),
None => Ok(Self { topic, partition }),
}
}
pub fn topic(&self) -> &str {
&self.topic
}
pub const fn partition(&self) -> i32 {
self.partition
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum GroupConsumerPartitionInputErrorKind {
EmptyTopic,
TopicTooLong,
NegativePartition,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct GroupConsumerPartitionInputError {
kind: GroupConsumerPartitionInputErrorKind,
}
impl GroupConsumerPartitionInputError {
pub const fn kind(self) -> GroupConsumerPartitionInputErrorKind {
self.kind
}
}
impl core::fmt::Display for GroupConsumerPartitionInputError {
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
formatter,
"invalid classic-group control partition: {:?}",
self.kind
)
}
}
impl std::error::Error for GroupConsumerPartitionInputError {}