diff --git a/src/votes/helper_functions.rs b/src/votes/helper_functions.rs index f4f0d60..ffd962d 100644 --- a/src/votes/helper_functions.rs +++ b/src/votes/helper_functions.rs @@ -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}; diff --git a/src/votes/holders.rs b/src/votes/holders.rs index 48dfb3a..0059a62 100644 --- a/src/votes/holders.rs +++ b/src/votes/holders.rs @@ -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 { - &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 { - &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 { - &self.closed_events - } } /// A generic event, holding an event and his type as variants, diff --git a/src/votes/mod.rs b/src/votes/mod.rs index 31f6954..f7408c1 100644 --- a/src/votes/mod.rs +++ b/src/votes/mod.rs @@ -19,6 +19,7 @@ mod ballots; mod events; mod helper_functions; mod holders; +mod traits; // Republish pub use ballots::Ballot; diff --git a/src/votes/traits.rs b/src/votes/traits.rs new file mode 100644 index 0000000..a2f3857 --- /dev/null +++ b/src/votes/traits.rs @@ -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 { + /// Borrow the inner ongoing events + fn borrow_inner(&self) -> &HashMap; +} + +impl BorrowInner for ListClosedEvents { + /// Borrow the inner ongoing events + fn borrow_inner(&self) -> &HashMap { + &self.closed_events + } +} + +impl BorrowInner for ListOngoingEvents { + /// Borrow the inner ongoing events + fn borrow_inner(&self) -> &HashMap { + &self.ongoing_events + } +} + +impl BorrowInner for ListPendingEvents { + /// Borrow the inner ongoing events + fn borrow_inner(&self) -> &HashMap { + &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 { + /// Borrow the inner ongoing events + fn borrow_inner(&self) -> &HashMap; +} + +/// Blanket inner dictionnary implementation +/// +/// This trait is meant to allow for inner dictionnary manipulation +pub(crate) trait EventManipulation { + /// get the inner value + fn get_attr(&self, s: &str) -> Option<&R>; +} + +impl EventManipulation for T +where + T: BorrowInner, +{ + fn get_attr(&self, s: &str) -> Option<&R> { + self.borrow_inner().get(s) + } +}