use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct FilterConfig {
pub fit_filter: Option<String>,
pub availability_filter: Option<String>,
pub tp_filter: Option<String>,
pub sort_column: Option<String>,
pub sort_ascending: Option<bool>,
pub installed_first: Option<bool>,
pub search_query: Option<String>,
pub providers: Option<HashMap<String, bool>>,
pub use_cases: Option<HashMap<String, bool>>,
pub capabilities: Option<HashMap<String, bool>>,
pub quants: Option<HashMap<String, bool>>,
pub run_modes: Option<HashMap<String, bool>>,
pub params_buckets: Option<HashMap<String, bool>>,
pub licenses: Option<HashMap<String, bool>>,
pub runtimes: Option<HashMap<String, bool>>,
}
impl FilterConfig {
fn config_path() -> Option<PathBuf> {
Some(dirs::config_dir()?.join("llmfit").join("filters.json"))
}
pub fn load() -> Self {
Self::config_path()
.and_then(|path| fs::read_to_string(path).ok())
.and_then(|s| serde_json::from_str(&s).ok())
.unwrap_or_default()
}
pub fn save(&self) {
if let Some(path) = Self::config_path() {
if let Some(parent) = path.parent() {
let _ = fs::create_dir_all(parent);
}
if let Ok(json) = serde_json::to_string_pretty(self) {
let _ = fs::write(&path, json);
}
}
}
pub fn apply_map(names: &[String], selected: &mut [bool], saved: &HashMap<String, bool>) {
for (i, name) in names.iter().enumerate() {
if let Some(&val) = saved.get(name) {
selected[i] = val;
}
}
}
pub fn build_map(names: &[String], selected: &[bool]) -> HashMap<String, bool> {
names
.iter()
.zip(selected.iter())
.map(|(name, &sel)| (name.clone(), sel))
.collect()
}
}