Skip to main content

systemprompt_models/paths/
build.rs

1use std::path::{Path, PathBuf};
2
3use super::PathError;
4use crate::profile::PathsConfig;
5
6#[derive(Debug, Clone)]
7pub struct BuildPaths {
8    bin: PathBuf,
9}
10
11impl BuildPaths {
12    pub fn from_profile(paths: &PathsConfig) -> Self {
13        Self {
14            bin: PathBuf::from(&paths.bin),
15        }
16    }
17
18    pub fn resolve_binary(&self, name: &str) -> Result<PathBuf, PathError> {
19        let mut searched = Vec::new();
20
21        let exe_name = format!("{}{}", name, std::env::consts::EXE_SUFFIX);
22        let exe_path = self.bin.join(&exe_name);
23        searched.push(exe_path.clone());
24
25        if exe_path.exists() {
26            return Self::ensure_absolute(exe_path);
27        }
28
29        if !std::env::consts::EXE_SUFFIX.is_empty() {
30            let path = self.bin.join(name);
31            searched.push(path.clone());
32            if path.exists() {
33                return Self::ensure_absolute(path);
34            }
35        }
36
37        Err(PathError::BinaryNotFound {
38            name: name.to_string(),
39            searched,
40        })
41    }
42
43    fn ensure_absolute(path: PathBuf) -> Result<PathBuf, PathError> {
44        if path.is_absolute() {
45            Ok(path)
46        } else {
47            std::fs::canonicalize(&path).map_err(|source| PathError::CanonicalizeFailed {
48                path,
49                field: "binary",
50                source,
51            })
52        }
53    }
54
55    pub fn binary_exists(&self, name: &str) -> bool {
56        self.resolve_binary(name).is_ok()
57    }
58
59    pub fn bin(&self) -> &Path {
60        &self.bin
61    }
62}