-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from lautarodragan/taro/remember-last-visited-…
…path-between-sessions feat: persist last_visited_path
- Loading branch information
Showing
4 changed files
with
79 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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())) | ||
} |