par_term/acp_harness/binary_resolver.rs
1//! Binary path resolution for the ACP harness.
2//!
3//! Resolves the path to the `par-term` binary used to host the MCP server.
4//! Checks the explicit CLI override first, then the directory of the current
5//! executable, and finally the system PATH.
6
7use std::path::{Path, PathBuf};
8
9use par_term_acp::agents::resolve_binary_in_path;
10
11/// Resolve the `par-term` binary to use for hosting the MCP server.
12///
13/// Resolution order:
14/// 1. `explicit` — if provided, return it immediately (no existence check).
15/// 2. Same directory as the current executable — used in bundled/installed layouts.
16/// 3. System `PATH` lookup via `resolve_binary_in_path`.
17///
18/// Returns an error if none of the above succeed.
19pub fn resolve_par_term_binary(
20 explicit: Option<&Path>,
21) -> Result<PathBuf, Box<dyn std::error::Error + Send + Sync>> {
22 if let Some(path) = explicit {
23 return Ok(path.to_path_buf());
24 }
25
26 if let Ok(current) = std::env::current_exe()
27 && let Some(dir) = current.parent()
28 {
29 let candidate = dir.join(if cfg!(windows) {
30 "par-term.exe"
31 } else {
32 "par-term"
33 });
34 if candidate.is_file() {
35 return Ok(candidate);
36 }
37 }
38
39 if let Some(path) = resolve_binary_in_path("par-term") {
40 return Ok(path);
41 }
42
43 Err("Could not find `par-term` binary. Pass --par-term-bin /path/to/par-term".into())
44}