use tokio_util::sync::CancellationToken;
use tracing::instrument;
use wayle_traits::Reactive;
use zbus::{Connection, zvariant::OwnedObjectPath};
use crate::{
core::device::{Device, types::LiveDeviceParams},
error::Error,
service::BatteryService,
};
pub struct BatteryServiceBuilder {
device_path: Option<OwnedObjectPath>,
}
impl BatteryServiceBuilder {
pub fn new() -> Self {
Self { device_path: None }
}
pub fn device_path(mut self, path: impl Into<OwnedObjectPath>) -> Self {
self.device_path = Some(path.into());
self
}
#[instrument(skip_all)]
pub async fn build(self) -> Result<BatteryService, Error> {
let device_path = if let Some(path) = self.device_path {
path
} else {
OwnedObjectPath::try_from("/org/freedesktop/UPower/devices/DisplayDevice")?
};
let connection = Connection::system().await?;
let cancellation_token = CancellationToken::new();
let device = Device::get_live(LiveDeviceParams {
connection: &connection,
device_path: &device_path,
cancellation_token: &cancellation_token,
})
.await?;
Ok(BatteryService {
device,
cancellation_token,
})
}
}
impl Default for BatteryServiceBuilder {
fn default() -> Self {
Self::new()
}
}