Changes uuid::id to String with human_ids #43
|
|
@ -13,7 +13,6 @@ once_cell = { version = "1.20.2", features = ["std"] }
|
|||
serde = { version = "1.0.214", features = ["alloc", "derive"] }
|
||||
thiserror = "1.0.65"
|
||||
tracing = { version = "0.1.40", features = ["async-await", "std"] }
|
||||
uuid = { version = "1.11.0", features = ["v4", "v7", "serde", "fast-rng"] }
|
||||
simplelog = { version = "0.12.2", features = ["paris"] }
|
||||
log = { version = "0.4.22", features = ["std"] }
|
||||
nom = "7.1.3"
|
||||
|
|
|
|||
|
|
@ -9,7 +9,8 @@ use chrono::Month;
|
|||
use log::SetLoggerError;
|
||||
use poise::serenity_prelude;
|
||||
use thiserror::Error;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::ids::EventIdType;
|
||||
|
||||
/// Global error struct for idlefox
|
||||
///
|
||||
|
|
@ -53,32 +54,32 @@ pub enum IdleError {
|
|||
/// Indicates an event couldn't be launched
|
||||
///
|
||||
/// Raised when there's an issue launching an event usually indicates something went wrong with bookeeping.
|
||||
/// Note that while a UUID is given, it may no longer exist in memory and might only be found in logs.
|
||||
/// Note that while a HUID is given, it may no longer exist in memory and might only be found in logs.
|
||||
#[error("An event couldn't be launched (Id: {0})")]
|
||||
EventLaunch(Uuid),
|
||||
EventLaunch(EventIdType),
|
||||
|
||||
/// Error indicating multiple events couldn't be pruned
|
||||
///
|
||||
/// Raised when one or more events cannot be removed from the ongoing events list.
|
||||
/// This usually indicates a problem with managing and updating the list of ongoing events.
|
||||
/// Note that while a UUID is given, it may no longer exist in memory and might only be found in logs.
|
||||
/// Note that while a HUID is given, it may no longer exist in memory and might only be found in logs.
|
||||
#[error("One or more events couldn't be removed from the ongoing events list (Id(s): {0:?})")]
|
||||
EventPrune(Vec<Uuid>),
|
||||
EventPrune(Vec<EventIdType>),
|
||||
|
||||
/// Error indicating an event could not be deleted
|
||||
///
|
||||
/// This error is raised when attempting to delete an event fails,
|
||||
/// either due to a problem with bookkeping or because the specified UUID
|
||||
/// either due to a problem with bookkeping or because the specified HUID
|
||||
/// does not correspond to any existing events anymore.
|
||||
#[error("An event couldn't be deleted (Id: {0})")]
|
||||
DeleteEvent(Uuid),
|
||||
DeleteEvent(EventIdType),
|
||||
|
||||
/// Error indicating that an ongoing event launch couldn't be performed
|
||||
///
|
||||
/// This error was likely raised as a launch operation for an event was already underway.
|
||||
/// Launching multiple times for the same event may lead to inconsistent states or data corruption.
|
||||
#[error("Couldn't launch event (Id: {0})")]
|
||||
LaunchingEvent(Uuid),
|
||||
LaunchingEvent(EventIdType),
|
||||
|
||||
/// Error indicating a mutex was poisoned or couldn't be acquired
|
||||
///
|
||||
|
|
|
|||
26
src/ids.rs
26
src/ids.rs
|
|
@ -1,13 +1,25 @@
|
|||
//! Handle all Ids functionalities
|
||||
//!
|
||||
//! Features:
|
||||
//! Generating a human readable ID for a UUID
|
||||
//! Finding the UUID from an ID
|
||||
//! Generating a human readable ID for a HUID
|
||||
//! Finding the HUID from an ID
|
||||
//! Removing ID as needed
|
||||
|
||||
// use human_ids::generate;
|
||||
// use std::{env, time::Instant};
|
||||
/// Generic type representing an ID
|
||||
pub type EventIdType = String;
|
||||
|
||||
// let tk = env::var("DISCORD_TOKEN");
|
||||
// let id = generate(None);
|
||||
// println!("Token: {:?} Id: {}", tk, id);
|
||||
/// Generate an id
|
||||
pub fn generate_id() -> String {
|
||||
human_ids::generate(None)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::generate_id;
|
||||
|
||||
#[test]
|
||||
fn test_id_generation() {
|
||||
let id = generate_id();
|
||||
assert!(!id.is_empty())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@
|
|||
pub mod config; // Maybe in lib
|
||||
pub mod date; // Definitely in lib
|
||||
mod error_handling; // Likely in lib
|
||||
mod ids; // Definitely in lib
|
||||
pub mod ids; // Definitely in lib
|
||||
pub mod io; // Maybe in lib
|
||||
mod modules; // Maybe in lib
|
||||
mod persistence; // Likely in lib
|
||||
|
|
|
|||
|
|
@ -110,7 +110,7 @@ impl ParsedVote {
|
|||
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
|
||||
// Maybe send a message ? the bot will need the Huid of the event to be able to add reaction
|
||||
// TODO: This is a vkypoint, as this method will need an IO interaction
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,9 +3,8 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use chrono::NaiveDate;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::IdleError;
|
||||
use crate::{ids::EventIdType, IdleError};
|
||||
|
||||
use super::{
|
||||
events::{EventOngoing, EventPending},
|
||||
|
|
@ -28,7 +27,10 @@ use super::{
|
|||
/// let parsed_vote = EventPending::default();
|
||||
/// let id = add_pending_event(parsed_vote, &ev_ctx).unwrap();
|
||||
/// ```
|
||||
pub fn add_pending_event(event: EventPending, ev_ctx: &EventHolder) -> Result<Uuid, IdleError> {
|
||||
pub fn add_pending_event(
|
||||
event: EventPending,
|
||||
ev_ctx: &EventHolder,
|
||||
) -> Result<EventIdType, IdleError> {
|
||||
ev_ctx
|
||||
.pending_events
|
||||
.lock()
|
||||
|
|
@ -39,10 +41,12 @@ pub fn add_pending_event(event: EventPending, ev_ctx: &EventHolder) -> Result<Uu
|
|||
|
||||
/// Launches a pending event by transferring it from the pending to ongoing events list.
|
||||
///
|
||||
/// This function identifies an event by its UUID and moves it from the planned (pending) events to the ongoing events. If the event is successfully launched, it returns `Ok(())`. Otherwise, it returns an error of type `IdleError`.
|
||||
/// This function identifies an event by its HUID and moves it from the planned
|
||||
/// (pending) events to the ongoing events. If the event is successfully launched, it returns `Ok(())`.
|
||||
/// Otherwise, it returns an error of type `IdleError`.
|
||||
///
|
||||
/// ## Parameters:
|
||||
/// - `uuid`: A `Uuid` that uniquely identifies the event to be launched.
|
||||
/// - `Huid`: A `Huid` that uniquely identifies the event to be launched.
|
||||
///
|
||||
/// ## Example:
|
||||
/// ```rust
|
||||
|
|
@ -50,39 +54,38 @@ pub fn add_pending_event(event: EventPending, ev_ctx: &EventHolder) -> Result<Uu
|
|||
/// let ev_ctx = EventHolder::default();
|
||||
/// let id = add_pending_event(EventPending::default(), &ev_ctx).unwrap();
|
||||
///
|
||||
/// make_into_ongoing_event(id, &ev_ctx).unwrap()
|
||||
/// make_into_ongoing_event(&id, &ev_ctx).unwrap()
|
||||
/// ```
|
||||
pub fn make_into_ongoing_event(uuid: Uuid, ev_ctx: &EventHolder) -> Result<(), IdleError> {
|
||||
pub fn make_into_ongoing_event(huid: &EventIdType, ev_ctx: &EventHolder) -> Result<(), IdleError> {
|
||||
ev_ctx
|
||||
.pending_events
|
||||
.lock()
|
||||
.unwrap()
|
||||
// .map_err(|_| IdleError::MutexError)?
|
||||
.launch(uuid, ev_ctx)
|
||||
.launch(huid, ev_ctx)
|
||||
}
|
||||
|
||||
/// Manually adds an ongoing event.
|
||||
///
|
||||
/// This function is intended for internal use and directly adds an event
|
||||
/// to the list of ongoing events. It expects a UUID and a parsed vote as inputs,
|
||||
/// to the list of ongoing events. It expects a HUID and a parsed vote as inputs,
|
||||
/// using which it updates the `OngoingEvents` instance.
|
||||
///
|
||||
/// ## Parameters:
|
||||
/// - `uuid`: A `Uuid` that uniquely identifies the event to be added.
|
||||
/// - `huid`: A `Huid` that uniquely identifies the event to be added.
|
||||
/// - `event`: A `ParsedVote` struct representing the details of the ongoing event to be added.
|
||||
///
|
||||
/// ## Example:
|
||||
/// ```rust
|
||||
/// # use uuid::Uuid;
|
||||
/// use idlefox::votes::{add_ongoing_event, EventPending, EventHolder};
|
||||
/// use idlefox::ids::generate_id;
|
||||
/// use idlefox::votes::{add_ongoing_event, EventHolder, EventPending};
|
||||
/// let ev_ctx = EventHolder::default();
|
||||
/// let uuid = Uuid::new_v4(); // A new id
|
||||
/// let huid = generate_id(); // A new id
|
||||
/// let ongoing = EventPending::default().into_ongoing(); // A very legit event
|
||||
///
|
||||
/// add_ongoing_event(uuid, ongoing, &ev_ctx).unwrap();
|
||||
/// add_ongoing_event(&huid, ongoing, &ev_ctx).unwrap();
|
||||
/// ```
|
||||
pub fn add_ongoing_event(
|
||||
uuid: Uuid,
|
||||
huid: &EventIdType,
|
||||
event: EventOngoing,
|
||||
ev_ctx: &EventHolder,
|
||||
) -> Result<(), IdleError> {
|
||||
|
|
@ -91,14 +94,14 @@ pub fn add_ongoing_event(
|
|||
.lock()
|
||||
.unwrap()
|
||||
// .map_err(|_| IdleError::MutexError)?
|
||||
.add_event(uuid, event)?;
|
||||
.add_event(huid, event)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Destroy an event without closing it and officially ending it
|
||||
///
|
||||
/// This function removes an ongoing event from the list of ongoing events.
|
||||
/// It expects a UUID as input and returns a `Result` containing either a `ParsedVote`
|
||||
/// It expects a HUID as input and returns a `Result` containing either a `ParsedVote`
|
||||
/// struct representing the details of the removed event or an `IdleError`.
|
||||
///
|
||||
/// ## Example:
|
||||
|
|
@ -108,23 +111,26 @@ pub fn add_ongoing_event(
|
|||
/// # };
|
||||
/// let ev_ctx = EventHolder::default();
|
||||
/// let id = add_pending_event(EventPending::default(), &ev_ctx).unwrap();
|
||||
/// make_into_ongoing_event(id, &ev_ctx).unwrap();
|
||||
/// make_into_ongoing_event(&id, &ev_ctx).unwrap();
|
||||
///
|
||||
/// del_ongoing_event(id, &ev_ctx).unwrap();
|
||||
/// del_ongoing_event(&id, &ev_ctx).unwrap();
|
||||
/// ```
|
||||
pub fn del_ongoing_event(uuid: Uuid, ev_ctx: &EventHolder) -> Result<EventOngoing, IdleError> {
|
||||
pub fn del_ongoing_event(
|
||||
huid: &EventIdType,
|
||||
ev_ctx: &EventHolder,
|
||||
) -> Result<EventOngoing, IdleError> {
|
||||
ev_ctx
|
||||
.ongoing_events
|
||||
.lock()
|
||||
.unwrap()
|
||||
// .map_err(|_| IdleError::MutexError)?
|
||||
.del_event(uuid)
|
||||
.del_event(huid)
|
||||
}
|
||||
|
||||
/// Destroy a pending event without closing it
|
||||
///
|
||||
/// This function removes a pending event from the list of pending events.
|
||||
/// It expects a UUID as input and returns a `Result` containing either a `ParsedVote`
|
||||
/// It expects a HUID as input and returns a `Result` containing either a `ParsedVote`
|
||||
/// struct representing the details of the removed event or an `IdleError`.
|
||||
///
|
||||
/// ## Example:
|
||||
|
|
@ -134,21 +140,24 @@ pub fn del_ongoing_event(uuid: Uuid, ev_ctx: &EventHolder) -> Result<EventOngoin
|
|||
/// # };
|
||||
/// let ev_ctx = EventHolder::default();
|
||||
/// let id = add_pending_event(EventPending::default(), &ev_ctx).unwrap();
|
||||
/// del_pending_event(id, &ev_ctx).unwrap();
|
||||
/// del_pending_event(&id, &ev_ctx).unwrap();
|
||||
/// ```
|
||||
pub fn del_pending_event(uuid: Uuid, ev_ctx: &EventHolder) -> Result<EventPending, IdleError> {
|
||||
pub fn del_pending_event(
|
||||
huid: &EventIdType,
|
||||
ev_ctx: &EventHolder,
|
||||
) -> Result<EventPending, IdleError> {
|
||||
ev_ctx
|
||||
.pending_events
|
||||
.lock()
|
||||
.unwrap()
|
||||
// .map_err(|_| IdleError::MutexError)?
|
||||
.del_event(uuid)
|
||||
.del_event(huid)
|
||||
}
|
||||
|
||||
/// This function is used to close an ongoing event and return the parsed vote associated with it. If the closure is successful, the owned event is returned for post-treatment.
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `uuid` - A Uuid representing the identifier of the event that is to be closed.
|
||||
/// * `huid` - A Huid representing the identifier of the event that is to be closed.
|
||||
///
|
||||
/// ## Example:
|
||||
/// ```rust
|
||||
|
|
@ -157,16 +166,16 @@ pub fn del_pending_event(uuid: Uuid, ev_ctx: &EventHolder) -> Result<EventPendin
|
|||
/// # };
|
||||
/// let ev_ctx = EventHolder::default();
|
||||
/// let id = add_pending_event(EventPending::default(), &ev_ctx).unwrap();
|
||||
/// make_into_ongoing_event(id, &ev_ctx).unwrap();
|
||||
/// close_ongoing_event(id, &ev_ctx).unwrap();
|
||||
/// make_into_ongoing_event(&id, &ev_ctx).unwrap();
|
||||
/// close_ongoing_event(&id, &ev_ctx).unwrap();
|
||||
/// ```
|
||||
pub fn close_ongoing_event(uuid: Uuid, ev_ctx: &EventHolder) -> Result<(), IdleError> {
|
||||
pub fn close_ongoing_event(huid: &EventIdType, ev_ctx: &EventHolder) -> Result<(), IdleError> {
|
||||
ev_ctx
|
||||
.ongoing_events
|
||||
.lock()
|
||||
.unwrap()
|
||||
// .map_err(|_| IdleError::MutexError)?
|
||||
.close_event(uuid, ev_ctx)
|
||||
.close_event(huid, ev_ctx)
|
||||
}
|
||||
|
||||
/// A link to all events, to be shared between threads related to this event pool.
|
||||
|
|
|
|||
|
|
@ -13,9 +13,11 @@
|
|||
//! actions such as adding, launching, processing, and autoclosing events.
|
||||
use chrono::NaiveDateTime;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::IdleError;
|
||||
use crate::{
|
||||
ids::{generate_id, EventIdType},
|
||||
IdleError,
|
||||
};
|
||||
|
||||
use super::{
|
||||
add_ongoing_event,
|
||||
|
|
@ -35,10 +37,13 @@ pub struct ListPendingEvents {
|
|||
impl ListPendingEvents {
|
||||
/// Add a new event to the list of upcoming events.
|
||||
///
|
||||
/// This function generates a new UUID for the event before adding it to the list.
|
||||
pub(super) fn add_pending_event(&mut self, event: EventPending) -> Result<Uuid, IdleError> {
|
||||
let id = Uuid::new_v4();
|
||||
self.pending_events.insert(id, event);
|
||||
/// This function generates a new HUID for the event before adding it to the list.
|
||||
pub(super) fn add_pending_event(
|
||||
&mut self,
|
||||
event: EventPending,
|
||||
) -> Result<EventIdType, IdleError> {
|
||||
let id = generate_id();
|
||||
self.pending_events.insert(id.clone(), event);
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
|
|
@ -52,8 +57,8 @@ impl ListPendingEvents {
|
|||
) -> Result<(), IdleError> {
|
||||
self.find_upcoming(current_time)
|
||||
.iter()
|
||||
.try_for_each(|uuid| {
|
||||
self.launch(*uuid, ev_ctx)?;
|
||||
.try_for_each(|huid| {
|
||||
self.launch(huid, ev_ctx)?;
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
|
@ -61,31 +66,36 @@ impl ListPendingEvents {
|
|||
/// Launch the specified event by moving it from pending events to ongoing events.
|
||||
///
|
||||
/// Returns an `IdleError` if the event cannot be found in pending events.
|
||||
pub(super) fn launch(&mut self, uuid: Uuid, ev_ctx: &EventHolder) -> Result<(), IdleError> {
|
||||
pub(super) fn launch(
|
||||
&mut self,
|
||||
huid: &EventIdType,
|
||||
ev_ctx: &EventHolder,
|
||||
) -> Result<(), IdleError> {
|
||||
let event = self
|
||||
.pending_events
|
||||
.remove(&uuid)
|
||||
.ok_or(IdleError::EventLaunch(uuid))?;
|
||||
add_ongoing_event(uuid, event.into_ongoing(), ev_ctx)?;
|
||||
// make_into_ongoing_event(uuid)?;
|
||||
.remove(huid)
|
||||
.ok_or(IdleError::EventLaunch(huid.to_string()))?;
|
||||
add_ongoing_event(huid, event.into_ongoing(), ev_ctx)?;
|
||||
// make_into_ongoing_event(huid)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Find all events that are scheduled to start after the given date.
|
||||
///
|
||||
/// Returns a vector of UUIDs corresponding to the upcoming events.
|
||||
pub(super) fn find_upcoming(&self, current_time: NaiveDateTime) -> Vec<Uuid> {
|
||||
/// Returns a vector of HUIDs corresponding to the upcoming events.
|
||||
/// TODO: Consider returning references
|
||||
pub(super) fn find_upcoming(&self, current_time: NaiveDateTime) -> Vec<EventIdType> {
|
||||
self.pending_events
|
||||
.iter()
|
||||
.filter(|&(_, e)| e.parsed_vote.launch_date <= current_time)
|
||||
.map(|(k, _v)| (*k))
|
||||
.map(|(k, _v)| (k.clone()))
|
||||
.collect()
|
||||
}
|
||||
/// delete the event without launching it
|
||||
pub fn del_event(&mut self, uuid: Uuid) -> Result<EventPending, IdleError> {
|
||||
pub fn del_event(&mut self, huid: &EventIdType) -> Result<EventPending, IdleError> {
|
||||
self.pending_events
|
||||
.remove(&uuid)
|
||||
.ok_or(IdleError::DeleteEvent(uuid))
|
||||
.remove(huid)
|
||||
.ok_or(IdleError::DeleteEvent(huid.to_string()))
|
||||
}
|
||||
|
||||
// TODO: maybe add an update_upcoming(date) ?
|
||||
|
|
@ -103,10 +113,14 @@ pub struct ListOngoingEvents {
|
|||
impl ListOngoingEvents {
|
||||
/// Add a new event to the list of ongoing events.
|
||||
///
|
||||
/// This function also launch the event based and adds a callback with the Uuid of the original caller.
|
||||
pub(crate) fn add_event(&mut self, id: Uuid, mut event: EventOngoing) -> Result<(), IdleError> {
|
||||
/// This function also launch the event based and adds a callback with the Huid of the original caller.
|
||||
pub(crate) fn add_event(
|
||||
&mut self,
|
||||
huid: &EventIdType,
|
||||
mut event: EventOngoing,
|
||||
) -> Result<(), IdleError> {
|
||||
event.parsed_vote.launch()?;
|
||||
self.ongoing_events.insert(id, event);
|
||||
self.ongoing_events.insert(huid.clone(), event);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
@ -119,8 +133,8 @@ impl ListOngoingEvents {
|
|||
ev_ctx: &EventHolder,
|
||||
) -> Result<(), IdleError> {
|
||||
let failure = Vec::new();
|
||||
for uuid in self.find_autoclose(current_time) {
|
||||
self.close_event(uuid, ev_ctx)?;
|
||||
for huid in self.find_autoclose(current_time) {
|
||||
self.close_event(&huid, ev_ctx)?;
|
||||
}
|
||||
match failure.is_empty() {
|
||||
true => Ok(()),
|
||||
|
|
@ -134,35 +148,39 @@ impl ListOngoingEvents {
|
|||
/// puting it into the closed event list.
|
||||
///
|
||||
/// Doing so allow for the vote result to be read while it is valid
|
||||
pub fn close_event(&mut self, uuid: Uuid, ev_ctx: &EventHolder) -> Result<(), IdleError> {
|
||||
pub fn close_event(
|
||||
&mut self,
|
||||
huid: &EventIdType,
|
||||
ev_ctx: &EventHolder,
|
||||
) -> Result<(), IdleError> {
|
||||
// TODO: Another vkypoint, as we cant continue without knowing how to send a message
|
||||
let event = self
|
||||
.ongoing_events
|
||||
.remove(&uuid)
|
||||
.ok_or(IdleError::LaunchingEvent(uuid))?;
|
||||
.remove(huid)
|
||||
.ok_or(IdleError::LaunchingEvent(huid.to_string()))?;
|
||||
ev_ctx
|
||||
.closed_events
|
||||
.lock()
|
||||
.unwrap()
|
||||
.add_event(uuid, event.into_closed())?;
|
||||
.add_event(huid.clone(), event.into_closed())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// delete the event without launching it
|
||||
pub fn del_event(&mut self, uuid: Uuid) -> Result<EventOngoing, IdleError> {
|
||||
pub fn del_event(&mut self, huid: &EventIdType) -> Result<EventOngoing, IdleError> {
|
||||
self.ongoing_events
|
||||
.remove(&uuid)
|
||||
.ok_or(IdleError::DeleteEvent(uuid))
|
||||
.remove(huid)
|
||||
.ok_or(IdleError::DeleteEvent(huid.to_string()))
|
||||
}
|
||||
|
||||
/// Find all events that are scheduled to be autoclosed.
|
||||
///
|
||||
/// Returns a vector of UUIDs corresponding to the old events.
|
||||
pub(super) fn find_autoclose(&mut self, current_time: NaiveDateTime) -> Vec<Uuid> {
|
||||
/// Returns a vector of HUIDs corresponding to the old events.
|
||||
pub(super) fn find_autoclose(&mut self, current_time: NaiveDateTime) -> Vec<EventIdType> {
|
||||
self.ongoing_events
|
||||
.iter()
|
||||
.filter(|(_, e)| e.parsed_vote.close_date <= current_time)
|
||||
.map(|(k, _)| *k)
|
||||
.map(|(k, _)| k.clone())
|
||||
.collect()
|
||||
}
|
||||
// TODO: maybe add an update_ongoing(date) ?
|
||||
|
|
@ -180,19 +198,19 @@ pub struct ListClosedEvents {
|
|||
|
||||
impl ListClosedEvents {
|
||||
/// Add a closed event to the list of closed events
|
||||
pub fn add_event(&mut self, uuid: Uuid, event: EventClosed) -> Result<(), IdleError> {
|
||||
self.closed_events.insert(uuid, event);
|
||||
pub fn add_event(&mut self, huid: EventIdType, event: EventClosed) -> Result<(), IdleError> {
|
||||
self.closed_events.insert(huid, event);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Find all events that are too old and supposed to be pruned.
|
||||
///
|
||||
/// Returns a vector of UUIDs corresponding to the old events.
|
||||
pub(super) fn find_old(&mut self, current_time: NaiveDateTime) -> Vec<Uuid> {
|
||||
/// Returns a vector of HUIDs corresponding to the old events.
|
||||
pub(super) fn find_old(&mut self, current_time: NaiveDateTime) -> Vec<EventIdType> {
|
||||
self.closed_events
|
||||
.iter()
|
||||
.filter(|(_, e)| e.parsed_vote.auto_delete_date <= current_time)
|
||||
.map(|(k, _)| *k)
|
||||
.map(|(k, _)| k.clone())
|
||||
.collect()
|
||||
}
|
||||
/// Remove all outdated votes for the current user
|
||||
|
|
@ -200,12 +218,12 @@ impl ListClosedEvents {
|
|||
/// This function is expected to be called periodically by the watchdog.
|
||||
pub(crate) fn prune_old(&mut self, current_time: NaiveDateTime) -> Result<(), IdleError> {
|
||||
let mut failure = Vec::new();
|
||||
for uuid in self.find_old(current_time) {
|
||||
println!("{}", uuid);
|
||||
if self.closed_events.remove(&uuid).is_none() {
|
||||
for huid in self.find_old(current_time) {
|
||||
println!("{}", huid);
|
||||
if self.closed_events.remove(&huid).is_none() {
|
||||
// Should only happen during hard to reproduce race conditions
|
||||
#[cfg(not(tarpaulin_include))]
|
||||
failure.push(uuid);
|
||||
failure.push(huid.clone());
|
||||
}
|
||||
}
|
||||
match failure.is_empty() {
|
||||
|
|
@ -248,20 +266,20 @@ mod tests {
|
|||
// Can we add a vote ?
|
||||
let id = add_pending_event(ev.clone(), &ev_ctx).unwrap();
|
||||
// assert!(gimme!(ev_ctx.pending_events).contains_key(&id)); // Is the vote present ?
|
||||
del_pending_event(id, &ev_ctx).unwrap();
|
||||
del_pending_event(&id, &ev_ctx).unwrap();
|
||||
assert!(!g!(ev_ctx.pending_events).pending_events.contains_key(&id)); // Is the vote now deleted
|
||||
|
||||
let id = add_pending_event(ev.clone(), &ev_ctx).unwrap();
|
||||
make_into_ongoing_event(id, &ev_ctx).unwrap(); // Launching this event
|
||||
make_into_ongoing_event(&id, &ev_ctx).unwrap(); // Launching this event
|
||||
assert!(!g!(ev_ctx.pending_events).pending_events.contains_key(&id)); // Is the vote now not pending
|
||||
assert!(g!(ev_ctx.ongoing_events).ongoing_events.contains_key(&id)); // Is the vote now ongoing
|
||||
|
||||
close_ongoing_event(id, &ev_ctx).unwrap();
|
||||
close_ongoing_event(&id, &ev_ctx).unwrap();
|
||||
assert!(!g!(ev_ctx.ongoing_events).ongoing_events.contains_key(&id)); // Is the vote now not ongoing
|
||||
|
||||
let id = add_pending_event(ev.clone(), &ev_ctx).unwrap();
|
||||
make_into_ongoing_event(id, &ev_ctx).unwrap(); // Launching this event
|
||||
del_ongoing_event(id, &ev_ctx).unwrap();
|
||||
make_into_ongoing_event(&id, &ev_ctx).unwrap(); // Launching this event
|
||||
del_ongoing_event(&id, &ev_ctx).unwrap();
|
||||
assert!(!g!(ev_ctx.ongoing_events).ongoing_events.contains_key(&id)); // Is the vote now not ongoing
|
||||
}
|
||||
|
||||
|
|
@ -298,7 +316,7 @@ mod tests {
|
|||
assert!(!g!(ev.closed_events).closed_events.contains_key(&id)); // not deleted
|
||||
|
||||
// Now some time passed
|
||||
assert_eq!(g!(ev.pending_events).find_upcoming(later), vec![id]); // one upcomming event
|
||||
assert_eq!(g!(ev.pending_events).find_upcoming(later), vec![id.clone()]); // one upcomming event
|
||||
assert!(g!(ev.closed_events).find_old(later).is_empty()); // no old events
|
||||
assert!(g!(ev.ongoing_events).find_autoclose(later).is_empty()); // no old events
|
||||
g!(ev.pending_events).process_upcoming(later, &ev).unwrap(); // one event should be processed
|
||||
|
|
@ -310,7 +328,10 @@ mod tests {
|
|||
|
||||
// More time passed
|
||||
assert!(g!(ev.pending_events).find_upcoming(very_later).is_empty()); // one upcomming event
|
||||
assert_eq!(g!(ev.ongoing_events).find_autoclose(very_later), vec![id]); // one old event
|
||||
assert_eq!(
|
||||
g!(ev.ongoing_events).find_autoclose(very_later),
|
||||
vec![id.clone()]
|
||||
); // one old event
|
||||
assert!(g!(ev.closed_events).find_old(very_later).is_empty()); // no old events
|
||||
g!(ev.pending_events)
|
||||
.process_upcoming(very_later, &ev)
|
||||
|
|
@ -325,7 +346,10 @@ mod tests {
|
|||
assert!(g!(ev.pending_events)
|
||||
.find_upcoming(very_very_later)
|
||||
.is_empty()); // one upcomming event
|
||||
assert_eq!(g!(ev.closed_events).find_old(very_very_later), vec![id]); // one old event
|
||||
assert_eq!(
|
||||
g!(ev.closed_events).find_old(very_very_later),
|
||||
vec![id.clone()]
|
||||
); // one old event
|
||||
assert!(g!(ev.ongoing_events)
|
||||
.find_autoclose(very_very_later)
|
||||
.is_empty()); // no old events
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ pub use holders::{ListOngoingEvents, ListPendingEvents};
|
|||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use uuid::Uuid;
|
||||
use crate::ids::EventIdType;
|
||||
|
||||
/// The hashmap of pending votes
|
||||
type PendingHashmap = HashMap<EventIdType, EventPending>;
|
||||
|
|
@ -38,11 +38,6 @@ type OngoingHashmap = HashMap<EventIdType, EventOngoing>;
|
|||
/// The hashmap of closed votes, still available for reading to check the results
|
||||
type ClosedHashmap = HashMap<EventIdType, EventClosed>;
|
||||
|
||||
/// Placeholder datatype
|
||||
///
|
||||
/// Links to an event, active or planned
|
||||
pub type EventIdType = Uuid;
|
||||
|
||||
/// Placeholder user data type
|
||||
///
|
||||
/// Links back to an user
|
||||
|
|
|
|||
|
|
@ -3,12 +3,12 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn generic_integration_test() {
|
||||
use idlefox::votes::{
|
||||
add_pending_event, make_into_ongoing_event, EventHolder, EventPending,
|
||||
};
|
||||
use idlefox::ids::generate_id;
|
||||
use idlefox::votes::{add_ongoing_event, EventHolder, EventPending};
|
||||
let ev_ctx = EventHolder::default();
|
||||
let id = add_pending_event(EventPending::default(), &ev_ctx).unwrap();
|
||||
let huid = generate_id(); // A new id
|
||||
let ongoing = EventPending::default().into_ongoing(); // A very legit event
|
||||
|
||||
make_into_ongoing_event(id, &ev_ctx).unwrap()
|
||||
add_ongoing_event(&huid, ongoing, &ev_ctx).unwrap();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue