windows_eco/
lib.rs

1use core::error::Error as StdError;
2use core::fmt;
3
4use windows::Win32::System::Threading::PROCESS_INFORMATION_CLASS;
5
6pub mod process;
7
8/// Constant for ProcessPowerThrottling enum variant
9pub const PROCESS_POWER_THROTTLING: PROCESS_INFORMATION_CLASS = PROCESS_INFORMATION_CLASS(4);
10/// NT build number for Windows 11 22H2 where the power throttling API first introduced
11pub(crate) const WIN11_22H2: u32 = 22621;
12
13/// Error variants.
14pub enum Error {
15    /// The power throttling API is not available on this Windows version
16    ///
17    /// The associated `u32` value is the NT build number
18    NotAvailable(u32),
19    /// A Windows API call returned an error.
20    Windows(windows::core::Error),
21    /// An error originating from the [`winver`](https://crates.io/crates/winver) crate.
22    WinVer(winver::Error),
23}
24
25impl fmt::Display for Error {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            Error::NotAvailable(winver) => {
29                write!(f, "Power throttling API is not available in the version: {winver}")
30            }
31            Error::Windows(err) => {
32                write!(f, "Windows error: {}", err)
33            }
34            Error::WinVer(err) => {
35                write!(f, "WinVer error: {}", err)
36            }
37        }
38    }
39}
40
41impl fmt::Debug for Error {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        match self {
44            Error::NotAvailable(winver) => f.debug_tuple("NotAvailable").field(winver).finish(),
45            Error::Windows(err) => f.debug_tuple("Windows").field(err).finish(),
46            Error::WinVer(err) => f.debug_tuple("WinVer").field(err).finish(),
47        }
48    }
49}
50
51impl StdError for Error {
52    fn source(&self) -> Option<&(dyn StdError + 'static)> {
53        match self {
54            Error::NotAvailable(_) => None,
55            Error::Windows(err) => Some(err),
56            Error::WinVer(err) => Some(err),
57        }
58    }
59}
60
61impl From<windows::core::Error> for Error {
62    fn from(err: windows::core::Error) -> Self {
63        Error::Windows(err)
64    }
65}
66
67impl From<winver::Error> for Error {
68    fn from(err: winver::Error) -> Self {
69        Error::WinVer(err)
70    }
71}