use anyhow::Result;
use serde_json::{json, Value};
use super::STEER_EVENTS;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum RecipeShell {
Bash,
Powershell,
}
const NOTE: [&str; 2] = [
"Paste the block above into the `hooks` object of a settings.json you own (user, \
project or local scope); the eight events are the delivery points, and slot k emits \
chunk k of what is waiting there.",
"csift never writes a settings file: arming the channel is your edit, and `csift \
deliver` only ever writes its own sidecar directory.",
];
pub(crate) fn fragment(slots: u32, shell: RecipeShell) -> Value {
let mut hooks = serde_json::Map::new();
for event in STEER_EVENTS {
let entries: Vec<Value> = (1..=slots).map(|k| entry(k, shell)).collect();
hooks.insert(event.to_string(), json!([{ "hooks": entries }]));
}
json!({ "hooks": Value::Object(hooks) })
}
fn entry(slot: u32, shell: RecipeShell) -> Value {
match shell {
RecipeShell::Bash => json!({
"type": "command",
"command": format!("csift deliver --slot {slot}"),
}),
RecipeShell::Powershell => json!({
"type": "command",
"shell": "powershell",
"command": format!("csift deliver --slot {slot}"),
}),
}
}
pub(crate) fn run_recipe(slots: u32, shell: RecipeShell) -> Result<()> {
println!("{}", serde_json::to_string_pretty(&fragment(slots, shell))?);
for line in NOTE {
eprintln!("{line}");
}
Ok(())
}