128 lines
3.6 KiB
Rust
128 lines
3.6 KiB
Rust
//! Handles all votes related logic
|
|
//!
|
|
//! Features:
|
|
//! Structs & Traits
|
|
//! Votes (struct)
|
|
//! Event (struct)
|
|
//! ListCurrentEvents (struct)
|
|
//! ListFutureEvents (struct)
|
|
//!
|
|
#![allow(dead_code, deprecated)]
|
|
|
|
use std::{collections::HashMap, sync::LazyLock};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
use uuid::Uuid;
|
|
|
|
/// Placeholder datatype
|
|
///
|
|
/// Links to an event, active or planned
|
|
type EventDataType = Uuid;
|
|
|
|
/// Placeholder user data type
|
|
///
|
|
/// Links back to an user
|
|
#[deprecated(note = "This is an incorrect type to define an user")]
|
|
type UserDataType = String;
|
|
|
|
/// What an user voted for
|
|
#[deprecated(note = "This is an incorrect type to define a vote")]
|
|
type VoteValueType = String;
|
|
|
|
/// A vote, represents a user interraction towards an event
|
|
#[derive(Serialize, Deserialize)]
|
|
pub(crate) struct Vote {
|
|
/// The event the vote refers to
|
|
linked_event: EventDataType,
|
|
/// The user casting the vote
|
|
user_handle: UserDataType,
|
|
/// What the user voted for
|
|
vote_value: VoteValueType,
|
|
}
|
|
|
|
/// Data type representing a date
|
|
type DateDataType = String;
|
|
|
|
/// The global status of the event
|
|
#[derive(Serialize, Deserialize)]
|
|
pub(crate) enum EventStatus {
|
|
/// The event exists but wasn't started yet
|
|
Pending,
|
|
/// The event was started but the finish date wasn't reached yet
|
|
Started,
|
|
/// The event was started and the date finish date was reached without being closed
|
|
Finished,
|
|
/// The event was closed and the outcome was made public
|
|
Closed,
|
|
/// The event was cancelled
|
|
Cancelled,
|
|
}
|
|
|
|
/// An event, wether ongoing or planned
|
|
#[derive(Serialize, Deserialize)]
|
|
pub(crate) struct Event {
|
|
/// The title of the even
|
|
title: String,
|
|
/// Description of the event
|
|
description: String,
|
|
/// The global status of the vote
|
|
status: EventStatus,
|
|
/// Date at which to autostart the vote process
|
|
launch_date: Option<DateDataType>,
|
|
/// Date at which to autoclose the vote process
|
|
close_date: Option<DateDataType>,
|
|
/// The inner list of votes cast
|
|
votes: Vec<Vote>,
|
|
/// The list of values to vote for, this list all the valid dates to vote for
|
|
vote_values: Vec<DateDataType>,
|
|
}
|
|
|
|
// This split in two differents structs conceptually represents
|
|
// the fact that very differents actions will be performed for each struct
|
|
type EventListType = HashMap<Uuid, Event>;
|
|
|
|
/// The list of pending events, not yet started
|
|
#[derive(Serialize, Deserialize)]
|
|
pub(crate) struct PendingEvents {
|
|
/// The list of pending events
|
|
pending_events: EventListType,
|
|
}
|
|
impl PendingEvents {
|
|
/// Create a new empty list of events, called in case of a first initialization
|
|
///
|
|
/// However a deserialization will always be performed if possible
|
|
fn new() -> Self {
|
|
Self {
|
|
pending_events: EventListType::new(),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The list of ongoing events, not yet closed
|
|
#[derive(Serialize, Deserialize)]
|
|
pub(crate) struct OngoingEvents {
|
|
/// The list of ongoing events
|
|
ongoing_events: EventListType,
|
|
}
|
|
|
|
impl OngoingEvents {
|
|
/// Create a new empty list of ongoing events, called in case of a first initialization
|
|
///
|
|
/// However a deserialisation will always be performed if possible
|
|
fn new() -> Self {
|
|
Self {
|
|
ongoing_events: EventListType::new(),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The thread safe list of pending events
|
|
static CURRENT_EVENTS: LazyLock<PendingEvents> = LazyLock::new(PendingEvents::new);
|
|
/// The thread safe list of ongoing events
|
|
static PENDING_EVENTS: LazyLock<OngoingEvents> = LazyLock::new(OngoingEvents::new);
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
// use super::*;
|
|
}
|