use std::path::PathBuf;
use serde::{de::DeserializeOwned, Serialize};
pub mod cache;
pub mod config;
pub mod downloads;
pub mod library;
pub mod lyrics_cache;
pub mod playlists;
pub mod search_history;
pub mod session;
pub mod thumbnails;
fn project_dirs() -> Option<directories::ProjectDirs> {
directories::ProjectDirs::from("", "", "goosemusic")
}
pub fn config_path(file: &str) -> PathBuf {
project_dirs().map_or_else(|| PathBuf::from(file), |d| d.config_dir().join(file))
}
pub fn cache_path(sub: &str) -> PathBuf {
project_dirs().map_or_else(|| PathBuf::from(sub), |d| d.cache_dir().join(sub))
}
pub fn data_path(file: &str) -> PathBuf {
project_dirs().map_or_else(|| PathBuf::from(file), |d| d.data_local_dir().join(file))
}
pub enum StoreLocation {
Config,
Data,
Cache,
}
pub trait JsonStore: Serialize + DeserializeOwned + Default {
const FILE: &'static str;
const LOCATION: StoreLocation = StoreLocation::Config;
fn path() -> PathBuf {
match Self::LOCATION {
StoreLocation::Config => config_path(Self::FILE),
StoreLocation::Data => data_path(Self::FILE),
StoreLocation::Cache => cache_path(Self::FILE),
}
}
fn load() -> Self {
std::fs::read_to_string(Self::path())
.ok()
.and_then(|s| serde_json::from_str(&s).ok())
.unwrap_or_default()
}
#[cfg(not(test))]
fn save(&self) {
let path = Self::path();
if let Some(dir) = path.parent() {
let _ = std::fs::create_dir_all(dir);
}
if let Ok(s) = serde_json::to_string_pretty(self) {
let _ = std::fs::write(&path, s);
}
}
#[cfg(test)]
fn save(&self) {}
}