cosmic-applets/applets/cosmic-app-list/src/config.rs

57 lines
1.4 KiB
Rust
Raw Normal View History

2022-07-19 11:33:19 -04:00
use anyhow::anyhow;
use serde::Deserialize;
2022-07-19 23:39:19 -04:00
use std::fmt::Debug;
use std::fs::File;
2022-07-19 11:33:19 -04:00
use xdg::BaseDirectories;
pub const APP_ID: &str = "com.system76.CosmicAppList";
pub const VERSION: &str = "0.1.0";
#[derive(Debug, Clone, Deserialize, Default)]
2022-07-19 11:33:19 -04:00
pub enum TopLevelFilter {
#[default]
2022-07-19 11:33:19 -04:00
ActiveWorkspace,
ConfiguredOutput,
}
#[derive(Debug, Clone, Default, Deserialize)]
pub struct AppListConfig {
pub filter_top_levels: Option<TopLevelFilter>,
pub favorites: Vec<String>,
2022-07-19 11:33:19 -04:00
}
impl AppListConfig {
/// load config with the provided name
pub fn load() -> anyhow::Result<AppListConfig> {
2022-07-19 23:39:19 -04:00
let file = match BaseDirectories::new()
.ok()
.and_then(|dirs| dirs.find_config_file(format!("{APP_ID}/config.ron")))
2022-07-19 23:39:19 -04:00
.and_then(|p| File::open(p).ok())
{
2022-07-19 11:33:19 -04:00
Some(path) => path,
_ => {
anyhow::bail!("Failed to load config");
}
};
2022-07-19 23:39:19 -04:00
ron::de::from_reader::<_, AppListConfig>(file)
.map_err(|err| anyhow!("Failed to parse config file: {}", err))
2022-07-19 11:33:19 -04:00
}
pub fn add_favorite(&mut self, id: String) -> anyhow::Result<()> {
if !self.favorites.contains(&id) {
self.favorites.push(id);
}
todo!()
}
pub fn remove_favorite(&mut self, id: String) -> anyhow::Result<()> {
self.favorites.retain(|e| e != &id);
todo!()
}
pub fn save() -> anyhow::Result<()> {
todo!()
}
2022-07-19 23:39:19 -04:00
}