cue_sdk/property/
boolean.rs

1use cue_sdk_sys as ffi;
2use num_traits::ToPrimitive;
3
4use super::RefreshValueError;
5use crate::device::DeviceIndex;
6use crate::errors::get_last_error;
7use crate::internal::CuePropertyValueHolder;
8
9/// A `boolean` property that can be refreshed to "check" the property at any point.
10#[derive(Debug, Clone, PartialEq)]
11pub struct BooleanProperty {
12    pub key: BooleanPropertyKey,
13    device_index: DeviceIndex,
14    pub last_value: bool,
15}
16
17impl BooleanProperty {
18    pub(crate) fn new(
19        device_index: DeviceIndex,
20        key: BooleanPropertyKey,
21        initial_value: bool,
22    ) -> Self {
23        BooleanProperty {
24            device_index,
25            key,
26            last_value: initial_value,
27        }
28    }
29
30    pub fn refresh_value(&mut self) -> Result<(), RefreshValueError> {
31        let mut new_value_holder = CuePropertyValueHolder::<bool>::new();
32        let was_successful = unsafe {
33            ffi::CorsairGetBoolPropertyValue(
34                self.device_index as i32,
35                self.key.into(),
36                new_value_holder.mut_ptr(),
37            )
38        };
39        if was_successful {
40            let updated_value = new_value_holder.value();
41            self.last_value = updated_value;
42            Ok(())
43        } else {
44            Err(RefreshValueError(get_last_error()))
45        }
46    }
47}
48
49/// The valid keys that some devices support for `boolean` property lookups.
50#[derive(Debug, Clone, Copy, ToPrimitive, FromPrimitive, PartialEq)]
51#[cfg_attr(test, derive(EnumIter))]
52pub enum BooleanPropertyKey {
53    HeadsetMicEnabled = ffi::CorsairDevicePropertyId_CDPI_Headset_MicEnabled as isize,
54    HeadsetSurroundSoundEnabled =
55        ffi::CorsairDevicePropertyId_CDPI_Headset_SurroundSoundEnabled as isize,
56    HeadsetSidetoneEnabled = ffi::CorsairDevicePropertyId_CDPI_Headset_SidetoneEnabled as isize,
57}
58
59impl From<BooleanPropertyKey> for u32 {
60    fn from(key: BooleanPropertyKey) -> Self {
61        //this unwrap is covered for all variants in unit tests
62        key.to_u32().unwrap()
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use crate::property::{BooleanProperty, BooleanPropertyKey};
69    #[cfg(test)]
70    use strum::IntoEnumIterator;
71
72    #[test]
73    fn new() {
74        let prop = BooleanProperty::new(5, BooleanPropertyKey::HeadsetMicEnabled, false);
75        assert_eq!(
76            prop,
77            BooleanProperty {
78                device_index: 5,
79                key: BooleanPropertyKey::HeadsetMicEnabled,
80                last_value: false
81            }
82        )
83    }
84
85    #[test]
86    fn from_boolean_property_key_for_u32() {
87        for key in BooleanPropertyKey::iter() {
88            u32::from(key); //ensure this never panics
89        }
90    }
91}