Files
momentry_core/src/core/path_utils.rs
T
Accusys 61b8fb3b52 feat: add models_dir() to path_utils (A-4.2 P3)
Purpose: Support health.rs models path resolution

Fallback: MOMENTRY_MODELS_DIR or relative to binary

Verification: cargo check --lib passes
2026-10-04 16:41:00 +08:00

43 lines
1.3 KiB
Rust

use std::path::PathBuf;
/// Resolve asset path at runtime (not compile-time).
///
/// Priority:
/// 1. Explicit environment variable (e.g., MOMENTRY_SCRIPTS_DIR)
/// 2. Sibling of current_exe (same directory as running binary)
/// 3. Relative path fallback (dev machine)
///
/// This avoids CARGO_MANIFEST_DIR being baked into the binary at compile time,
/// which breaks on binary-only release hosts.
pub fn resolve_asset_path(env_var: &str, fallback_relative: &str) -> PathBuf {
std::env::var_os(env_var)
.map(PathBuf::from)
.filter(|p| p.exists())
.or_else(|| {
std::env::current_exe()
.ok()
.and_then(|p| p.parent().map(|d| d.join(fallback_relative)))
.filter(|p| p.exists())
})
.unwrap_or_else(|| PathBuf::from(fallback_relative))
}
/// Get scripts directory.
pub fn scripts_dir() -> PathBuf {
resolve_asset_path("MOMENTRY_SCRIPTS_DIR", "scripts")
}
/// Get CLI tools directory.
pub fn cli_dir() -> PathBuf {
resolve_asset_path("MOMENTRY_CLI_DIR", "tools/cli")
}
/// Get config directory.
pub fn config_dir() -> PathBuf {
resolve_asset_path("MOMENTRY_CONFIG_DIR", "config")
}
/// Get models directory.
pub fn models_dir() -> PathBuf {
resolve_asset_path("MOMENTRY_MODELS_DIR", "models")
}