From 7c6fee6914e251d738b5aa9a912181291daf945d Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Tue, 29 Oct 2024 16:09:56 +0100 Subject: [PATCH] Adds a config file and implements a base votes structs --- .gitignore | 4 ++ Cargo.toml | 2 + src/config.rs | 17 ++++++++ src/main.rs | 2 + src/votes.rs | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 134 insertions(+) create mode 100644 src/config.rs diff --git a/.gitignore b/.gitignore index 193d30e..a470081 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,7 @@ Cargo.lock # Added by cargo /target + +# The discord token + +.token diff --git a/Cargo.toml b/Cargo.toml index 8b2bd28..d9d3d76 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,8 @@ edition = "2021" poise = "0.6.1" poise_macros = "0.6.1" tokio = { version = "1.41.0", features = ["rt-multi-thread"] } +serde = { version = "1.0.214", features = ["alloc", "derive"] } +uuid = { version = "1.11.0", features = ["v4", "v7", "serde", "fast-rng"] } [profile.dev] debug = 0 diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..297f410 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,17 @@ +//! Handles all config related functions +//! +//! Tries to load a config from .idlefox.toml +//! and creates a default version if it fails to do so +//! +//! Features: +//! Create, loads and saves config +//! Default config +//! Import old data from outdated configs +//! Recover from importing faulty data +//! + +use serde::{Deserialize, Serialize}; + +/// The global config +#[derive(Serialize, Deserialize)] +pub(crate) struct GlobalConfig {} diff --git a/src/main.rs b/src/main.rs index 1275e60..eec1c79 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ // This line is here to force the use of extensive documentation #![warn(clippy::missing_docs_in_private_items)] +mod config; mod io; mod modules; mod persistence; @@ -142,6 +143,7 @@ pub async fn component_modal(ctx: crate::Context<'_>) -> Result<(), Error> { Ok(()) } +#[cfg(not(tarpaulin_include))] #[tokio::main] async fn main() { let token = std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN"); diff --git a/src/votes.rs b/src/votes.rs index 25e5c98..1dd7052 100644 --- a/src/votes.rs +++ b/src/votes.rs @@ -8,6 +8,115 @@ //! ListFutureEvents (struct) //! +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, + /// Date at which to autoclose the vote process + close_date: Option, + /// The inner list of votes cast + votes: Vec, + /// The list of values to vote for, this list all the valid dates to vote for + vote_values: Vec, +} + +// This split in two differents structs conceptually represents +// the fact that very differents actions will be performed for each struct +type EventListType = HashMap; + +/// 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 { + fn new() -> Self { + Self { + ongoing_events: EventListType::new(), + } + } +} + +/// The thread safe list of pending events +static CURRENT_EVENTS: LazyLock = LazyLock::new(PendingEvents::new); +/// The thread safe list of ongoing events +static PENDING_EVENTS: LazyLock = LazyLock::new(OngoingEvents::new); + #[cfg(test)] mod tests { // use super::*;