Initial commit

Signed-off-by: Marko Korhonen <marko.korhonen@reekynet.com>
This commit is contained in:
Marko Korhonen 2020-01-19 21:33:52 +02:00
commit 5f81577b83
No known key found for this signature in database
GPG key ID: 911B85FBC6003FE5
7 changed files with 398 additions and 0 deletions

52
src/config.rs Normal file
View file

@ -0,0 +1,52 @@
use super::fs;
use super::Config;
use std::path::PathBuf;
use xdg::BaseDirectories;
extern crate xdg;
fn default_config() -> Config {
Config {
resume_file_path: PathBuf::from("/tmp/lqsd-resume"),
idle_level: 0,
dim_speed: 50,
resume_speed: 25,
}
}
fn xdg_config() -> PathBuf {
BaseDirectories::with_prefix("lqsd")
.expect("cannot create configuration directory")
.place_config_file("config.toml")
.unwrap()
}
pub fn load_xdg() -> Config {
let path = xdg_config();
if !path.exists() {
println!(
"{} does not exist, writing default configuration",
path.display()
);
match fs::write(&path, toml::to_string(&default_config()).unwrap()) {
Ok(()) => println!("Default config saved to {}", path.display()),
Err(err) => eprintln!("Failed to write default config: {}", err),
};
default_config()
} else {
let toml = fs::read(&path).unwrap();
let config: Config = toml::from_str(&toml).unwrap();
config
}
}
pub fn load_user(path: PathBuf) -> Config {
if !path.exists() {
panic!("{} does not exist", path.display());
} else {
let toml = fs::read(&path).unwrap();
let config: Config = toml::from_str(&toml).unwrap();
config
}
}