CyberModManager/src/lib.rs

65 lines
2.0 KiB
Rust
Raw Normal View History

2023-07-09 16:53:01 -07:00
use std::{fs, path::Path};
use serde::{Deserialize, Serialize};
pub struct ConfigFile {
pub latest_date: String,
pub cache_folder: String,
}
pub fn get_home() -> String {
let home = match std::env::consts::OS {
"linux" => std::env::var("HOME").expect("how is there not a $HOME"),
"windows" => std::env::var("userprofile")
.expect("yell at me if this doesn't work please i'm not going to touch windows"),
_ => std::env::var("HOME").expect(
"if this doesn't work, you're probably on macos, which i literally cannot test on",
),
};
// println!("{home}");
home
}
pub fn create_config(file: &String) -> bool {
let replaced = file.replace('~', &get_home()[..]); // replace ~ with $HOME
let mut paths = replaced.split('/').collect::<Vec<&str>>(); // split path by /
paths.pop();
let path = paths.join("/");
let file_path = std::path::Path::new(&path); // Path without the filename
if let Some(_) = file_path.parent() {
// if the file_path has a parent folder
fs::create_dir_all(&path).expect("couldn't create needed folders") // create folders
};
if file_path.exists() && !Path::new(&replaced).exists() {
let file_contents = "{{ \"archives\": \"{{CONFIG}/CyberModManager/Archives\", \"folders\": \"{{CONFIG}/CyberModManager/Mods\", \"game_folder\": \"{{STEAM}/steamapps/common/Cyberpunk 2077\"}";
fs::write(replaced, file_contents).expect("couldn't write to config file");
true
} else {
false
}
}
pub fn get_steam_location() -> () {
}
pub fn read_config(file: &String) -> Config {
let read = fs::read_to_string(file).expect("couldn't read config file");
serde_json::from_str(&read[..]).expect("couldn't parse json")
}
pub fn ensure_exists(path: String) {
let p = Path::new(&path);
if !p.exists() {
fs::create_dir_all(p).expect("couldn't create folder")
}
}
#[derive(Serialize, Deserialize)]
pub struct Config {
pub latest_date: String,
pub cache_folder: String,
}