use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SpawnTaskAction {
pub goal: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub workflow: Option<String>,
#[serde(default)]
pub inherit: SpawnInherit,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SpawnTasksAction {
pub from_var: String,
pub json_path: String,
pub mapping: SpawnMapping,
#[serde(default)]
pub inherit: SpawnInherit,
#[serde(default = "default_max_tasks")]
pub max_tasks: usize,
#[serde(default = "default_true")]
pub queue: bool,
}
fn default_max_tasks() -> usize {
5
}
fn default_true() -> bool {
true
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SpawnMapping {
pub goal: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub workflow: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SpawnInherit {
#[serde(default = "default_inherit_true")]
pub workspace: bool,
#[serde(default = "default_inherit_true")]
pub project: bool,
#[serde(default)]
pub target_files: bool,
}
fn default_inherit_true() -> bool {
true
}
impl Default for SpawnInherit {
fn default() -> Self {
Self {
workspace: true,
project: true,
target_files: false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_spawn_task_action_minimal() {
let json = r#"{"goal": "improve {area}"}"#;
let action: SpawnTaskAction = serde_json::from_str(json).expect("deserialize spawn task");
assert_eq!(action.goal, "improve {area}");
assert!(action.workflow.is_none());
assert!(action.inherit.workspace);
assert!(action.inherit.project);
assert!(!action.inherit.target_files);
}
#[test]
fn test_spawn_tasks_action_defaults() {
let json = r#"{
"from_var": "goals_output",
"json_path": "$.goals",
"mapping": {"goal": "$.description"}
}"#;
let action: SpawnTasksAction = serde_json::from_str(json).expect("deserialize spawn tasks");
assert_eq!(action.max_tasks, 5);
assert!(action.queue);
assert!(action.inherit.workspace);
}
#[test]
fn test_spawn_inherit_default() {
let inherit = SpawnInherit::default();
assert!(inherit.workspace);
assert!(inherit.project);
assert!(!inherit.target_files);
}
#[test]
fn test_spawn_tasks_action_full() {
let json = r#"{
"from_var": "analysis",
"json_path": "$.tasks",
"mapping": {
"goal": "$.goal",
"workflow": "$.workflow",
"name": "$.name"
},
"inherit": {"workspace": true, "project": false, "target_files": true},
"max_tasks": 10,
"queue": false
}"#;
let action: SpawnTasksAction =
serde_json::from_str(json).expect("deserialize full spawn tasks");
assert_eq!(action.max_tasks, 10);
assert!(!action.queue);
assert!(!action.inherit.project);
assert!(action.inherit.target_files);
}
}