feat: config methods

This commit is contained in:
Ashley Wulber 2022-12-13 22:46:30 -05:00 committed by Ashley Wulber
parent 86ed2d4e31
commit 6fef03ed2f
16 changed files with 25 additions and 1578 deletions

View file

@ -1,31 +1,35 @@
use anyhow::anyhow;
use serde::Deserialize;
use serde::{Serialize, Deserialize};
use std::fmt::Debug;
use std::fs::File;
use std::path::PathBuf;
use xdg::BaseDirectories;
pub const APP_ID: &str = "com.system76.CosmicAppList";
pub const VERSION: &str = "0.1.0";
#[derive(Debug, Clone, Deserialize, Default)]
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
pub enum TopLevelFilter {
#[default]
ActiveWorkspace,
ConfiguredOutput,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct AppListConfig {
pub filter_top_levels: Option<TopLevelFilter>,
pub favorites: Vec<String>,
}
impl AppListConfig {
// TODO async?
/// load config with the provided name
pub fn load() -> anyhow::Result<AppListConfig> {
let mut relative_path = PathBuf::from(APP_ID);
relative_path.push("config.ron");
let file = match BaseDirectories::new()
.ok()
.and_then(|dirs| dirs.find_config_file(format!("{APP_ID}/config.ron")))
.and_then(|dirs| dirs.find_config_file(relative_path))
.and_then(|p| File::open(p).ok())
{
Some(path) => path,
@ -42,15 +46,22 @@ impl AppListConfig {
if !self.favorites.contains(&id) {
self.favorites.push(id);
}
todo!()
self.save()
}
pub fn remove_favorite(&mut self, id: String) -> anyhow::Result<()> {
self.favorites.retain(|e| e != &id);
todo!()
self.save()
}
pub fn save() -> anyhow::Result<()> {
todo!()
// TODO async?
pub fn save(&self) -> anyhow::Result<()> {
let bd = BaseDirectories::new()?;
let mut relative_path = PathBuf::from(APP_ID);
relative_path.push("config.ron");
let config_path = bd.place_config_file(relative_path)?;
let f = File::create(config_path)?;
ron::ser::to_writer_pretty(f, self, Default::default())?;
Ok(())
}
}