windows-eco 0.1.0

A safe, idiomatic Rust wrapper for the Windows Power Throttling API.
Documentation
use core::error::Error as StdError;
use core::fmt;

use windows::Win32::System::Threading::PROCESS_INFORMATION_CLASS;

pub mod process;

/// Constant for ProcessPowerThrottling enum variant
pub const PROCESS_POWER_THROTTLING: PROCESS_INFORMATION_CLASS = PROCESS_INFORMATION_CLASS(4);
/// NT build number for Windows 11 22H2 where the power throttling API first introduced
pub(crate) const WIN11_22H2: u32 = 22621;

/// Error variants.
pub enum Error {
    /// The power throttling API is not available on this Windows version
    ///
    /// The associated `u32` value is the NT build number
    NotAvailable(u32),
    /// A Windows API call returned an error.
    Windows(windows::core::Error),
    /// An error originating from the [`winver`](https://crates.io/crates/winver) crate.
    WinVer(winver::Error),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::NotAvailable(winver) => {
                write!(f, "Power throttling API is not available in the version: {winver}")
            }
            Error::Windows(err) => {
                write!(f, "Windows error: {}", err)
            }
            Error::WinVer(err) => {
                write!(f, "WinVer error: {}", err)
            }
        }
    }
}

impl fmt::Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::NotAvailable(winver) => f.debug_tuple("NotAvailable").field(winver).finish(),
            Error::Windows(err) => f.debug_tuple("Windows").field(err).finish(),
            Error::WinVer(err) => f.debug_tuple("WinVer").field(err).finish(),
        }
    }
}

impl StdError for Error {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match self {
            Error::NotAvailable(_) => None,
            Error::Windows(err) => Some(err),
            Error::WinVer(err) => Some(err),
        }
    }
}

impl From<windows::core::Error> for Error {
    fn from(err: windows::core::Error) -> Self {
        Error::Windows(err)
    }
}

impl From<winver::Error> for Error {
    fn from(err: winver::Error) -> Self {
        Error::WinVer(err)
    }
}