smn_rust_util 0.1.0

A collection of utility functions for Rust
Documentation
use std::env;

/// Retrieves the root path from the `ROOT_PATH` environment variable.
/// 
/// - If `ROOT_PATH` is set, its value is returned as a `String`.
/// - If `ROOT_PATH` is not set or retrieval fails, it defaults to the current working directory.
/// - If retrieving the current directory also fails, it defaults to `"./"`.
///
/// # Returns
///
/// - `String`: The root path as a `String`.
///
pub fn get_path_root() -> String {
    const ENV_VAR: &str = "ROOT_PATH";

    // Attempt to get the environment variable
    if let Ok(val) = env::var(ENV_VAR) {
        return val;
    }

    // Attempt to get the current working directory
    if let Ok(current_dir) = env::current_dir() {
        if let Some(path_str) = current_dir.to_str() {
            return path_str.to_string();
        }
    }

    // Fallback to "./" if all else fails
    "./".to_string()
}