Files
TUI-FM/src/config.rs

49 lines
1.3 KiB
Rust

use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Serialize, Deserialize, Default, Clone)]
pub struct Config {
pub favorites: Vec<String>,
}
impl Config {
fn config_path() -> Option<PathBuf> {
dirs::config_dir().map(|d| d.join("tui-fm").join("config.json"))
}
pub fn load() -> Self {
if let Some(path) = Self::config_path() {
if let Ok(data) = std::fs::read_to_string(&path) {
if let Ok(config) = serde_json::from_str::<Config>(&data) {
return config;
}
}
}
Config::default()
}
pub fn save(&self) {
if let Some(path) = Self::config_path() {
if let Some(parent) = path.parent() {
let _ = std::fs::create_dir_all(parent);
}
if let Ok(data) = serde_json::to_string_pretty(self) {
let _ = std::fs::write(&path, data);
}
}
}
pub fn add_favorite(&mut self, path: &str) {
let s = path.to_string();
if !self.favorites.contains(&s) {
self.favorites.push(s);
self.save();
}
}
pub fn remove_favorite(&mut self, path: &str) {
self.favorites.retain(|f| f != path);
self.save();
}
}