Skip to main content

vanta_platform/
lib.rs

1//! `vanta-platform` — OS/arch detection and path/executable/shell helpers
2//! (`docs/17-cross-platform.md`). A thin, dependency-light utility layer the rest
3//! of the workspace builds on.
4#![forbid(unsafe_code)]
5
6use std::path::PathBuf;
7pub use vanta_core::Platform;
8
9/// The platform the running binary targets.
10pub fn detect() -> Platform {
11    Platform::current()
12}
13
14/// The user's home directory (`HOME`, or `USERPROFILE` on Windows).
15pub fn home_dir() -> Option<PathBuf> {
16    std::env::var_os("HOME")
17        .or_else(|| std::env::var_os("USERPROFILE"))
18        .map(PathBuf::from)
19}
20
21/// `$VANTA_HOME`, else `<home>/.vanta`.
22pub fn vanta_home() -> Option<PathBuf> {
23    if let Some(h) = std::env::var_os("VANTA_HOME") {
24        return Some(PathBuf::from(h));
25    }
26    home_dir().map(|h| h.join(".vanta"))
27}
28
29/// The PATH list separator (`;` on Windows, `:` elsewhere).
30pub fn path_list_sep() -> char {
31    if cfg!(windows) {
32        ';'
33    } else {
34        ':'
35    }
36}
37
38/// The executable file suffix (`.exe` on Windows, empty elsewhere).
39pub fn exe_suffix() -> &'static str {
40    if cfg!(windows) {
41        ".exe"
42    } else {
43        ""
44    }
45}
46
47/// Append the platform's executable suffix to `name` if not already present.
48pub fn with_exe(name: &str) -> String {
49    let suffix = exe_suffix();
50    if suffix.is_empty() || name.ends_with(suffix) {
51        name.to_string()
52    } else {
53        format!("{name}{suffix}")
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn exe_helpers_are_consistent() {
63        let n = with_exe("node");
64        if cfg!(windows) {
65            assert_eq!(n, "node.exe");
66            assert_eq!(with_exe("node.exe"), "node.exe"); // idempotent
67        } else {
68            assert_eq!(n, "node");
69        }
70    }
71
72    #[test]
73    fn detect_returns_a_platform() {
74        assert!(detect().token().contains('/'));
75    }
76}