From cc66406216b592081a8cf2a5a33242454beee868 Mon Sep 17 00:00:00 2001 From: asimon-1 <40246417+asimon-1@users.noreply.github.com> Date: Fri, 18 Aug 2023 13:21:47 -0400 Subject: [PATCH] More gracefully handle scenario when TOML file doesn't match schema (#599) --- src/common/release.rs | 13 ++----------- training_mod_consts/src/config.rs | 30 ++++++++++++++++++------------ 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/common/release.rs b/src/common/release.rs index c2cda8a..e18e3fa 100644 --- a/src/common/release.rs +++ b/src/common/release.rs @@ -7,12 +7,11 @@ use anyhow::{anyhow, Result}; use lazy_static::lazy_static; use parking_lot::Mutex; use serde_json::Value; -use std::io::{Error, ErrorKind}; use zip::ZipArchive; lazy_static! { pub static ref CURRENT_VERSION: Mutex = - Mutex::new(get_current_version().unwrap_or("".to_string())); + Mutex::new(get_current_version().expect("Could not find current modpack version!\n")); } #[derive(Debug)] @@ -160,20 +159,12 @@ fn user_wants_to_install() -> bool { } fn get_current_version() -> Result { - let config = TrainingModpackConfig::load(); + let config = TrainingModpackConfig::load_or_create(); match config { Ok(c) => { info!("Config file found and parsed. Loading..."); Ok(c.update.last_update_version) } - Err(e) - if e.is::() - && e.downcast_ref::().unwrap().kind() == ErrorKind::NotFound => - { - warn!("No config file found, creating default..."); - TrainingModpackConfig::create_default()?; - get_current_version() - } Err(e) => { // Some other error, re-raise it Err(e) diff --git a/training_mod_consts/src/config.rs b/training_mod_consts/src/config.rs index 251ec0f..9608822 100644 --- a/training_mod_consts/src/config.rs +++ b/training_mod_consts/src/config.rs @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize}; use skyline::nn::time; use std::fs; -use std::io::{Error, ErrorKind}; +use std::io; use std::time::{SystemTime, UNIX_EPOCH}; /// Top level struct which represents the entirety of the modpack config @@ -31,23 +31,29 @@ impl TrainingModpackConfig { let parsed = toml::from_str::(&toml_config_str)?; Ok(parsed) } else { - Err(Error::from(ErrorKind::NotFound).into()) + Err(io::Error::from(io::ErrorKind::NotFound).into()) } } pub fn load_or_create() -> Result { match TrainingModpackConfig::load() { Ok(c) => Ok(c), - Err(e) - if e.is::() - && e.downcast_ref::().unwrap().kind() == ErrorKind::NotFound => - { - TrainingModpackConfig::create_default()?; - TrainingModpackConfig::load() - } Err(e) => { - // Some other error, re-raise it - Err(e) + if e.is::() + && e.downcast_ref::().unwrap().kind() == io::ErrorKind::NotFound + { + // No config file exists already + TrainingModpackConfig::create_default()?; + TrainingModpackConfig::load() + } else if e.is::() { + // A config file exists but its not in the right format + fs::remove_file(TRAINING_MODPACK_TOML_PATH)?; + TrainingModpackConfig::create_default()?; + TrainingModpackConfig::load() + } else { + // Some other error, re-raise it + Err(e) + } } } } @@ -56,7 +62,7 @@ impl TrainingModpackConfig { /// Returns Err if the file already exists pub fn create_default() -> Result<()> { if fs::metadata(TRAINING_MODPACK_TOML_PATH).is_ok() { - Err(Error::from(ErrorKind::AlreadyExists).into()) + Err(io::Error::from(io::ErrorKind::AlreadyExists).into()) } else { let default_config: TrainingModpackConfig = TrainingModpackConfig::new(); let contents = toml::to_string(&default_config)?;