handle non existent file

This commit is contained in:
ajuvercr 2020-03-28 20:01:12 +01:00
parent 498a780854
commit 56e3138405
2 changed files with 8 additions and 2 deletions

View file

@ -193,7 +193,7 @@ impl game::Controller for PlanetWarsGame {
fn is_done(&mut self) -> bool {
if self.state.is_finished() {
let mut f = match OpenOptions::new().append(true).open("games.ini") { Err(_) => return true, Ok(f) => f };
let mut f = match OpenOptions::new().create(true).append(true).open("games.ini") { Err(_) => return true, Ok(f) => f };
let mut conf = Ini::new();
conf.with_section(Some(self.log_file_loc.clone()))

View file

@ -99,7 +99,13 @@ use ini::Ini;
pub async fn get_games() -> Result<Vec<GameOption>, String> {
let mut games = Vec::new();
let content = fs::read_to_string("games.ini").await.map_err(|_| "IO error".to_string())?;
let content = match fs::read_to_string("games.ini").await {
Ok(v) => v,
Err(_) => {
fs::File::create("games.ini").await.map_err(|_| "IO Error".to_string())?;
String::new()
}
};
let i = Ini::load_from_str(&content).map_err(|_| "Corrupt ini file".to_string())?;