use std::fs;
use serde::{Deserialize, Serialize};
use crate::cli::Java;
use crate::home::jvr_config_json_path;
#[derive(Serialize, Deserialize, Debug)]
pub struct Configuration {
#[serde(skip_serializing_if = "Option::is_none")]
pub java_home_path: Option<String>,
pub current: Option<String>,
pub versions: Vec<Java>,
}
impl Configuration {
pub fn new() -> Self {
let config_path = jvr_config_json_path();
if config_path.exists() {
let config_content =
fs::read_to_string(config_path).expect("Failed to read config file");
serde_json::from_str(&config_content).expect("Failed to parse config file")
} else {
Configuration {
java_home_path: None,
versions: Vec::new(),
current: None,
}
}
}
pub fn store(&self) {
let config_path = jvr_config_json_path();
let config_content =
serde_json::to_string_pretty(self).expect("Failed to serialize config");
fs::write(config_path, config_content).expect("Failed to write config file");
}
}
impl Default for Configuration {
fn default() -> Self {
Self::new()
}
}