mirror of
https://github.com/hulthe/inkopslista.git
synced 2026-09-18 18:03:39 +02:00
Make it run in browser
This commit is contained in:
+42
-33
@@ -1,9 +1,6 @@
|
||||
use anyhow::{Context, Ok};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
|
||||
pub struct Config {
|
||||
pub address: String,
|
||||
}
|
||||
@@ -11,42 +8,54 @@ pub struct Config {
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
address: "http://localhost:8000".to_string(),
|
||||
address: "https://inkopslista.nubo.sh".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
const CONFIG_FILE_NAME: &'static str = "config.json";
|
||||
const XDG_PREFIX: &'static 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: address };
|
||||
let conf = serde_json::to_string(&conf).context("unable to serialize config")?;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub use fs::*;
|
||||
|
||||
let path = xdg_dirs
|
||||
.place_config_file(CONFIG_FILE_NAME)
|
||||
.context("unable to create config directory")?;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
mod fs {
|
||||
use super::Config;
|
||||
use anyhow::{Context, Ok};
|
||||
use serde_json;
|
||||
|
||||
tokio::spawn(async {
|
||||
let res = tokio::fs::write(path, conf)
|
||||
const CONFIG_FILE_NAME: &'static str = "config.json";
|
||||
const XDG_PREFIX: &'static 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: 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")?;
|
||||
|
||||
tokio::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("unable to write config file");
|
||||
if let Err(err) = res {
|
||||
eprintln!("{err:?}");
|
||||
}
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
.context("failed to read config file")?;
|
||||
|
||||
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)
|
||||
let conf: Config =
|
||||
serde_json::from_str(&content).context("Unable to deserealize contect")?;
|
||||
Ok(conf)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user