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 primary = self.bin.join(&exe_name);
23        searched.push(primary.clone());
24
25        let sibling = self.sibling_bin_path();
26        let alt = sibling.as_ref().map(|s| s.join(&exe_name));
27        if let Some(ref alt_path) = alt {
28            searched.push(alt_path.clone());
29        }
30
31        let alt_exists = alt.as_ref().filter(|p| p.exists());
32
33        match (primary.exists(), alt_exists) {
34            (true, Some(alt_path)) => {
35                let primary_mtime = std::fs::metadata(&primary).and_then(|m| m.modified()).ok();
36                let alt_mtime = std::fs::metadata(alt_path).and_then(|m| m.modified()).ok();
37                match (primary_mtime, alt_mtime) {
38                    (Some(p), Some(a)) if a > p => Self::ensure_absolute(alt_path.clone()),
39                    _ => Self::ensure_absolute(primary),
40                }
41            },
42            (true, None) => Self::ensure_absolute(primary),
43            (false, Some(alt_path)) => Self::ensure_absolute(alt_path.clone()),
44            (false, None) => {
45                if !std::env::consts::EXE_SUFFIX.is_empty() {
46                    let path = self.bin.join(name);
47                    searched.push(path.clone());
48                    if path.exists() {
49                        return Self::ensure_absolute(path);
50                    }
51                }
52                Err(PathError::BinaryNotFound {
53                    name: name.to_string(),
54                    searched,
55                })
56            },
57        }
58    }
59
60    fn sibling_bin_path(&self) -> Option<PathBuf> {
61        let dir_name = self.bin.file_name()?.to_str()?;
62        let sibling_name = match dir_name {
63            "release" => "debug",
64            "debug" => "release",
65            _ => return None,
66        };
67        Some(self.bin.with_file_name(sibling_name))
68    }
69
70    fn ensure_absolute(path: PathBuf) -> Result<PathBuf, PathError> {
71        if path.is_absolute() {
72            Ok(path)
73        } else {
74            std::fs::canonicalize(&path).map_err(|source| PathError::CanonicalizeFailed {
75                path,
76                field: "binary",
77                source,
78            })
79        }
80    }
81
82    pub fn binary_exists(&self, name: &str) -> bool {
83        self.resolve_binary(name).is_ok()
84    }
85
86    pub fn bin(&self) -> &Path {
87        &self.bin
88    }
89}