//! Standard environment implementation.

use super::Env;
use std::path::PathBuf;

/// Standard environment using `std::env`.
#[derive(Debug, Clone, Copy, Default)]
pub struct StdEnv;

impl StdEnv {
    /// Create a new standard environment accessor.
    #[must_use]
    pub const fn new() -> Self {
        Self
    }
}

impl Env for StdEnv {
    fn get(&self, key: &str) -> Option<String> {
        std::env::var(key).ok()
    }

    fn home_dir(&self) -> Option<PathBuf> {
        #[cfg(target_os = "windows")]
        {
            std::env::var("USERPROFILE")
                .ok()
                .map(PathBuf::from)
                .or_else(|| std::env::var("HOME").ok().map(PathBuf::from))
        }
        #[cfg(not(target_os = "windows"))]
        {
            std::env::var("HOME").ok().map(PathBuf::from)
        }
    }
}
