Check if config file exists
This commit is contained in:
parent
5b8ffc8dd9
commit
4ec946ebbd
1 changed files with 7 additions and 11 deletions
|
@ -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> {
|
||||
Ok(serde_json::from_str(&std::fs::read_to_string(path)?)?)
|
||||
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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue