mirror of
https://github.com/hulthe/inkopslista.git
synced 2026-09-18 18:03:39 +02:00
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
63 lines
1.7 KiB
Rust
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)
|
|
}
|
|
}
|