use super::MAX_NAME_LENGTH;
use crate::CompressionAlgorithm;
use crate::Identifier;
use crate::Validatable;
use crate::error::IggyError;
use crate::utils::expiry::IggyExpiry;
use crate::utils::topic_size::MaxTopicSize;
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct UpdateTopic {
#[serde(skip)]
pub stream_id: Identifier,
#[serde(skip)]
pub topic_id: Identifier,
pub compression_algorithm: CompressionAlgorithm,
pub message_expiry: IggyExpiry,
pub max_topic_size: MaxTopicSize,
pub replication_factor: Option<u8>,
pub name: String,
}
impl Default for UpdateTopic {
fn default() -> Self {
UpdateTopic {
stream_id: Identifier::default(),
topic_id: Identifier::default(),
compression_algorithm: Default::default(),
message_expiry: IggyExpiry::NeverExpire,
max_topic_size: MaxTopicSize::ServerDefault,
replication_factor: None,
name: "topic".to_string(),
}
}
}
impl Validatable<IggyError> for UpdateTopic {
fn validate(&self) -> Result<(), IggyError> {
if self.name.is_empty() || self.name.len() > MAX_NAME_LENGTH {
return Err(IggyError::InvalidTopicName);
}
if let Some(replication_factor) = self.replication_factor
&& replication_factor == 0
{
return Err(IggyError::InvalidReplicationFactor);
}
Ok(())
}
}