60 lines
1.5 KiB
Rust
60 lines
1.5 KiB
Rust
use std::fs::File;
|
|
use std::io::{Write, Read};
|
|
|
|
use serde_derive::{Serialize, Deserialize};
|
|
|
|
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
|
|
pub(crate) struct Config {
|
|
pub(crate) position: (i32, i32),
|
|
pub(crate) size: (i32, i32),
|
|
}
|
|
|
|
pub(crate) fn save_config(config: Config) {
|
|
let mut config_path = dirs::config_dir()
|
|
.expect("Couldn't find user config directory");
|
|
|
|
config_path.push("ruin");
|
|
config_path.push("lull");
|
|
|
|
if !config_path.exists() {
|
|
std::fs::create_dir_all(&config_path)
|
|
.expect("Couldn't create lull config directory");
|
|
}
|
|
|
|
let toml = toml::to_string(&config)
|
|
.expect("Couldn't convert config to toml");
|
|
|
|
config_path.push("lull.toml");
|
|
|
|
let mut config_file = File::create(config_path)
|
|
.expect("Couldn't create config file");
|
|
|
|
config_file.write(&toml.into_bytes())
|
|
.expect("Couldn't write config file");
|
|
}
|
|
|
|
pub(crate) fn load_config() -> Option<Config> {
|
|
let mut config_path = dirs::config_dir()
|
|
.expect("Couldn't find user config directory");
|
|
|
|
config_path.push("ruin");
|
|
config_path.push("lull");
|
|
config_path.push("lull.toml");
|
|
|
|
let file = File::open(config_path);
|
|
|
|
if file.is_err() {
|
|
return None;
|
|
}
|
|
|
|
let mut toml = String::new();
|
|
|
|
file.unwrap().read_to_string(&mut toml)
|
|
.expect("Couldn't read config file");
|
|
|
|
let config = toml::from_str(&toml)
|
|
.expect("Couldn't parse config");
|
|
|
|
Some(config)
|
|
}
|