use serde::Deserialize;
use std::path::Path;
#[derive(Debug, Clone, Deserialize)]
pub struct ProbeRuleFile {
pub probe: Vec<ProbeTemplate>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct ProbeTemplate {
pub id: String,
pub variant: String,
pub description: String,
pub method: String,
pub path: String,
pub headers: Vec<HeaderTemplate>,
pub body: String,
pub requires_feature: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct HeaderTemplate {
pub name: String,
pub value: String,
}
pub fn load_templates<P: AsRef<Path>>(path: P) -> Result<ProbeRuleFile, RulesError> {
let contents = std::fs::read_to_string(path)?;
let file: ProbeRuleFile = toml::from_str(&contents)?;
Ok(file)
}
#[derive(Debug, thiserror::Error)]
pub enum RulesError {
#[error("io error: {0}")]
Io(#[from] std::io::Error),
#[error("toml parse error: {0}")]
Toml(#[from] toml::de::Error),
}
pub fn validate_template(t: &ProbeTemplate) -> Result<(), RulesError> {
if t.id.is_empty() || t.variant.is_empty() || t.method.is_empty() {
return Err(RulesError::Io(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"probe template missing required fields",
)));
}
Ok(())
}