1
0
Fork 0
mirror of https://github.com/jugeeya/UltimateTrainingModpack.git synced 2024-11-27 20:34:03 +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 parking_lot::Mutex;
use serde_json::Value;
use std::io::{Error, ErrorKind};
use zip::ZipArchive;
lazy_static! {
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)]
@ -160,20 +159,12 @@ fn user_wants_to_install() -> bool {
}
fn get_current_version() -> Result<String> {
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::<Error>()
&& e.downcast_ref::<Error>().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)

View file

@ -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,32 +31,38 @@ impl TrainingModpackConfig {
let parsed = toml::from_str::<TrainingModpackConfig>(&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<TrainingModpackConfig> {
match TrainingModpackConfig::load() {
Ok(c) => Ok(c),
Err(e)
if e.is::<Error>()
&& e.downcast_ref::<Error>().unwrap().kind() == ErrorKind::NotFound =>
Err(e) => {
if e.is::<io::Error>()
&& e.downcast_ref::<io::Error>().unwrap().kind() == io::ErrorKind::NotFound
{
// No config file exists already
TrainingModpackConfig::create_default()?;
TrainingModpackConfig::load()
}
Err(e) => {
} 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)
}
}
}
}
/// Creates a default config and saves to file
/// 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)?;