mod rules;
mod schemas;
mod validate;
use crate::error::{Mecha10Error, Result};
use anyhow::Context as _;
use rules::validate_custom_rules;
use serde_json::Value;
use std::fs;
use std::path::Path;
use tracing::info;
pub use schemas::{get_node_config_schema, get_project_schema};
pub use validate::{validate_node_config_json, validate_project_config, validate_project_json};
pub fn load_and_validate_project<P: AsRef<Path>>(path: P) -> Result<Value> {
let path = path.as_ref();
let content = fs::read_to_string(path)
.with_context(|| format!("Failed to read config file: {}", path.display()))
.map_err(|e| Mecha10Error::Configuration(format!("{:#}", e)))?;
let config: Value = serde_json::from_str(&content)
.with_context(|| format!("Failed to parse JSON: {}", path.display()))
.map_err(|e| Mecha10Error::Configuration(format!("{:#}", e)))?;
validate_project_json(&config)?;
validate_custom_rules(&config)?;
Ok(config)
}
pub fn export_project_schema<P: AsRef<Path>>(path: P) -> Result<()> {
let schema = get_project_schema();
let json = serde_json::to_string_pretty(&schema)
.map_err(|e| Mecha10Error::Configuration(format!("Failed to serialize schema: {}", e)))?;
fs::write(path.as_ref(), json)
.with_context(|| format!("Failed to write schema to: {}", path.as_ref().display()))
.map_err(|e| Mecha10Error::Configuration(format!("{:#}", e)))?;
info!("Exported schema to: {}", path.as_ref().display());
Ok(())
}