ipcez 0.1.0

Rust library for ipcez.
Documentation
//! OS detection from the `os` environment variable.
//!
//! Single responsibility: detect operating system (Linux or Windows) from environment.

use std::env;
use std::error::Error;
use std::fmt;

/// Detected operating system from the environment.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OsKind {
    Linux,
    Windows,
}

/// Error when detecting OS from the `os` environment variable.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OsDetectionError {
    /// The `os` environment variable was not set.
    NotFound,
    /// The `os` environment variable had an invalid value.
    InvalidValue(String),
}

impl fmt::Display for OsDetectionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            OsDetectionError::NotFound => {
                write!(f, "environment variable \"os\" was not found")
            }
            OsDetectionError::InvalidValue(v) => {
                write!(
                    f,
                    "environment variable \"os\" had invalid value \"{}\"; expected \"linux\" or \"windows\"",
                    v
                )
            }
        }
    }
}

impl Error for OsDetectionError {}

/// Detects the OS by reading the `os` environment variable.
///
/// Expects the variable to be set to `"linux"` or `"windows"` (case-insensitive).
/// Returns an error if the variable is missing or has any other value.
pub fn detect_os_from_env() -> Result<OsKind, OsDetectionError> {
    let raw = env::var("os").map_err(|_| OsDetectionError::NotFound)?;
    let value = raw.trim().to_lowercase();
    match value.as_str() {
        "linux" => Ok(OsKind::Linux),
        "windows" => Ok(OsKind::Windows),
        _ => Err(OsDetectionError::InvalidValue(raw)),
    }
}