meowy-webring/src/sites.rs

41 lines
987 B
Rust
Raw Normal View History

use rocket::tokio::sync::Mutex;
use shared::{
directories,
errors::{Error, ErrorStatus},
names::{self, Site},
};
use std::sync::OnceLock;
pub async fn get_global_names() -> Vec<Site> {
NAMES.get().unwrap().lock().await.clone()
}
pub fn set_names() {
match get_names() {
Ok(names) => *NAMES.get().unwrap().blocking_lock() = names,
Err(err) => println!("{:?}", err),
}
}
static NAMES: OnceLock<Mutex<Vec<Site>>> = OnceLock::new();
pub fn init_names() -> Result<(), Error> {
println!(
"names.json path: {}",
directories::get_names_path().unwrap().display()
);
match NAMES.set(Mutex::new(get_names().unwrap())) {
Ok(_) => Ok(()),
Err(_) => Err(Error {
status: ErrorStatus::GenericError,
data: "an error has occured while trying to get the names.json file".into(),
}),
}
}
fn get_names() -> Result<Vec<Site>, Error> {
let names_path = directories::get_names_path()?;
let names_file = names::read_names_file(&names_path)?;
names::load_names(names_file)
}