-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstate.rs
44 lines (35 loc) · 1.15 KB
/
state.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use std::fs;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct State {
pub last_visited_path: Option<String>,
}
pub fn load_state() -> State {
let state_file_paths = [home::home_dir()
.unwrap()
.as_path()
.join(".config/kronos/state.toml")];
let mut content: String = "".to_owned();
for state_file_path in state_file_paths {
let result: Result<String, std::io::Error> = fs::read_to_string(state_file_path);
if let Ok(file_content) = result {
content = file_content;
break;
}
}
let state_toml: State = toml::from_str(&content).unwrap_or_else(|_| {
eprintln!("FAILED TO CREATE STATE OBJECT FROM FILE");
State {
last_visited_path: None,
}
});
state_toml
}
pub fn save_state(state: State) -> Result<(), String> {
let state_file_path = home::home_dir()
.unwrap()
.as_path()
.join(".config/kronos/state.toml");
toml::to_string(&state).map_err(|e| e.to_string())
.and_then(|serialized| fs::write(state_file_path, serialized).map_err(|e| e.to_string()))
}