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 path = self.bin.join(name);
20        if path.exists() {
21            Self::ensure_absolute(path)
22        } else {
23            Err(PathError::BinaryNotFound {
24                name: name.to_string(),
25                searched: vec![path],
26            })
27        }
28    }
29
30    fn ensure_absolute(path: PathBuf) -> Result<PathBuf, PathError> {
31        if path.is_absolute() {
32            Ok(path)
33        } else {
34            std::fs::canonicalize(&path).map_err(|source| PathError::CanonicalizeFailed {
35                path,
36                field: "binary",
37                source,
38            })
39        }
40    }
41
42    pub fn binary_exists(&self, name: &str) -> bool {
43        self.resolve_binary(name).is_ok()
44    }
45
46    pub fn bin(&self) -> &Path {
47        &self.bin
48    }
49}