#![deny(warnings)]
#![deny(missing_docs)]
#![cfg_attr(docsrs, feature(doc_cfg))]
use std::error::Error as StdError;
use thiserror::Error;
pub mod blocking;
#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
cfg_if::cfg_if! {
if #[cfg(target_os = "linux")] {
mod linux;
use self::linux as platform;
} else if #[cfg(windows)] {
pub mod windows;
use self::windows as platform;
} else {
compile_error!("unsupported platform");
}
}
#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
mod r#async {
use super::{Error, platform};
use futures::{Stream, StreamExt};
use std::future::Future;
pub trait Brightness {
fn device_name(&self) -> impl Future<Output = Result<String, Error>> + Send;
fn friendly_device_name(&self) -> impl Future<Output = Result<String, Error>> + Send;
fn get(&self) -> impl Future<Output = Result<u32, Error>> + Send;
fn set(&mut self, percentage: u32) -> impl Future<Output = Result<(), Error>> + Send;
}
#[derive(Debug)]
pub struct BrightnessDevice(pub(crate) platform::AsyncDeviceImpl);
impl Brightness for BrightnessDevice {
async fn device_name(&self) -> Result<String, Error> {
self.0.device_name().await
}
async fn friendly_device_name(&self) -> Result<String, Error> {
self.0.friendly_device_name().await
}
async fn get(&self) -> Result<u32, Error> {
self.0.get().await
}
async fn set(&mut self, percentage: u32) -> Result<(), Error> {
self.0.set(percentage).await
}
}
pub fn brightness_devices() -> impl Stream<Item = Result<BrightnessDevice, Error>> {
platform::brightness_devices().map(|r| r.map(BrightnessDevice).map_err(Into::into))
}
}
#[cfg(feature = "async")]
pub use r#async::{Brightness, BrightnessDevice, brightness_devices};
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum Error {
#[error("Failed to list brightness devices")]
ListingDevices(#[source] Box<dyn StdError + Send + Sync>),
#[error("Failed to get brightness device {device} information")]
GettingDeviceInfo {
device: String,
source: Box<dyn StdError + Send + Sync>,
},
#[error("Setting brightness failed for device {device}")]
SettingBrightness {
device: String,
source: Box<dyn StdError + Send + Sync>,
},
}