use crate::Consumer;
use crate::error::IggyError;
use crate::{Identifier, PollingStrategy, Validatable};
use serde::{Deserialize, Serialize};
pub const DEFAULT_PARTITION_ID: u32 = 0;
pub const DEFAULT_NUMBER_OF_MESSAGES_TO_POLL: u32 = 10;
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct PollMessages {
#[serde(flatten)]
pub consumer: Consumer,
#[serde(skip)]
pub stream_id: Identifier,
#[serde(skip)]
pub topic_id: Identifier,
#[serde(default = "PollMessages::default_partition_id")]
pub partition_id: Option<u32>,
#[serde(default = "PollingStrategy::default", flatten)]
pub strategy: PollingStrategy,
#[serde(default = "PollMessages::default_number_of_messages_to_poll")]
pub count: u32,
#[serde(default)]
pub auto_commit: bool,
}
impl PollMessages {
pub fn default_number_of_messages_to_poll() -> u32 {
DEFAULT_NUMBER_OF_MESSAGES_TO_POLL
}
pub fn default_partition_id() -> Option<u32> {
Some(DEFAULT_PARTITION_ID)
}
}
impl Default for PollMessages {
fn default() -> Self {
Self {
consumer: Consumer::default(),
stream_id: Identifier::numeric(1).unwrap(),
topic_id: Identifier::numeric(1).unwrap(),
partition_id: PollMessages::default_partition_id(),
strategy: PollingStrategy::default(),
count: PollMessages::default_number_of_messages_to_poll(),
auto_commit: false,
}
}
}
impl Validatable<IggyError> for PollMessages {
fn validate(&self) -> Result<(), IggyError> {
Ok(())
}
}