Adds a mock of (untested) persistence
This commit is contained in:
parent
bbd008b092
commit
e179620113
|
|
@ -1 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="122" height="18" role="img" aria-label="test-coverage: 84%"><title>test-coverage: 84%</title><linearGradient id="bms-c15e65abae712a1a" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-color="#000" stop-opacity=".3"/><stop offset="1" stop-color="#000" stop-opacity=".5"/></linearGradient><clipPath id="bmr-c15e65abae712a1a"><rect width="122" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#bmr-c15e65abae712a1a)"><rect width="87" height="18" fill="#555"/><rect x="87" width="35" height="18" fill="#fe7d37"/><rect width="122" height="18" fill="url(#bms-c15e65abae712a1a)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="445" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="770">test-coverage</text><text x="445" y="130" transform="scale(.1)" fill="#fff" textLength="770">test-coverage</text><text aria-hidden="true" x="1035" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="250">84%</text><text x="1035" y="130" transform="scale(.1)" fill="#fff" textLength="250">84%</text></g></svg>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="122" height="18" role="img" aria-label="test-coverage: 75%"><title>test-coverage: 75%</title><linearGradient id="bms-10c887a79af584fb" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-color="#000" stop-opacity=".3"/><stop offset="1" stop-color="#000" stop-opacity=".5"/></linearGradient><clipPath id="bmr-10c887a79af584fb"><rect width="122" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#bmr-10c887a79af584fb)"><rect width="87" height="18" fill="#555"/><rect x="87" width="35" height="18" fill="#e05d44"/><rect width="122" height="18" fill="url(#bms-10c887a79af584fb)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="445" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="770">test-coverage</text><text x="445" y="130" transform="scale(.1)" fill="#fff" textLength="770">test-coverage</text><text aria-hidden="true" x="1035" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="250">75%</text><text x="1035" y="130" transform="scale(.1)" fill="#fff" textLength="250">75%</text></g></svg>
|
||||
|
|
|
|||
|
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |
|
|
@ -103,6 +103,12 @@ pub enum IdleError {
|
|||
/// This error will happen when an incorrect string is sent to a command invocation
|
||||
#[error("An error occured while trying to parse a Discord command")]
|
||||
DiscordContainerParse,
|
||||
/// Indicate a backup coouldn't be done, usually means the file descriptor is busy or permission are incorrect
|
||||
#[error("An error occured while trying to save file {0}")]
|
||||
BackupError(std::io::Error),
|
||||
/// Indicate a value couldn't be serialized
|
||||
#[error("An error occured while trying to serialize data {0}")]
|
||||
SerdeError(serde_json::Error),
|
||||
}
|
||||
|
||||
impl From<nom::Err<nom::error::Error<&str>>> for IdleError {
|
||||
|
|
|
|||
|
|
@ -6,6 +6,36 @@
|
|||
//!
|
||||
//!
|
||||
|
||||
use crate::IdleError;
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
use std::{fs::File, io::Write, path::Path};
|
||||
|
||||
/// Tries to load the list of pending events from disk or return a default (empty) list
|
||||
pub(crate) fn load_from_disk<T>(path: &str) -> T
|
||||
where
|
||||
T: Default + DeserializeOwned,
|
||||
{
|
||||
match File::open(&path) {
|
||||
Ok(f) => serde_json::from_reader(f).unwrap_or_default(),
|
||||
Err(_) => T::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Tries to save the list to disk and return an error on failure
|
||||
pub(crate) fn save_to_disk<T>(data: T, path: &str) -> Result<(), IdleError>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
let mut f = File::create(path).map_err(IdleError::BackupError)?;
|
||||
f.write(
|
||||
serde_json::to_string_pretty(&data)
|
||||
.map_err(IdleError::SerdeError)?
|
||||
.as_bytes(),
|
||||
)
|
||||
.map_err(IdleError::BackupError)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
// use super::*;
|
||||
|
|
|
|||
|
|
@ -4,6 +4,10 @@
|
|||
//! Show all votes (pending and happening)
|
||||
//! Allow vote closing
|
||||
//!
|
||||
// warp for creating the API
|
||||
// Tokio to run an asynchronous server
|
||||
// Serde to help serialize incoming JSON
|
||||
// parking_lot to create a ReadWriteLock for your local storage
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
|
|
|||
|
|
@ -1,16 +1,21 @@
|
|||
//! Submodule including all helper functions to handle vote creation and evolution
|
||||
//!
|
||||
use std::sync::Arc;
|
||||
|
||||
use chrono::NaiveDate;
|
||||
|
||||
use crate::{ids::EventIdType, IdleError};
|
||||
use std::{
|
||||
ops::{Deref, DerefMut},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use super::{
|
||||
events::{EventOngoing, EventPending},
|
||||
holders::ListClosedEvents,
|
||||
ListOngoingEvents, ListPendingEvents,
|
||||
};
|
||||
use crate::{
|
||||
ids::EventIdType,
|
||||
persistence::{load_from_disk, save_to_disk},
|
||||
IdleError,
|
||||
};
|
||||
|
||||
/// Adds a parsed vote to the list of planned events.
|
||||
///
|
||||
|
|
@ -206,3 +211,41 @@ pub struct EventHolder {
|
|||
/// The newly created discord context
|
||||
pub discord_ctx: Arc<tokio::sync::Mutex<Option<poise::serenity_prelude::Context>>>,
|
||||
}
|
||||
|
||||
impl EventHolder {
|
||||
pub fn try_load_from_disk() -> Result<Self, IdleError> {
|
||||
Ok(Self {
|
||||
pending_events: Arc::new(std::sync::Mutex::new(load_from_disk::<ListPendingEvents>(
|
||||
"data/pending_events.json",
|
||||
))),
|
||||
ongoing_events: Arc::new(std::sync::Mutex::new(load_from_disk::<ListOngoingEvents>(
|
||||
"data/ongoin_events.json",
|
||||
))),
|
||||
closed_events: Arc::new(std::sync::Mutex::new(load_from_disk::<ListClosedEvents>(
|
||||
"data/ongoin_events.json",
|
||||
))),
|
||||
old_date: Arc::new(std::sync::Mutex::new(load_from_disk::<NaiveDate>(
|
||||
"data/old_date.json",
|
||||
))),
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
|
||||
pub fn try_save_to_disk(holder: EventHolder) -> Result<(), IdleError> {
|
||||
save_to_disk::<&ListPendingEvents>(
|
||||
&holder.pending_events.lock().unwrap(),
|
||||
"data/pending_events.json",
|
||||
)?;
|
||||
save_to_disk::<&ListOngoingEvents>(
|
||||
&holder.ongoing_events.lock().unwrap(),
|
||||
"data/ongoing_events.json",
|
||||
)?;
|
||||
save_to_disk::<&ListPendingEvents>(
|
||||
&holder.pending_events.lock().unwrap(),
|
||||
"data/pending_events.json",
|
||||
)?;
|
||||
|
||||
save_to_disk::<&NaiveDate>(&holder.old_date.lock().unwrap(), "data/old_date.json")?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,10 @@
|
|||
//! The module aims to provide a robust framework for managing event lifecycles within an
|
||||
//! application, ensuring separation between pending and ongoing events and supporting
|
||||
//! actions such as adding, launching, processing, and autoclosing events.
|
||||
use std::{fs::File, io::Write};
|
||||
|
||||
use chrono::NaiveDateTime;
|
||||
use nom::AsBytes;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
|
|
|
|||
Loading…
Reference in New Issue