pub fn handle_action(state:&mut crate::state::AphroditeState, action:&str, name:&str) -> serde_json::Value {
match action {
"list" => {
let ephemeral:Vec<serde_json::Value> = state
.ephemeral_directives
.iter()
.map(|e| {
serde_json::json!({
"name": e.name,
"inline": e.inline,
"expires_after_turn": e.expires_after_turn,
})
})
.collect();
let mut available:Vec<&String> = state.directives.keys().collect();
available.sort();
serde_json::json!({
"available": available,
"active": &state.active_directives,
"ephemeral": ephemeral,
})
},
"swap" => {
if state.directives.contains_key(name) {
state.active_directives = vec![name.to_string()];
state.manual_directive_turn = Some(state.turn_counter);
serde_json::json!({"swapped": name, "active": &state.active_directives})
} else {
serde_json::json!({"error": format!("unknown directive: {}", name)})
}
},
"add" => {
if state.directives.contains_key(name) && !state.active_directives.contains(&name.to_string()) {
state.active_directives.push(name.to_string());
}
state.manual_directive_turn = Some(state.turn_counter);
serde_json::json!({"active": &state.active_directives})
},
"load" => {
if !state.directives.contains_key(name) {
return serde_json::json!({"error": format!("unknown directive: {}", name)});
}
if !state.active_directives.contains(&name.to_string()) {
state.active_directives.push(name.to_string());
}
state.manual_directive_turn = Some(state.turn_counter);
serde_json::json!({"loaded": name, "active": &state.active_directives})
},
"remove" => {
state.active_directives.retain(|d| d != name);
state.manual_directive_turn = Some(state.turn_counter);
serde_json::json!({"active": &state.active_directives})
},
"reset" => {
state.active_directives.clear();
state.ephemeral_directives.clear();
state.manual_directive_turn = None;
serde_json::json!({"active": &state.active_directives})
},
_ => serde_json::json!({"error": format!("unknown action: {} (use list|swap|add|load|remove|reset)", action)}),
}
}