use crate::builders::traits::{
ConditionalBuilder, InspectableBuilder, SchemaBuilder, ValidatableBuilder,
};
use crate::builders::validation::EventBuildError;
use crate::schema::Event;
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct EventBuilder {
name: Option<String>,
schedule: Option<String>,
command: Option<String>,
active: bool,
database: Option<String>,
username: Option<String>,
nodename: Option<String>,
port: Option<u16>,
parameters: HashMap<String, String>,
comment: Option<String>,
}
impl EventBuilder {
pub fn new() -> Self {
Self {
name: None,
schedule: None,
command: None,
active: true,
database: None,
username: None,
nodename: None,
port: None,
parameters: HashMap::new(),
comment: None,
}
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn set_name(&mut self, name: impl Into<String>) {
self.name = Some(name.into());
}
pub fn schedule(mut self, schedule: impl Into<String>) -> Self {
self.schedule = Some(schedule.into());
self
}
pub fn set_schedule(&mut self, schedule: impl Into<String>) {
self.schedule = Some(schedule.into());
}
pub fn command(mut self, command: impl Into<String>) -> Self {
self.command = Some(command.into());
self
}
pub fn set_command(&mut self, command: impl Into<String>) {
self.command = Some(command.into());
}
pub fn active(mut self, active: bool) -> Self {
self.active = active;
self
}
pub fn set_active(&mut self, active: bool) {
self.active = active;
}
pub fn database(mut self, database: impl Into<String>) -> Self {
self.database = Some(database.into());
self
}
pub fn set_database(&mut self, database: impl Into<String>) {
self.database = Some(database.into());
}
pub fn username(mut self, username: impl Into<String>) -> Self {
self.username = Some(username.into());
self
}
pub fn set_username(&mut self, username: impl Into<String>) {
self.username = Some(username.into());
}
pub fn nodename(mut self, nodename: impl Into<String>) -> Self {
self.nodename = Some(nodename.into());
self
}
pub fn set_nodename(&mut self, nodename: impl Into<String>) {
self.nodename = Some(nodename.into());
}
pub fn port(mut self, port: u16) -> Self {
self.port = Some(port);
self
}
pub fn set_port(&mut self, port: u16) {
self.port = Some(port);
}
pub fn parameter(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.parameters.insert(key.into(), value.into());
self
}
pub fn add_parameter(&mut self, key: impl Into<String>, value: impl Into<String>) {
self.parameters.insert(key.into(), value.into());
}
pub fn parameters(mut self, parameters: HashMap<String, String>) -> Self {
self.parameters.extend(parameters);
self
}
pub fn add_parameters(&mut self, parameters: HashMap<String, String>) {
self.parameters.extend(parameters);
}
pub fn comment(mut self, comment: impl Into<String>) -> Self {
self.comment = Some(comment.into());
self
}
pub fn set_comment(&mut self, comment: impl Into<String>) {
self.comment = Some(comment.into());
}
pub fn daily_at(mut self, hour: u8, minute: u8) -> Self {
if hour > 23 || minute > 59 {
return self; }
self.schedule = Some(format!("{} {} * * *", minute, hour));
self
}
pub fn weekly(mut self, day_of_week: u8, hour: u8, minute: u8) -> Self {
if day_of_week > 6 || hour > 23 || minute > 59 {
return self; }
self.schedule = Some(format!("{} {} * * {}", minute, hour, day_of_week));
self
}
pub fn monthly(mut self, day_of_month: u8, hour: u8, minute: u8) -> Self {
if day_of_month > 31 || hour > 23 || minute > 59 {
return self; }
self.schedule = Some(format!("{} {} {} * *", minute, hour, day_of_month));
self
}
pub fn hourly(mut self, minute: u8) -> Self {
if minute > 59 {
return self; }
self.schedule = Some(format!("{} * * * *", minute));
self
}
pub fn every_minutes(mut self, minutes: u8) -> Self {
if minutes == 0 || minutes > 59 {
return self; }
self.schedule = Some(format!("*/{} * * * *", minutes));
self
}
pub fn every_hours(mut self, hours: u8) -> Self {
if hours == 0 || hours > 23 {
return self; }
self.schedule = Some(format!("0 */{} * * *", hours));
self
}
}
impl SchemaBuilder for EventBuilder {
type Output = Event;
type Error = EventBuildError;
fn build(self) -> Result<Self::Output, EventBuildError> {
let name = self.name.ok_or(EventBuildError::MissingName)?;
let schedule = self.schedule.ok_or(EventBuildError::MissingSchedule)?;
let command = self.command.ok_or(EventBuildError::MissingCommand)?;
let mut event = Event::new(name, schedule, command);
event.enabled = self.active;
if let Some(database) = self.database {
event.database = Some(database);
}
if let Some(username) = self.username {
event.username = Some(username);
}
Ok(event)
}
}
impl ValidatableBuilder for EventBuilder {
fn is_valid(&self) -> bool {
self.name.is_some()
&& self.schedule.is_some()
&& self.command.is_some()
&& self.port.map_or(true, |port| port > 0)
}
fn validation_errors(&self) -> Vec<String> {
let mut errors = Vec::new();
if self.name.is_none() {
errors.push("Event name is required".to_string());
}
if self.schedule.is_none() {
errors.push("Event schedule is required".to_string());
}
if self.command.is_none() {
errors.push("Event command is required".to_string());
}
if let Some(ref schedule) = self.schedule {
let parts: Vec<&str> = schedule.split_whitespace().collect();
if parts.len() != 5 {
errors.push("Invalid cron schedule format".to_string());
}
}
if let Some(port) = self.port {
if port == 0 {
errors.push(format!("Invalid port number: {}", port));
}
}
errors
}
}
impl ConditionalBuilder for EventBuilder {
}
impl InspectableBuilder for EventBuilder {
fn debug_info(&self) -> String {
format!(
"EventBuilder {{ name: {:?}, schedule: {:?}, command: {:?}, active: {}, database: {:?}, username: {:?}, nodename: {:?}, port: {:?}, parameters: {:?}, comment: {:?} }}",
self.name,
self.schedule,
self.command,
self.active,
self.database,
self.username,
self.nodename,
self.port,
self.parameters,
self.comment
)
}
fn summary(&self) -> String {
let name = self.name.as_deref().unwrap_or("unnamed");
let schedule = self.schedule.as_deref().unwrap_or("no schedule");
let status = if self.active { "active" } else { "inactive" };
format!("Event '{}' scheduled '{}' ({})", name, schedule, status)
}
}