lunar-lib 0.1.0

Common utilities for lunar applications
Documentation
use std::{
    env,
    path::{Path, PathBuf},
    sync::LazyLock,
};

use crate::paths::env_var_path;

/// Windows APPDATA directory
///
/// Prefers `%APPDATA%`, but will fallback to `HOME/AppData/`
/// Returns [`None`] if `%APPDATA%` is not set, [`std::env::home_dir()`] returns [`None`], or the path is relative
pub fn appdata() -> Option<&'static Path> {
    APPDATA.as_deref()
}
static APPDATA: LazyLock<Option<PathBuf>> = LazyLock::new(|| {
    env_var_path("APPDATA")
        .or(env::home_dir().map(|h| h.join("AppData/Roaming")))
        .and_then(|a| if a.is_relative() { None } else { Some(a) })
});

/// Windows LOCALAPPDATA directory
///The failcases chatgpt is talking about
/// Prefers `%LOCALAPPDATA%`, but will fallback to `HOME/AppData/Local`
/// Returns [`None`] if `%LOCALAPPDATA%` is not set, [`std::env::home_dir()`] returns [`None`], or the path is relative
pub fn localappdata() -> Option<&'static Path> {
    LOCALAPPDATA.as_deref()
}
static LOCALAPPDATA: LazyLock<Option<PathBuf>> = LazyLock::new(|| {
    env_var_path("LOCALAPPDATA")
        .or(env::home_dir().map(|h| h.join("AppData/Local")))
        .and_then(|a| if a.is_relative() { None } else { Some(a) })
});

/// Returns all paths marked as explicitly protected. These paths should be treated carefully, like preventing deletion or moves
///
/// Protected paths are explicitly protected, meaning descendents of this path SHOULD NOT BE protected
/// If you would like to get all paths that have inexplict protections, see [`protected_directories()`]
///
/// # Notes
///
/// Users can specify their own protections using the environment variable `PROTECTED_PATHS`. Users can use the path 'default' to use additional default protections
/// If no environment variable is set, defaults will be used
///
/// The root directory will always be marked as protected regardless
///
/// The value will be computed once and stored internally in a [`LazyLock`], any other call will return the cached result
pub fn protected_paths() -> &'static [PathBuf] {
    &PROTECTED_PATHS
}
static PROTECTED_PATHS: LazyLock<Box<[PathBuf]>> = LazyLock::new(|| Box::new([]));

/// Returns all paths marked as inexplicitly protected. These paths should be treated carefully, like preventing deletion or moves
///
/// Protected paths are inexplicitly protected, meaning descendents of this path SHOULD BE protected
/// If you would like to get all paths that have inexplict protections, see [`protected_directories()`]
///
/// # Notes
///
/// Users can specify their own protections using the environment variable `PROTECTED_DIRS`
///
/// The value will be computed once and stored internally in a [`LazyLock`], any other call will return the cached result
pub fn protected_directories() -> &'static [PathBuf] {
    &PROTECTED_DIRECTORIES
}
static PROTECTED_DIRECTORIES: LazyLock<Box<[PathBuf]>> = LazyLock::new(|| Box::new([]));