use std::io::{BufRead, BufReader, Write};
use std::process::{Child, ChildStdin, ChildStdout, Command, Stdio};
use serde::Serialize;
use crate::plugin::protocol::{CoreRequest, Event};
pub struct PluginProcess {
pub program: String,
child: Child,
stdin: ChildStdin,
stdout: BufReader<ChildStdout>,
}
impl PluginProcess {
pub fn start(program: &str) -> Result<Self, String> {
Self::start_command(program, &["serve"])
}
pub fn start_command(program: &str, args: &[&str]) -> Result<Self, String> {
let mut child = Command::new(program)
.args(args)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.spawn()
.map_err(|e| format!("{program}: {e}"))?;
let stdin = child.stdin.take().ok_or("no stdin")?;
let stdout = BufReader::new(child.stdout.take().ok_or("no stdout")?);
Ok(Self { program: program.to_string(), child, stdin, stdout })
}
pub fn alive(&mut self) -> bool {
matches!(self.child.try_wait(), Ok(None))
}
pub fn request<R: Serialize>(
&mut self,
req: &R,
progress: &mut dyn FnMut(&str, Option<f32>),
) -> Result<serde_json::Value, String> {
let line = serde_json::to_string(req).map_err(|e| e.to_string())?;
writeln!(self.stdin, "{line}")
.and_then(|_| self.stdin.flush())
.map_err(|e| format!("{}: {e}", self.program))?;
loop {
let mut buf = String::new();
let n = self.stdout.read_line(&mut buf).map_err(|e| e.to_string())?;
if n == 0 {
return Err(format!("{} stopped (see its messages in the terminal)", self.program));
}
let Ok(event) = serde_json::from_str::<Event>(buf.trim()) else { continue };
match event {
Event::Progress { stage, fraction } => progress(&stage, fraction),
Event::Done { result } => return Ok(result),
Event::Error { message } => return Err(message),
}
}
}
}
impl Drop for PluginProcess {
fn drop(&mut self) {
let _ = writeln!(self.stdin, "{}", serde_json::to_string(&CoreRequest::Quit).unwrap_or_default());
let _ = self.stdin.flush();
let _ = self.child.kill();
let _ = self.child.wait();
}
}