iwconf-win 0.1.2

A colorful, iwconfig-style WLAN CLI for Windows, backed by the native WLAN API (no netsh required).
use config_get::ConfigGet;

use crate::error::Result;

/// User-tunable defaults, loaded from a `config.toml`/`.env`/`.ini` that
/// `config-get` auto-discovers under `%APPDATA%\iwconf\` /
/// `%USERPROFILE%\iwconf\` on Windows (and the XDG-ish equivalents
/// elsewhere, for anyone hacking on this off-Windows).
#[derive(Debug, Clone)]
pub struct Settings {
    /// Default interface pattern to use when `-i` isn't given.
    pub default_iface: Option<String>,
    /// Disable colors even without `--no-color`.
    pub no_color: bool,
    /// Disable emoji even without `--no-emoji`.
    pub no_emoji: bool,
    /// Seconds to wait for a connect attempt before giving up.
    pub connect_timeout: u64,
}

impl Default for Settings {
    fn default() -> Self {
        Self {
            default_iface: None,
            no_color: false,
            no_emoji: false,
            connect_timeout: 15,
        }
    }
}

/// Loads settings from disk if a config file exists; falls back to
/// built-in defaults silently (a missing config is not an error — most
/// users will never create one).
pub fn load() -> Result<Settings> {
    let mut settings = Settings::default();

    let cfg = match ConfigGet::builder("iwconf").config_dir("iwconf").build() {
        Ok(cfg) => cfg,
        Err(config_get::ConfigError::NotFound(_)) => return Ok(settings),
        Err(e) => return Err(e.into()),
    };

    let iface = cfg.get_in_or("iwconf", "default_iface", "");
    if !iface.is_empty() {
        settings.default_iface = Some(iface.to_string());
    }

    settings.no_color = cfg
        .get_in_or("iwconf", "no_color", "false")
        .eq_ignore_ascii_case("true");
    settings.no_emoji = cfg
        .get_in_or("iwconf", "no_emoji", "false")
        .eq_ignore_ascii_case("true");

    if let Ok(timeout) = cfg.parse_in::<u64>("iwconf", "connect_timeout") {
        settings.connect_timeout = timeout;
    }

    Ok(settings)
}