use std::path::PathBuf;
use bonsaidb::core::Error;
use crate::end_of_life::Cycle;
use bonsaidb::{
core::{
connection::Connection,
schema::{Collection, SerializedCollection},
},
local::{
config::{Builder, StorageConfiguration},
Database,
},
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Collection)]
#[collection(
name = "eol_cycles",
primary_key = String,
natural_id = Some(format!("{}::{}", self.product, self.cycle))
)]
struct CachedCycle {
product: String,
cycle: String,
data: Cycle,
}
pub fn get_cycle(
db: &Database,
product: &str,
cycle: &str,
) -> Result<Option<Cycle>, bonsaidb::core::Error> {
let key = format!("{product}::{cycle}");
let entry = CachedCycle::get(&key, db)?;
match entry {
Some(x) => Ok(Some(x.contents.data)),
None => Ok(None),
}
}
pub fn insert_cycle(
db: &Database,
product: &str,
cycle: &str,
data: Cycle,
) -> Result<Cycle, Error> {
let key = format!("{product}::{cycle}");
let entry = CachedCycle {
product: String::from(product),
cycle: String::from(cycle),
data,
};
if let Ok(None) = get_cycle(db, product, cycle) {
db.collection::<CachedCycle>().insert(&key, &entry)?;
}
Ok(entry.data)
}
pub fn clear() -> Result<(), Error> {
drop(std::fs::remove_dir_all(eol_cache_db_path()));
Ok(())
}
pub fn eol_cache_db() -> Result<Database, Error> {
let db_path = eol_cache_db_path();
Ok(
Database::open::<CachedCycle>(StorageConfiguration::new(db_path))
.expect("Couldn't connect to DB"),
)
}
fn eol_cache_db_path() -> PathBuf {
directories::ProjectDirs::from("rs", "", "corrator")
.expect("could not find project directory")
.data_dir()
.join("corrator.bonsaidb")
}