Skip to content

Commit 2f994a2

Browse files
committed
Add better toml validation
1 parent 1bd9dea commit 2f994a2

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

src/main.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ mod config;
44
mod macos;
55
mod utils;
66

7+
use std::path::Path;
8+
79
use crate::{app::Tile, config::Config, utils::to_key_code};
810

911
use global_hotkey::{
@@ -20,16 +22,19 @@ fn main() -> iced::Result {
2022
let home = std::env::var("HOME").unwrap();
2123

2224
let file_path = home.clone() + "/.config/rustcast/config.toml";
25+
if !Path::new(&file_path).exists() {
26+
std::fs::create_dir_all(home + "/.config/rustcast").unwrap();
27+
std::fs::write(
28+
&file_path,
29+
toml::to_string(&Config::default()).unwrap_or_else(|x| x.to_string()),
30+
)
31+
.unwrap();
32+
}
2333
let config: Config = match std::fs::read_to_string(&file_path) {
24-
Ok(a) => toml::from_str(&a).unwrap(),
34+
Ok(a) => toml::from_str(&a).unwrap_or(Config::default()),
2535
Err(_) => Config::default(),
2636
};
27-
std::fs::create_dir_all(home + "/.config/rustcast").unwrap();
28-
std::fs::write(
29-
&file_path,
30-
toml::to_string(&config).unwrap_or_else(|x| x.to_string()),
31-
)
32-
.unwrap();
37+
3338
let manager = GlobalHotKeyManager::new().unwrap();
3439

3540
let show_hide = HotKey::new(

0 commit comments

Comments
 (0)