1use std::sync::Arc;
11
12use hidpp::{channel::HidppChannel, device::Device, feature::CreatableFeature};
13
14use crate::route::{DeviceRoute, open_route_channel};
15
16mod diagnostics;
17mod dpi;
18mod error;
19mod lighting;
20mod shared;
21mod smartshift;
22
23pub use diagnostics::{FeatureEntry, ReprogControlEntry, dump_features, dump_reprog_controls};
24pub use dpi::{DpiCapabilities, DpiInfo, get_dpi, get_dpi_info, set_dpi};
25pub use error::{HidppFeatureErrorKind, HidppOperation, WriteError};
26pub use lighting::{LightingMethod, set_keyboard_color, set_keyboard_color_with};
27pub use shared::{SharedChannel, set_dpi_on, set_smartshift_on, toggle_smartshift_on};
28pub use smartshift::{
29 get_smartshift_status, set_smartshift, set_smartshift_sensitivity, toggle_smartshift,
30};
31
32pub(crate) use error::classify_hidpp_error;
33
34pub(crate) async fn open_feature<F: CreatableFeature + 'static>(
43 device: &mut Device,
44) -> Result<Arc<F>, WriteError> {
45 let info = device
46 .root()
47 .get_feature(F::ID)
48 .await
49 .map_err(|e| classify_hidpp_error(e, HidppOperation::ResolveFeature, F::ID))?
50 .ok_or(WriteError::FeatureUnsupported { feature_hex: F::ID })?;
51 Ok(device.add_feature::<F>(info.index))
52}
53
54pub(crate) async fn with_route<F, Fut, T>(route: &DeviceRoute, f: F) -> Result<T, WriteError>
57where
58 F: FnOnce(Arc<HidppChannel>) -> Fut,
59 Fut: std::future::Future<Output = Result<T, WriteError>>,
60{
61 match open_route_channel(route).await? {
62 Some(channel) => f(channel).await,
63 None => Err(WriteError::DeviceNotFound),
64 }
65}
66
67#[cfg(test)]
68mod tests;