use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum MediumType {
Codebase,
Filesystem,
Graph,
Git,
Web,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Source {
pub name: String,
#[serde(rename = "type")]
pub medium_type: MediumType,
pub pointer: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub change_detection: Option<String>,
#[serde(default)]
pub scope: Vec<PatternEntry>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub engagement: Option<serde_json::Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub preparation: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum PatternMode {
Allow,
Deny,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PatternEntry {
pub path: String,
pub mode: PatternMode,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum IngestTrigger {
Loop,
Manual,
OnEvent,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn source_round_trips_with_both_halves() {
let s = Source {
name: "source-tree".to_string(),
medium_type: MediumType::Codebase,
pointer: "../public".to_string(),
change_detection: None,
scope: vec![
PatternEntry {
path: "../public/**/*.rs".to_string(),
mode: PatternMode::Allow,
},
PatternEntry {
path: "../public/target/**".to_string(),
mode: PatternMode::Deny,
},
],
engagement: None,
preparation: None,
};
let json = serde_json::to_string(&s).unwrap();
assert!(json.contains(r#""type":"codebase""#), "got {json}");
assert!(json.contains(r#""mode":"deny""#), "got {json}");
for absent in ["change_detection", "engagement", "preparation"] {
assert!(!json.contains(absent), "unset {absent} omitted: {json}");
}
let back: Source = serde_json::from_str(&json).unwrap();
assert_eq!(back, s);
}
#[test]
fn source_optional_slots_round_trip_when_set() {
let s = Source {
name: "manual-pages".to_string(),
medium_type: MediumType::Filesystem,
pointer: "../docs".to_string(),
change_detection: Some("mtime".to_string()),
scope: Vec::new(),
engagement: Some(serde_json::json!({ "readVerb": "Read PDF" })),
preparation: Some("pdf-to-markdown".to_string()),
};
let json = serde_json::to_string(&s).unwrap();
assert!(json.contains(r#""change_detection":"mtime""#), "got {json}");
let back: Source = serde_json::from_str(&json).unwrap();
assert_eq!(back, s);
}
#[test]
fn ingest_trigger_uses_kebab_wire_forms() {
let on_event = serde_json::to_string(&IngestTrigger::OnEvent).unwrap();
assert_eq!(on_event, r#""on-event""#);
let loop_ = serde_json::to_string(&IngestTrigger::Loop).unwrap();
assert_eq!(loop_, r#""loop""#);
}
}