Files
inkopslista/gui/src/config.rs
T
ide 22cf928852 Add automatic generation of favourite buttons
favourite buttons are based on how often a good i added to list
finished implementing setting category
made number of days until button an environment variable

rebase on main and fix

ran cargo fmt
2026-08-19 23:48:19 +02:00

63 lines
1.7 KiB
Rust

use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct Config {
pub address: String,
}
impl Default for Config {
fn default() -> Self {
Self {
address: "https://inkopslista.nubo.sh".to_string(),
}
}
}
#[cfg(target_os = "linux")]
pub use fs::*;
#[cfg(target_os = "linux")]
mod fs {
use crate::runtime::spawn;
use super::Config;
use anyhow::{Context, Ok};
const CONFIG_FILE_NAME: &str = "config.json";
const XDG_PREFIX: &str = "inköpslista";
pub fn save_config(address: String) -> Result<(), anyhow::Error> {
let xdg_dirs = xdg::BaseDirectories::with_prefix(XDG_PREFIX);
let conf = Config { address };
let conf = serde_json::to_string(&conf).context("unable to serialize config")?;
let path = xdg_dirs
.place_config_file(CONFIG_FILE_NAME)
.context("unable to create config directory")?;
spawn(async {
let res = tokio::fs::write(path, conf)
.await
.context("unable to write config file");
if let Err(err) = res {
eprintln!("{err:?}");
}
});
Ok(())
}
pub async fn get_config() -> Result<Config, anyhow::Error> {
let xdg_dirs = xdg::BaseDirectories::with_prefix(XDG_PREFIX);
let file = xdg_dirs
.get_config_file(CONFIG_FILE_NAME)
.context("no config file")?;
let content = tokio::fs::read_to_string(file)
.await
.context("failed to read config file")?;
let conf: Config =
serde_json::from_str(&content).context("Unable to deserealize contect")?;
Ok(conf)
}
}