use anyhow::anyhow;
use bevy::prelude::*;
use serde::{de::DeserializeOwned, Deserialize};
use super::execute_jj_command;
pub fn plugin(app: &mut App) {
trace!("Initializing plugin...");
app.insert_resource(init_config());
trace!("Plugin initialized.");
}
#[tracing::instrument(skip_all)]
fn init_config() -> Config {
let config_toml = execute_jj_command(vec!["config", "list"])
.map_err(|e| anyhow!("Failed to read config: {e}"))
.unwrap();
let root = toml::de::from_str::<ConfigRoot>(&config_toml)
.map_err(|e| anyhow!("Failed to parse config: {e}"))
.unwrap();
info!("{:?}", &root.jjj);
root.jjj
}
#[allow(unused)]
#[derive(Debug, Deserialize)]
struct ConfigRoot {
#[serde(default = "default_table")]
jjj: Config,
}
fn default_table<De: DeserializeOwned>() -> De {
toml::de::from_str::<De>("").unwrap()
}
#[derive(Debug, Deserialize, Reflect, Resource)]
#[serde(rename_all = "kebab-case")]
pub struct Config {
#[serde(default = "default_table")]
pub log: LogConfig,
#[serde(default = "default_table")]
pub splash: SplashConfig,
}
#[derive(Debug, Deserialize, Reflect)]
#[serde(rename_all = "kebab-case")]
pub struct LogConfig {
#[serde(default = "LogConfig::default_poll_interval_ms")]
pub poll_interval_ms: u64,
}
impl LogConfig {
fn default_poll_interval_ms() -> u64 {
1000
}
}
#[derive(Debug, Deserialize, Reflect)]
#[serde(rename_all = "kebab-case")]
pub struct SplashConfig {
#[serde(default)]
pub skip: bool,
#[serde(default = "SplashConfig::default_total_duration_ms")]
pub total_duration_ms: u64,
#[serde(default = "SplashConfig::default_line_interval_ms")]
pub line_interval_ms: u64,
}
impl SplashConfig {
fn default_total_duration_ms() -> u64 {
1950
}
fn default_line_interval_ms() -> u64 {
150
}
}