use crate::parallel_config::ParallelConfig;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
#[cfg(feature = "gpu")]
use crate::gpu::config::GpuConfig;
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct BlazeConfig {
#[serde(skip_serializing_if = "Option::is_none")]
pub parallel: Option<ParallelConfig>,
#[cfg(feature = "gpu")]
#[serde(skip_serializing_if = "Option::is_none")]
pub gpu: Option<GpuConfig>,
}
impl BlazeConfig {
pub fn load(path: &Path) -> Self {
let content = match std::fs::read_to_string(path) {
Ok(c) => c,
Err(_) => return Self::default(),
};
toml::from_str(&content).unwrap_or_default()
}
pub fn load_default() -> Self {
Self::load(&config_path())
}
pub fn save(&self, path: &Path) -> Result<()> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
let content = toml::to_string(self)?;
std::fs::write(path, content)?;
Ok(())
}
pub fn save_default(&self) -> Result<()> {
self.save(&config_path())
}
}
pub fn config_path() -> PathBuf {
config_dir().join("config.toml")
}
pub fn config_dir() -> PathBuf {
if let Ok(dir) = std::env::var("BLAZEHASH_CONFIG_DIR") {
return PathBuf::from(dir);
}
if let Some(cfg) = dirs::config_dir() {
cfg.join("blazehash")
} else {
PathBuf::from(".")
}
}