idlefox/src/votes/events.rs

130 lines
4.6 KiB
Rust

//! The `voting_events` submodule is dedicated to managing the lifecycle and
//! functionality of voting events
//!
//! * `ParsedVote`: A comprehensive structure that represents a single vote,
//! encapsulating both ongoing and planned votes. It stores information
//! about the event's title, description, status, start and end dates,
//! auto-delete date, votes cast by users, list of valid options (candidates)
//! for the vote, and type of vote being conducted.
//! * `EventStatus`: An enumeration that defines various states a voting event
//! can be in, such as planned, ongoing, closed, and cancelled. This helps
//! to track and manage the progression of each voting event according to
//! predefined rules and conditions.
use std::fmt::Display;
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use crate::{date::NaiveDateList, io::VoteContainer, IdleError};
use super::Ballot;
/// A pending event, waiting to be opened. In this state the even can be safely modified
/// and either manual input or the autostart date will start the vote.
#[derive(Serialize, Deserialize, Default)]
#[cfg_attr(test, derive(Clone))]
pub struct EventPending {
/// The parsed representation of the vote associated with this event.
/// It holds the details required for the voting process.
pub(super) parsed_vote: ParsedVote,
}
impl EventPending {
pub fn into_ongoing(self) -> EventOngoing {
EventOngoing {
parsed_vote: self.parsed_vote,
}
}
}
#[derive(Serialize, Deserialize)]
#[cfg_attr(test, derive(Clone))]
// An ongoing event, waiting to be closed thus allowing the ballots to be counted.
pub struct EventOngoing {
/// The parsed representation of the vote associated with this event.
/// It holds the details required for the voting process.
pub(super) parsed_vote: ParsedVote,
}
impl Display for EventOngoing {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.parsed_vote.fmt(f)
}
}
impl EventOngoing {
pub fn into_closed(self) -> EventClosed {
EventClosed {
parsed_vote: self.parsed_vote,
}
}
}
#[derive(Serialize, Deserialize)]
#[cfg_attr(test, derive(Clone))]
/// A closed event, while the users cannot interract with it in this state,
/// this event can still be reopened and it's results can be read.
/// All events have an expiry date and in this state the event will be cleaned
/// up after a while.
///
/// Closing an event will call the cleanup and completion functions.
pub struct EventClosed {
/// The parsed representation of the vote associated with this event.
/// It holds the details required for the voting process.
pub(super) parsed_vote: ParsedVote,
}
/// A `ParsedVote` represents a voting event that has been parsed and validated. It
/// contains all necessary information about the voting process itself.
///
/// This structure is specifically used within the application to encapsulate both
/// ongoing and planned voting events, providing a clear and organized way to manage their
/// data throughout their lifecycle.
#[derive(Debug, Serialize, Deserialize, Default)]
#[cfg_attr(test, derive(Clone))]
pub(super) struct ParsedVote {
/// The title of the even
pub(super) title: String,
/// Description of the event
pub(super) description: String,
/// Date at which to autostart the vote process
pub(super) launch_date: NaiveDateTime,
/// Date at which to autoclose the vote process
pub(super) close_date: NaiveDateTime,
/// Date at which to autodelete the vote process
pub(super) auto_delete_date: NaiveDateTime,
/// The inner list of votes cast
pub(super) votes: Vec<Ballot>,
/// The list of values to vote for, this list all the valid dates to vote for
pub(super) list_candidates: NaiveDateList,
}
impl ParsedVote {
/// All the logic behind a vote becoming live
pub(super) fn launch(&mut self) -> Result<(), IdleError> {
// TODO: Define this
// self.status = EventStatus::Started;
// Maybe send a message ? the bot will need the Uuid of the event to be able to add reaction
// TODO: This is a vkypoint, as this method will need an IO interaction
Ok(())
}
}
impl TryFrom<VoteContainer> for ParsedVote {
type Error = IdleError;
fn try_from(value: VoteContainer) -> Result<Self, Self::Error> {
let _ = value;
todo!()
}
}
impl Display for ParsedVote {
/// A display for a vote, it is exactly what the discord bot will send
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// TODO: Complete this (vky)
write!(f, "{:?}", self)
}
}