diff --git a/.gitignore b/.gitignore
index 4d3429a..9b6bb11 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,6 +4,7 @@
debug/
target/
test/
+bobito/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
diff --git a/badges/coverage.svg b/badges/coverage.svg
index b1445c0..1a24f8b 100644
--- a/badges/coverage.svg
+++ b/badges/coverage.svg
@@ -1 +1 @@
-
+
diff --git a/src/error_handling.rs b/src/error_handling.rs
index 0dc55ed..26666de 100644
--- a/src/error_handling.rs
+++ b/src/error_handling.rs
@@ -104,11 +104,11 @@ pub enum IdleError {
#[error("An error occured while trying to parse a Discord command")]
DiscordContainerParse,
/// Indicate a backup write couldn't be done, usually means the file descriptor is busy or permission are incorrect
- #[error("An error occured while trying to write file {0}")]
- BackupWriteError(std::io::Error),
+ #[error("An error occured while trying to write file {0} {1}")]
+ BackupWriteError(String, std::io::Error),
/// Indicate a backup read couldn't be done, usually means the file descriptor is busy or permission are incorrect
- #[error("An error occured while trying to read file {0}")]
- BackupReadError(std::io::Error),
+ #[error("An error occured while trying to read file {0} {1}")]
+ BackupReadError(String, std::io::Error),
/// Indicate a value couldn't be serialized
#[error("An error occured while trying to serialize data {0}")]
SerdeError(serde_json::Error),
diff --git a/src/ids.rs b/src/ids.rs
index 8c4d948..418ba01 100644
--- a/src/ids.rs
+++ b/src/ids.rs
@@ -36,6 +36,8 @@ pub fn generate_id() -> String {
pub fn generate_unique_id(holder: &EventHolder) -> String {
let id = generate_id();
match holder.contains_event(&id) {
+ // Testing this collision is not possible
+ #[cfg(not(tarpaulin_include))]
true => generate_unique_id(holder),
false => id,
}
diff --git a/src/io.rs b/src/io.rs
index 0817149..0f8a7b7 100644
--- a/src/io.rs
+++ b/src/io.rs
@@ -422,5 +422,16 @@ mod tests {
find_role_from_str(Some("ffxivpogfish"), arguments.to_vec()),
Some("ffxiv_progfish".to_string())
);
+ assert!(find_role_from_str(None, arguments.to_vec()).is_none());
+ assert!(find_role_from_str(Some("Bob"), Vec::new()).is_none());
+ }
+
+ #[test]
+ fn create_container() {
+ VoteContainer::new(
+ "IO".to_string(),
+ "desc".to_string(),
+ VoteTypeIdentifier::WeeklyVote,
+ );
}
}
diff --git a/src/persistence.rs b/src/persistence.rs
index d278473..18258c0 100644
--- a/src/persistence.rs
+++ b/src/persistence.rs
@@ -26,25 +26,26 @@ where
macro_rules! load_from_reader {
($r_type:ident, $path:expr) => {
Arc::new(std::sync::Mutex::new(load_from_reader::<$r_type, File>(
- File::open($path).map_err(IdleError::BackupReadError)?,
+ File::open($path).map_err(|e| IdleError::BackupReadError($path.to_owned(), e))?,
)))
};
}
+// Testing a bad serialisation is pointless
+#[cfg(not(tarpaulin_include))]
/// Tries to save the list to disk and return an error on failure
pub(crate) fn save_to_writer(data: Data, mut writer: Writer) -> Result<(), IdleError>
where
Data: Serialize,
- Writer: Write + Sized,
+ Writer: Write + Sized + std::fmt::Debug,
{
- // let mut f = File::create(wrtr).map_err(IdleError::BackupError)?;
writer
.write(
serde_json::to_string_pretty(&data)
.map_err(IdleError::SerdeError)?
.as_bytes(),
)
- .map_err(IdleError::BackupWriteError)?;
+ .map_err(|e| IdleError::BackupWriteError(format!("{:?}", writer), e))?;
Ok(())
}
@@ -60,7 +61,7 @@ macro_rules! save_to_writer {
($value:expr, $r_type:ident, $path:expr) => {
save_to_writer::<&$r_type, File>(
&$value.lock().unwrap(),
- File::create($path).map_err(IdleError::BackupWriteError)?,
+ File::create($path).map_err(|e| IdleError::BackupWriteError($path.to_string(), e))?,
)?;
};
}
@@ -92,7 +93,6 @@ mod tests {
let event_og = Arc::new(Mutex::new(ListPendingEvents::default()));
{
let mut e = event_og.lock().unwrap();
- println!(" Here {:?}", event_og);
for _ in 0..10 {
e.add_pending_event(EventPending::default())?;
}
diff --git a/src/votes/helper_functions.rs b/src/votes/helper_functions.rs
index ffd962d..4433615 100644
--- a/src/votes/helper_functions.rs
+++ b/src/votes/helper_functions.rs
@@ -2,7 +2,10 @@
//!
use crate::votes::traits::BorrowInner;
use chrono::NaiveDate;
-use std::{fs::File, sync::Arc};
+use std::{
+ fs::{create_dir, File},
+ sync::Arc,
+};
use super::{
events::{EventOngoing, EventPending},
@@ -221,33 +224,49 @@ macro_rules! inner {
}
impl EventHolder {
- pub fn try_load_from_disk() -> Result {
+ pub fn try_load_from_disk(save_path: Option<&str>) -> Result {
+ let save_path = save_path.unwrap_or("data");
Ok(Self {
- pending_events: load_from_reader!(ListPendingEvents, "data/pending_events.json"),
- ongoing_events: load_from_reader!(ListOngoingEvents, "data/ongoin_events.json"),
- closed_events: load_from_reader!(ListClosedEvents, "data/closed_events.json"),
- old_date: load_from_reader!(NaiveDate, "data/old_date.json"),
+ pending_events: load_from_reader!(
+ ListPendingEvents,
+ save_path.to_owned() + "/pending_events.json"
+ ),
+ ongoing_events: load_from_reader!(
+ ListOngoingEvents,
+ save_path.to_owned() + "/ongoing_events.json"
+ ),
+ closed_events: load_from_reader!(
+ ListClosedEvents,
+ save_path.to_owned() + "/closed_events.json"
+ ),
+ old_date: load_from_reader!(NaiveDate, save_path.to_owned() + "/old_date.json"),
..Default::default()
})
}
- pub fn try_save_to_disk(holder: EventHolder) -> Result<(), IdleError> {
+ pub fn try_save_to_disk(&self, save_path: Option<&str>) -> Result<(), IdleError> {
+ let save_path = save_path.unwrap_or("data");
+ let _ = create_dir(save_path);
save_to_writer!(
- holder.pending_events,
+ self.pending_events,
ListPendingEvents,
- "data/pending_events.json"
+ save_path.to_owned() + "/pending_events.json"
);
save_to_writer!(
- holder.ongoing_events,
+ self.ongoing_events,
ListOngoingEvents,
- "data/ongoing_events.json"
+ save_path.to_owned() + "/ongoing_events.json"
);
save_to_writer!(
- holder.closed_events,
+ self.closed_events,
ListClosedEvents,
- "data/closed_events.json"
+ save_path.to_owned() + "/closed_events.json"
+ );
+ save_to_writer!(
+ &self.old_date,
+ NaiveDate,
+ save_path.to_owned() + "/old_date.json"
);
- save_to_writer!(&holder.old_date, NaiveDate, "data/old_date.json");
Ok(())
}
@@ -256,8 +275,8 @@ impl EventHolder {
/// This is a low level plumbing tool, mostly used to check for collisions
pub(crate) fn contains_event(&self, id: &String) -> bool {
inner!(self.pending_events).contains_key(id)
- || inner!(self.pending_events).contains_key(id)
- || inner!(self.pending_events).contains_key(id)
+ || inner!(self.ongoing_events).contains_key(id)
+ || inner!(self.closed_events).contains_key(id)
}
/// Try to find an even by it's id, returns a TypedEvent to keep track of the location of the event
@@ -281,3 +300,49 @@ impl EventHolder {
})
}
}
+
+#[cfg(test)]
+mod test {
+ use crate::votes::{holders::TypedEvent, EventPending};
+
+ use super::{add_pending_event, close_ongoing_event, make_into_ongoing_event, EventHolder};
+
+ #[test]
+ fn test_save_load() {
+ let hld = EventHolder::default();
+
+ let (a, b, c) = (
+ add_pending_event(EventPending::default(), &hld).unwrap(),
+ add_pending_event(EventPending::default(), &hld).unwrap(),
+ add_pending_event(EventPending::default(), &hld).unwrap(),
+ );
+
+ make_into_ongoing_event(&b, &hld).unwrap();
+ make_into_ongoing_event(&c, &hld).unwrap();
+
+ close_ongoing_event(&c, &hld).unwrap();
+ assert!(hld.contains_event(&a));
+ assert!(hld.contains_event(&b));
+ assert!(hld.contains_event(&c));
+
+ hld.try_save_to_disk(Some("bobito")).unwrap();
+
+ let hld2 = EventHolder::try_load_from_disk(Some("bobito")).unwrap();
+
+ assert!(hld2.contains_event(&a));
+ assert!(hld2.contains_event(&b));
+ assert!(hld2.contains_event(&c));
+ let (a2, b2, c2) = (
+ hld.find_event(&a).unwrap(),
+ hld.find_event(&b).unwrap(),
+ hld.find_event(&c).unwrap(),
+ );
+ // match (a2, b2, c2) {
+ // TypedEvent::Ongoing(),TypedEvent::Pending(),TypedEvent::Closed() => (),
+ // _ =>
+ // }
+ assert!(matches!(a2, TypedEvent::Pending(_)));
+ assert!(matches!(b2, TypedEvent::Ongoing(_)));
+ assert!(matches!(c2, TypedEvent::Closed(_)));
+ }
+}