troll 0.3.2

The port manager that guards the bridge: permanent claims, expiring leases, and a live view of who listens where.
use std::{env, ops::RangeInclusive, path::PathBuf};

use troll_core::ProbeError;
use troll_probe::SystemProbe;

use crate::error::CliError;

use super::unix::troll_home;

pub fn config_home() -> Result<PathBuf, CliError> {
    troll_home(
        env::var_os("XDG_CONFIG_HOME").as_deref(),
        env::var_os("HOME").as_deref(),
        ".config",
    )
    .ok_or(CliError::MissingConfigHome)
}

pub fn data_home() -> Result<PathBuf, CliError> {
    troll_home(
        env::var_os("XDG_DATA_HOME").as_deref(),
        env::var_os("HOME").as_deref(),
        ".local/share",
    )
    .ok_or(CliError::MissingDataHome)
}

pub fn state_home() -> Result<PathBuf, CliError> {
    troll_home(
        env::var_os("XDG_STATE_HOME").as_deref(),
        env::var_os("HOME").as_deref(),
        ".local/state",
    )
    .ok_or(CliError::MissingStateHome)
}

pub fn require_listener_probe(_probe: &SystemProbe) -> Result<(), ProbeError> {
    Ok(())
}

pub fn setup_ephemeral_range() -> Result<Option<RangeInclusive<u16>>, ProbeError> {
    #[cfg(debug_assertions)]
    if let Some(probe) = fixture_probe() {
        return probe.ephemeral_range();
    }
    troll_probe::ephemeral_range()
}

#[cfg(debug_assertions)]
fn fixture_probe() -> Option<SystemProbe> {
    env::var_os("TROLL_TEST_PROC_ROOT").map(|path| SystemProbe::with_proc_root(PathBuf::from(path)))
}

pub fn proc_probe() -> SystemProbe {
    // CLI integration tests use an isolated procfs fixture. Release builds
    // always read the real kernel tables and ephemeral port range.
    #[cfg(debug_assertions)]
    if let Some(probe) = fixture_probe() {
        return probe;
    }
    SystemProbe::new()
}