1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use crate::communication_protocol::Protocol;
use crate::device::Device;
use crate::device::Write;
use crate::error::{BluetoothError};
use async_trait::async_trait;

//---------//
// animate //
//---------//
pub enum HWAnimateOption<'e> {
    Pulsating(&'e HWStaticColorOption, &'e HWAnimationSpeedSetting),
}

pub enum HWStaticColorOption {
    Red,
    Green,
    Blue,
}
// TODO: more meaningf&ul name
pub enum HWAnimationSpeedSetting {
    Speed1,
    Speed2,
    Speed3,
    Speed4,
    Speed5,
    Speed6,
    Speed7,
    Speed8,
    Speed9,
}

#[async_trait]
pub trait HWAnimate {
    async fn set<'e, P: Protocol + std::marker::Send + std::marker::Sync>(
        device: &Self,
        protocol: &'e P,
        option: &'e HWAnimateOption,
    ) -> Result<(), BluetoothError>;

    // -------------------------------//
    // Syntactic sugar /////////////////
    // more idiomatic syntactic sugar //
    // -------------------------------//
    async fn hw_anim_pulsating<'e, P: Protocol + std::marker::Send + std::marker::Sync>(
        &self,
        protocol: &'e P,
        color: &'e HWStaticColorOption,
        speed: &'e HWAnimationSpeedSetting
    ) -> Result<(), BluetoothError>;
}

//-------------------------//
// Blanket implementations //
//-------------------------//
#[async_trait]
impl<D: Device + std::marker::Sync> HWAnimate for D {
    // bound type to be transferred across threads
    async fn set<'e, P: Protocol + std::marker::Send + std::marker::Sync>(
        device: &Self,
        protocol: &'e P,
        option: &'e HWAnimateOption,
    ) -> Result<(), BluetoothError> {
        device.push(&protocol.hw_animate(option)[..]).await?;
        Ok(())
    }

    // -------------------------------//
    // Syntactic sugar /////////////////
    // more idiomatic syntactic sugar //
    // -------------------------------//
    async fn hw_anim_pulsating<'e, P: Protocol + std::marker::Send + std::marker::Sync>(
        &self,
        protocol: &'e P,
        color: &'e HWStaticColorOption,
        speed: &'e HWAnimationSpeedSetting
    ) -> Result<(), BluetoothError> {
        self.push(&protocol.hw_animate(&HWAnimateOption::Pulsating(color, speed))[..]).await?;
        Ok(())
    }
}