Skip to content

Commit

Permalink
create data dir when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Dec 27, 2024
1 parent e4446b0 commit 1f5e6fa
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions pumpkin/src/data/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::{fs, path::Path};
use std::{env, fs, path::Path};

use serde::{Deserialize, Serialize};

const DATA_FOLDER: &str = "data/";

pub mod op_data;

pub trait LoadJSONConfiguration {
Expand All @@ -10,23 +12,29 @@ pub trait LoadJSONConfiguration {
where
Self: Sized + Default + Serialize + for<'de> Deserialize<'de>,
{
let path = Self::get_path();
let exe_dir = env::current_dir().unwrap();
let data_dir = exe_dir.join(DATA_FOLDER);
if !data_dir.exists() {
log::debug!("creating new data root folder");
fs::create_dir(&data_dir).expect("Failed to create data root folder");
}
let path = data_dir.join(Self::get_path());

let config = if path.exists() {
let file_content = fs::read_to_string(path)
let file_content = fs::read_to_string(&path)
.unwrap_or_else(|_| panic!("Couldn't read configuration file at {path:?}"));

serde_json::from_str(&file_content).unwrap_or_else(|err| {
panic!(
"Couldn't parse config at {path:?}. Reason: {err}. This is probably caused by a config update. Just delete the old config and restart.",
"Couldn't parse data config at {path:?}. Reason: {err}. This is probably caused by a config update. Just delete the old data config and restart.",
)
})
} else {
let content = Self::default();

if let Err(err) = fs::write(path, serde_json::to_string_pretty(&content).unwrap()) {
if let Err(err) = fs::write(&path, serde_json::to_string_pretty(&content).unwrap()) {
eprintln!(
"Couldn't write default config to {path:?}. Reason: {err}. This is probably caused by a config update. Just delete the old config and restart.",
"Couldn't write default data config to {path:?}. Reason: {err}. This is probably caused by a config update. Just delete the old data config and restart.",
);
}

Expand All @@ -49,21 +57,27 @@ pub trait SaveJSONConfiguration: LoadJSONConfiguration {
where
Self: Sized + Default + Serialize + for<'de> Deserialize<'de>,
{
let path = <Self as LoadJSONConfiguration>::get_path();
let exe_dir = env::current_dir().unwrap();
let data_dir = exe_dir.join(DATA_FOLDER);
if !data_dir.exists() {
log::debug!("creating new data root folder");
fs::create_dir(&data_dir).expect("Failed to create data root folder");
}
let path = data_dir.join(Self::get_path());

let content = match serde_json::to_string_pretty(self) {
Ok(content) => content,
Err(err) => {
log::warn!(
"Couldn't serialize operator config to {:?}. Reason: {}",
"Couldn't serialize operator data config to {:?}. Reason: {}",
path,
err
);
return;
}
};

if let Err(err) = std::fs::write(path, content) {
if let Err(err) = std::fs::write(&path, content) {
log::warn!(
"Couldn't write operator config to {:?}. Reason: {}",
path,
Expand Down

0 comments on commit 1f5e6fa

Please sign in to comment.