diff --git a/swayr/src/config.rs b/swayr/src/config.rs index f4afd4f..88b49a3 100644 --- a/swayr/src/config.rs +++ b/swayr/src/config.rs @@ -15,11 +15,9 @@ //! TOML configuration for swayr. -use crate::shared::util as shared_util; +use crate::shared::cfg; use serde::{Deserialize, Serialize}; use std::collections::HashMap; -use std::fs::OpenOptions; -use std::io::{Read, Write}; #[derive(Debug, Serialize, Deserialize)] pub struct Config { @@ -302,56 +300,12 @@ impl Default for Config { } } -pub fn save_config(cfg: Config) { - let path = shared_util::get_config_file_path("swayr"); - let content = - toml::to_string_pretty(&cfg).expect("Cannot serialize config."); - let mut file = OpenOptions::new() - .read(false) - .write(true) - .create(true) - .open(path) - .unwrap(); - file.write_all(content.as_str().as_bytes()).unwrap(); -} - pub fn load_config() -> Config { - let path = shared_util::get_config_file_path("swayr"); - if !path.exists() { - save_config(Config::default()); - // Tell the user that a fresh default config has been created. - std::process::Command::new("swaynag") - .arg("--background") - .arg("00FF44") - .arg("--text") - .arg("0000CC") - .arg("--message") - .arg( - "Welcome to swayr! ".to_owned() - + "I've created a fresh config for use with wofi for you in " - + &path.to_string_lossy() - + ". Adapt it to your needs.", - ) - .arg("--type") - .arg("warning") - .arg("--dismiss-button") - .arg("Thanks!") - .spawn() - .ok(); - } - let mut file = OpenOptions::new() - .read(true) - .write(false) - .create(false) - .open(path) - .unwrap(); - let mut buf: String = String::new(); - file.read_to_string(&mut buf).unwrap(); - toml::from_str(&buf).expect("Invalid config.") + cfg::load_config::("swayr") } #[test] fn test_load_config() { - let cfg = load_config(); + let cfg = cfg::load_config::("swayr"); println!("{:?}", cfg); } diff --git a/swayr/src/shared/cfg.rs b/swayr/src/shared/cfg.rs new file mode 100644 index 0000000..490f0a8 --- /dev/null +++ b/swayr/src/shared/cfg.rs @@ -0,0 +1,100 @@ +// Copyright (C) 2021-2022 Tassilo Horn +// +// This program is free software: you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation, either version 3 of the License, or (at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +// more details. +// +// You should have received a copy of the GNU General Public License along with +// this program. If not, see . + +/// Config file loading stuff. +use directories::ProjectDirs; +use serde::de::DeserializeOwned; +use serde::Serialize; +use std::fs::{DirBuilder, OpenOptions}; +use std::io::{Read, Write}; +use std::path::Path; + +pub fn get_config_file_path(project: &str) -> Box { + let proj_dirs = ProjectDirs::from("", "", project).expect(""); + let user_config_dir = proj_dirs.config_dir(); + if !user_config_dir.exists() { + let sys_path = format!("/etc/xdg/{}/config.toml", project); + let sys_config_file = Path::new(sys_path.as_str()); + if sys_config_file.exists() { + return sys_config_file.into(); + } + DirBuilder::new() + .recursive(true) + .create(user_config_dir) + .unwrap(); + } + user_config_dir.join("config.toml").into_boxed_path() +} + +pub fn save_config(project: &str, cfg: T) +where + T: Serialize, +{ + let path = get_config_file_path(project); + let content = + toml::to_string_pretty::(&cfg).expect("Cannot serialize config."); + let mut file = OpenOptions::new() + .read(false) + .write(true) + .create(true) + .open(path) + .unwrap(); + file.write_all(content.as_str().as_bytes()).unwrap(); +} + +pub fn load_config<'a, T>(project: &str) -> T +where + T: Serialize + DeserializeOwned + Default, +{ + let path = get_config_file_path(project); + if !path.exists() { + save_config(project, T::default()); + // Tell the user that a fresh default config has been created. + std::process::Command::new("swaynag") + .arg("--background") + .arg("00FF44") + .arg("--text") + .arg("0000CC") + .arg("--message") + .arg( + if project == "swayr" { + "Welcome to swayr! ".to_owned() + + "I've created a fresh config for use with wofi for you in " + + &path.to_string_lossy() + + ". Adapt it to your needs." + }else{ + "Welcome to swayrbar! ".to_owned() + + "I've created a fresh config for for you in " + + &path.to_string_lossy() + + ". Adapt it to your needs." + }, + ) + .arg("--type") + .arg("warning") + .arg("--dismiss-button") + .arg("Thanks!") + .spawn() + .ok(); + } + let mut file = OpenOptions::new() + .read(true) + .write(false) + .create(false) + .open(path) + .unwrap(); + let mut buf: String = String::new(); + file.read_to_string(&mut buf).unwrap(); + toml::from_str::(&buf).expect("Invalid config.") +} diff --git a/swayr/src/shared/mod.rs b/swayr/src/shared/mod.rs index 6be39e1..8223bf2 100644 --- a/swayr/src/shared/mod.rs +++ b/swayr/src/shared/mod.rs @@ -13,6 +13,6 @@ // You should have received a copy of the GNU General Public License along with // this program. If not, see . +pub mod cfg; pub mod fmt; pub mod ipc; -pub mod util; diff --git a/swayr/src/shared/util.rs b/swayr/src/shared/util.rs deleted file mode 100644 index f32011e..0000000 --- a/swayr/src/shared/util.rs +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (C) 2021-2022 Tassilo Horn -// -// This program is free software: you can redistribute it and/or modify it -// under the terms of the GNU General Public License as published by the Free -// Software Foundation, either version 3 of the License, or (at your option) -// any later version. -// -// This program is distributed in the hope that it will be useful, but WITHOUT -// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for -// more details. -// -// You should have received a copy of the GNU General Public License along with -// this program. If not, see . - -/// Utility stuff shared by swayr and swayrbar. -use directories::ProjectDirs; -use std::fs::DirBuilder; -use std::path::Path; - -pub fn get_config_file_path(project: &str) -> Box { - let proj_dirs = ProjectDirs::from("", "", project).expect(""); - let user_config_dir = proj_dirs.config_dir(); - if !user_config_dir.exists() { - let sys_path = format!("/etc/xdg/{}/config.toml", project); - let sys_config_file = Path::new(sys_path.as_str()); - if sys_config_file.exists() { - return sys_config_file.into(); - } - DirBuilder::new() - .recursive(true) - .create(user_config_dir) - .unwrap(); - } - user_config_dir.join("config.toml").into_boxed_path() -}