Check if config file exists

This commit is contained in:
hodasemi 2022-07-15 19:17:08 +02:00
parent 5b8ffc8dd9
commit 4ec946ebbd

View file

@ -1,18 +1,22 @@
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::{fs::File, io::Write};
use std::{fs::File, io::Write, path::Path};
pub const COMMAND_COUNT: usize = 8;
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct Config {
pub commands: [Option<String>; COMMAND_COUNT],
}
impl Config {
pub fn open(path: &str) -> Result<Config> {
if Path::new(path).exists() {
Ok(serde_json::from_str(&std::fs::read_to_string(path)?)?)
} else {
Ok(Config::default())
}
}
pub fn save(&self, path: &str) -> Result<()> {
@ -24,11 +28,3 @@ impl Config {
Ok(())
}
}
impl Default for Config {
fn default() -> Config {
Config {
commands: Default::default(),
}
}
}