#![allow(dead_code)]
use serde::Deserialize;
use super::manifest::DetectionManifest;
#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ComputeDetection {
pub detection: DetectionManifest,
pub compute: ComputeSection,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ComputeSection {
pub language: Language,
#[serde(default)]
pub source: Option<String>,
#[serde(default)]
pub body: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Language {
Rhai,
}
#[cfg(test)]
mod tests {
use super::*;
fn parse(toml: &str) -> ComputeDetection {
toml::from_str(toml).expect("a valid compute detection parses")
}
const MANIFEST: &str = r#"
[detection]
id = "example"
version = "1.0.0"
title = "Example"
[detection.when]
service = "http"
[detection.capabilities]
class = "passive"
"#;
#[test]
fn an_inline_body_reads_its_source() {
let detection = parse(&format!(
"{MANIFEST}\n[compute]\nlanguage = \"rhai\"\nsource = '''\nfn analyze(ctx, responses) {{ [] }}\n'''\n"
));
assert_eq!(detection.detection.id, "example");
assert_eq!(detection.compute.language, Language::Rhai);
assert!(
detection
.compute
.source
.as_deref()
.unwrap()
.contains("fn analyze")
);
assert!(detection.compute.body.is_none());
}
#[test]
fn a_file_body_reads_its_reference() {
let detection = parse(&format!(
"{MANIFEST}\n[compute]\nlanguage = \"rhai\"\nbody = \"example.rhai\"\n"
));
assert_eq!(detection.compute.body.as_deref(), Some("example.rhai"));
assert!(detection.compute.source.is_none());
}
}