ble_ledly/capability/
light.rs

1use crate::communication_protocol::Protocol;
2use crate::device::Device;
3use crate::device::Write;
4use crate::error::{BluetoothError};
5use async_trait::async_trait;
6
7//-------//
8// Light //
9//-------//
10pub enum LightOption {
11    On,
12    Off,
13}
14#[async_trait]
15pub trait Light {
16    async fn set<'e, P: Protocol + std::marker::Send + std::marker::Sync>(
17        device: &Self,
18        protocol: &'e P,
19        option: &'e LightOption,
20    ) -> Result<(), BluetoothError>;
21
22
23    // -------------------------------//
24    // Syntactic sugar /////////////////
25    // more idiomatic syntactic sugar //
26    // -------------------------------//
27    async fn turn_on<'e, P: Protocol + std::marker::Send + std::marker::Sync>(
28        &self,
29        protocol: &'e P,
30    ) -> Result<(), BluetoothError>;
31    async fn turn_off<'e, P: Protocol + std::marker::Send + std::marker::Sync>(
32        &self,
33        protocol: &'e P,
34    ) -> Result<(), BluetoothError>;
35}
36
37//-------------------------//
38// Blanket implementations //
39//-------------------------//
40#[async_trait]
41impl<D: Device + std::marker::Sync> Light for D {
42    // bound type to be transferred across threads
43    async fn set<'e, P: Protocol + std::marker::Send + std::marker::Sync>(
44        device: &Self,
45        protocol: &'e P,
46        option: &'e LightOption,
47    ) -> Result<(), BluetoothError> {
48        device.push(&protocol.light(option)[..]).await?;
49        Ok(())
50    }
51
52
53    // -------------------------------//
54    // Syntactic sugar /////////////////
55    // more idiomatic syntactic sugar //
56    // -------------------------------//
57    async fn turn_on<'e, P: Protocol + std::marker::Send + std::marker::Sync>(
58        &self,
59        protocol: &'e P,
60    ) -> Result<(), BluetoothError>{
61        self.push(&protocol.light(&LightOption::On)[..]).await?;
62        Ok(())
63    }
64    async fn turn_off<'e, P: Protocol + std::marker::Send + std::marker::Sync>(
65        &self,
66        protocol: &'e P,
67    ) -> Result<(), BluetoothError>{
68        self.push(&protocol.light(&LightOption::Off)[..]).await?;
69        Ok(())
70    }
71}