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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2021-2022 Jacob Alexander
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

mod animation;
mod layer;
mod led;
mod rotation;
mod switch;

mod convert {
    use crate::converters::{animation, layer, led, rotation, switch};
    use crate::{Capability, CapabilityEvent, CapabilityRun, TriggerCondition, TriggerEvent};

    impl From<TriggerEvent> for CapabilityRun {
        fn from(event: TriggerEvent) -> Self {
            match event {
                TriggerEvent::Switch { .. } => switch::convert(event),
                TriggerEvent::Layer { .. } => layer::convert(event),
                TriggerEvent::HidLed { .. } => led::convert(event),
                TriggerEvent::Animation { .. } => animation::convert(event),
                TriggerEvent::Rotation { .. } => rotation::convert(event),
                TriggerEvent::None => CapabilityRun::NoOp {
                    state: CapabilityEvent::None,
                },
                // TriggerEvent::AnalogDistance=>,
                // TriggerEvent::AnalogVelocity=>,
                // TriggerEvent::AnalogAcceleration=>,
                // TriggerEvent::AnalogJerk=>,
                // TriggerEvent::Sleep =>,
                // TriggerEvent::Resume=>,
                // TriggerEvent::Inactive=>,
                // TriggerEvent::Active=>,
                other => {
                    panic!("*** remove once all events are handled ***\n TriggerEvent {:?} not recognised", other)
                }
            }
        }
    }

    /// Convert TriggerEvent into the u8 identifier
    impl From<TriggerEvent> for u8 {
        fn from(event: TriggerEvent) -> Self {
            match event {
                TriggerEvent::None => 0,
                TriggerEvent::Switch { .. } => 1,
                TriggerEvent::HidLed { .. } => 2,
                TriggerEvent::AnalogDistance { .. } => 3,
                TriggerEvent::AnalogVelocity { .. } => 4,
                TriggerEvent::AnalogAcceleration { .. } => 5,
                TriggerEvent::AnalogJerk { .. } => 6,
                TriggerEvent::Layer { .. } => 7,
                TriggerEvent::Animation { .. } => 8,
                TriggerEvent::Sleep { .. } => 9,
                TriggerEvent::Resume { .. } => 10,
                TriggerEvent::Inactive { .. } => 11,
                TriggerEvent::Active { .. } => 12,
                TriggerEvent::Rotation { .. } => 13,
            }
        }
    }

    /// Convert TriggerCondition into the u8 identifier
    impl From<TriggerCondition> for u8 {
        fn from(cond: TriggerCondition) -> Self {
            match cond {
                TriggerCondition::None => 0,
                TriggerCondition::Switch { .. } => 1,
                TriggerCondition::HidLed { .. } => 2,
                TriggerCondition::AnalogDistance { .. } => 3,
                TriggerCondition::AnalogVelocity { .. } => 4,
                TriggerCondition::AnalogAcceleration { .. } => 5,
                TriggerCondition::AnalogJerk { .. } => 6,
                TriggerCondition::Layer { .. } => 7,
                TriggerCondition::Animation { .. } => 8,
                TriggerCondition::Sleep { .. } => 9,
                TriggerCondition::Resume { .. } => 10,
                TriggerCondition::Inactive { .. } => 11,
                TriggerCondition::Active { .. } => 12,
                TriggerCondition::Rotation { .. } => 13,
            }
        }
    }

    /// Convert TriggerCondition to TriggerEvent
    /// Used for mainly for kll validation, events are time index 0
    /// and are assumed to be immediate with no history
    impl From<TriggerCondition> for TriggerEvent {
        fn from(cond: TriggerCondition) -> Self {
            match cond {
                TriggerCondition::None => TriggerEvent::None,
                TriggerCondition::Switch { state, index, .. } => TriggerEvent::Switch {
                    state,
                    index,
                    last_state: 0,
                },
                _ => {
                    panic!(
                        "TriggerCondition to TriggerEvent not implemented! {:?}",
                        cond
                    );
                }
            }
        }
    }

    /// Convert Capability to CapabilityRun
    /// Used for mainly for kll validation
    impl From<Capability> for CapabilityRun {
        fn from(cap: Capability) -> Self {
            let tevent = TriggerEvent::None;
            match cap {
                Capability::NoOp { state, .. } => CapabilityRun::NoOp {
                    state: state.event(tevent),
                },
                Capability::HidKeyboard { state, id, .. } => CapabilityRun::HidKeyboard {
                    state: state.event(tevent),
                    id,
                },
                _ => {
                    panic!("Capability to CapabilityRun not implemented! {:?}", cap);
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{CapabilityEvent, CapabilityRun, TriggerEvent};

    #[test]
    fn non_event_converted_to_noop_run() {
        let expected = CapabilityRun::NoOp {
            state: CapabilityEvent::None,
        };
        let result: CapabilityRun = TriggerEvent::None.into();

        assert_eq!(result, expected);
    }
}