Split out common config code

main
Tassilo Horn 3 years ago
parent 6c6d237164
commit 80a8a3a262
  1. 52
      swayr/src/config.rs
  2. 100
      swayr/src/shared/cfg.rs
  3. 2
      swayr/src/shared/mod.rs
  4. 36
      swayr/src/shared/util.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::<Config>("swayr")
}
#[test]
fn test_load_config() {
let cfg = load_config();
let cfg = cfg::load_config::<Config>("swayr");
println!("{:?}", cfg);
}

@ -0,0 +1,100 @@
// Copyright (C) 2021-2022 Tassilo Horn <tsdh@gnu.org>
//
// 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 <https://www.gnu.org/licenses/>.
/// 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<Path> {
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<T>(project: &str, cfg: T)
where
T: Serialize,
{
let path = get_config_file_path(project);
let content =
toml::to_string_pretty::<T>(&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::<T>(&buf).expect("Invalid config.")
}

@ -13,6 +13,6 @@
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/>.
pub mod cfg;
pub mod fmt;
pub mod ipc;
pub mod util;

@ -1,36 +0,0 @@
// Copyright (C) 2021-2022 Tassilo Horn <tsdh@gnu.org>
//
// 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 <https://www.gnu.org/licenses/>.
/// 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<Path> {
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()
}
Loading…
Cancel
Save