1
0
Fork 0
mirror of https://github.com/jugeeya/UltimateTrainingModpack.git synced 2025-02-25 18:40:22 +00:00

More gracefully handle scenario when TOML file doesn't match schema (#599)

This commit is contained in:
asimon-1 2023-08-18 13:21:47 -04:00 committed by GitHub
parent 6ccdb7addd
commit cc66406216
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 23 deletions

View file

@ -7,12 +7,11 @@ use anyhow::{anyhow, Result};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use parking_lot::Mutex; use parking_lot::Mutex;
use serde_json::Value; use serde_json::Value;
use std::io::{Error, ErrorKind};
use zip::ZipArchive; use zip::ZipArchive;
lazy_static! { lazy_static! {
pub static ref CURRENT_VERSION: Mutex<String> = pub static ref CURRENT_VERSION: Mutex<String> =
Mutex::new(get_current_version().unwrap_or("".to_string())); Mutex::new(get_current_version().expect("Could not find current modpack version!\n"));
} }
#[derive(Debug)] #[derive(Debug)]
@ -160,20 +159,12 @@ fn user_wants_to_install() -> bool {
} }
fn get_current_version() -> Result<String> { fn get_current_version() -> Result<String> {
let config = TrainingModpackConfig::load(); let config = TrainingModpackConfig::load_or_create();
match config { match config {
Ok(c) => { Ok(c) => {
info!("Config file found and parsed. Loading..."); info!("Config file found and parsed. Loading...");
Ok(c.update.last_update_version) Ok(c.update.last_update_version)
} }
Err(e)
if e.is::<Error>()
&& e.downcast_ref::<Error>().unwrap().kind() == ErrorKind::NotFound =>
{
warn!("No config file found, creating default...");
TrainingModpackConfig::create_default()?;
get_current_version()
}
Err(e) => { Err(e) => {
// Some other error, re-raise it // Some other error, re-raise it
Err(e) Err(e)

View file

@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
use skyline::nn::time; use skyline::nn::time;
use std::fs; use std::fs;
use std::io::{Error, ErrorKind}; use std::io;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
/// Top level struct which represents the entirety of the modpack config /// Top level struct which represents the entirety of the modpack config
@ -31,23 +31,29 @@ impl TrainingModpackConfig {
let parsed = toml::from_str::<TrainingModpackConfig>(&toml_config_str)?; let parsed = toml::from_str::<TrainingModpackConfig>(&toml_config_str)?;
Ok(parsed) Ok(parsed)
} else { } else {
Err(Error::from(ErrorKind::NotFound).into()) Err(io::Error::from(io::ErrorKind::NotFound).into())
} }
} }
pub fn load_or_create() -> Result<TrainingModpackConfig> { pub fn load_or_create() -> Result<TrainingModpackConfig> {
match TrainingModpackConfig::load() { match TrainingModpackConfig::load() {
Ok(c) => Ok(c), Ok(c) => Ok(c),
Err(e)
if e.is::<Error>()
&& e.downcast_ref::<Error>().unwrap().kind() == ErrorKind::NotFound =>
{
TrainingModpackConfig::create_default()?;
TrainingModpackConfig::load()
}
Err(e) => { Err(e) => {
// Some other error, re-raise it if e.is::<io::Error>()
Err(e) && e.downcast_ref::<io::Error>().unwrap().kind() == io::ErrorKind::NotFound
{
// No config file exists already
TrainingModpackConfig::create_default()?;
TrainingModpackConfig::load()
} else if e.is::<toml::de::Error>() {
// 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 /// Returns Err if the file already exists
pub fn create_default() -> Result<()> { pub fn create_default() -> Result<()> {
if fs::metadata(TRAINING_MODPACK_TOML_PATH).is_ok() { if fs::metadata(TRAINING_MODPACK_TOML_PATH).is_ok() {
Err(Error::from(ErrorKind::AlreadyExists).into()) Err(io::Error::from(io::ErrorKind::AlreadyExists).into())
} else { } else {
let default_config: TrainingModpackConfig = TrainingModpackConfig::new(); let default_config: TrainingModpackConfig = TrainingModpackConfig::new();
let contents = toml::to_string(&default_config)?; let contents = toml::to_string(&default_config)?;