Adds a blanket implementation and a trait for Borrowable inner dictionnary

This commit is contained in:
Dandaedre 2024-12-05 04:56:59 -06:00
parent 9a671a16e7
commit 0e3a7fb3ad
4 changed files with 68 additions and 20 deletions

View File

@ -1,5 +1,6 @@
//! Submodule including all helper functions to handle vote creation and evolution
//!
use crate::votes::traits::BorrowInner;
use chrono::NaiveDate;
use std::{fs::File, sync::Arc};

View File

@ -12,8 +12,6 @@
//! application, ensuring separation between pending and ongoing events and supporting
//! actions such as adding, launching, processing, and autoclosing events.
use std::collections::HashMap;
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
@ -35,7 +33,7 @@ use super::{
#[cfg_attr(test, derive(Debug, PartialEq, Clone))]
pub struct ListPendingEvents {
/// The list of pending events
pending_events: PendingHashmap,
pub(super) pending_events: PendingHashmap,
}
impl ListPendingEvents {
@ -104,11 +102,6 @@ impl ListPendingEvents {
// pub fn contains_event(&self, id: &str) -> bool {
// self.pending_events.contains_key(id)
// }
/// Borrow the inner pending events
pub fn borrow_inner(&self) -> &HashMap<String, EventPending> {
&self.pending_events
}
}
/// The list of ongoing events, not yet closed
@ -118,7 +111,7 @@ impl ListPendingEvents {
#[cfg_attr(test, derive(Debug, PartialEq))]
pub struct ListOngoingEvents {
/// The list of ongoing events
ongoing_events: OngoingHashmap,
pub(super) ongoing_events: OngoingHashmap,
}
impl ListOngoingEvents {
@ -194,11 +187,6 @@ impl ListOngoingEvents {
.map(|(k, _)| k.clone())
.collect()
}
/// Borrow the inner ongoing events
pub fn borrow_inner(&self) -> &HashMap<String, EventOngoing> {
&self.ongoing_events
}
}
/// The list of closed events
@ -208,7 +196,7 @@ impl ListOngoingEvents {
#[derive(Serialize, Deserialize, Default)]
pub struct ListClosedEvents {
/// The list of closed events
closed_events: ClosedHashmap,
pub(super) closed_events: ClosedHashmap,
}
impl ListClosedEvents {
@ -247,11 +235,6 @@ impl ListClosedEvents {
false => Err(IdleError::EventPrune(failure)),
}
}
/// Borrow the inner ongoing events
pub fn borrow_inner(&self) -> &HashMap<String, EventClosed> {
&self.closed_events
}
}
/// A generic event, holding an event and his type as variants,

View File

@ -19,6 +19,7 @@ mod ballots;
mod events;
mod helper_functions;
mod holders;
mod traits;
// Republish
pub use ballots::Ballot;

63
src/votes/traits.rs Normal file
View File

@ -0,0 +1,63 @@
//! Module holding all inner voting traits
#![allow(dead_code)]
use std::collections::HashMap;
use super::{
holders::ListClosedEvents, EventClosed, EventOngoing, EventPending, ListOngoingEvents,
ListPendingEvents,
};
/// Signals that the object will let you borrow an inner dictionnary
///
/// Doing so will allow blanket implementations great for generic dictionnary handling
pub(crate) trait BorrowInner<R> {
/// Borrow the inner ongoing events
fn borrow_inner(&self) -> &HashMap<String, R>;
}
impl BorrowInner<EventClosed> for ListClosedEvents {
/// Borrow the inner ongoing events
fn borrow_inner(&self) -> &HashMap<String, EventClosed> {
&self.closed_events
}
}
impl BorrowInner<EventOngoing> for ListOngoingEvents {
/// Borrow the inner ongoing events
fn borrow_inner(&self) -> &HashMap<String, EventOngoing> {
&self.ongoing_events
}
}
impl BorrowInner<EventPending> for ListPendingEvents {
/// Borrow the inner ongoing events
fn borrow_inner(&self) -> &HashMap<String, EventPending> {
&self.pending_events
}
}
/// Signals that the object will let you mutably borrow an inner dictionnary
///
/// Doing so will allow blanket implementations great for generic dictionnary handling
pub(crate) trait BorrowMutInner<R> {
/// Borrow the inner ongoing events
fn borrow_inner(&self) -> &HashMap<String, R>;
}
/// Blanket inner dictionnary implementation
///
/// This trait is meant to allow for inner dictionnary manipulation
pub(crate) trait EventManipulation<R> {
/// get the inner value
fn get_attr(&self, s: &str) -> Option<&R>;
}
impl<T, R> EventManipulation<R> for T
where
T: BorrowInner<R>,
{
fn get_attr(&self, s: &str) -> Option<&R> {
self.borrow_inner().get(s)
}
}