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
// Copyright 2021 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.

use crate::trigger::Dro;
use crate::{error, warn};
use crate::{CapabilityEvent, CapabilityRun, TriggerEvent};

/// Converts the passed `TriggerEvent` into a `CapabilityRun::PixelAnimationIndex`
///
/// # Arguments
///
/// * `event`: The TriggerEvent to convert.  This should always be `TriggerEvent::Animation`,
/// if it is anything else a CapabilityRun::NoOp will be returned
///
/// returns: CapabilityRun::PixelAnimationIndex
pub(super) fn convert(event: TriggerEvent) -> CapabilityRun {
    if let TriggerEvent::Animation { state, index, .. } = event {
        match state {
            Dro::Off => CapabilityRun::PixelAnimationIndex {
                state: CapabilityEvent::None,
                index,
            },
            Dro::Done => CapabilityRun::PixelAnimationIndex {
                state: CapabilityEvent::Last,
                index,
            },
            Dro::Repeat => CapabilityRun::PixelAnimationIndex {
                state: CapabilityEvent::Initial,
                index,
            },
            _ => {
                warn!("Unexpected state {:?}", state);
                CapabilityRun::NoOp {
                    state: CapabilityEvent::None,
                }
            }
        }
    } else {
        error!("Unexpected event {:?}", event);
        CapabilityRun::NoOp {
            state: CapabilityEvent::None,
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::converters::animation;
    use crate::trigger::{Aodo, Dro};
    use crate::{CapabilityEvent, CapabilityRun, TriggerEvent};
    use kll_hid::Keyboard;

    #[test]
    fn animation_off_to_capability_run_none() {
        let a = TriggerEvent::Animation {
            state: Dro::Off,
            index: Keyboard::A.into(),
            last_state: 0,
        };
        let result = animation::convert(a);

        check_results(result, CapabilityEvent::None, Keyboard::A);
    }

    #[test]
    fn animation_done_to_capability_run_last() {
        let a = TriggerEvent::Animation {
            state: Dro::Done,
            index: Keyboard::B.into(),
            last_state: 0,
        };
        let result = animation::convert(a);

        check_results(result, CapabilityEvent::Last, Keyboard::B);
    }

    #[test]
    fn animation_repeat_to_capability_run_initial() {
        let a = TriggerEvent::Animation {
            state: Dro::Repeat,
            index: Keyboard::C.into(),
            last_state: 0,
        };
        let result = animation::convert(a);
        check_results(result, CapabilityEvent::Initial, Keyboard::C);
    }

    #[test]
    fn convert_unexpected_state_type_returns_noop() {
        let a = TriggerEvent::Animation {
            state: Dro::Passthrough,
            index: Keyboard::A.into(),
            last_state: 0,
        };
        let result = animation::convert(a);

        assert_eq!(
            result,
            CapabilityRun::NoOp {
                state: CapabilityEvent::None
            }
        )
    }

    #[test]
    fn convert_unexpected_trigger_event_type_returns_noop() {
        let a = TriggerEvent::Sleep {
            state: Aodo::Activate,
            last_state: 0,
        };
        let result = animation::convert(a);

        assert_eq!(
            result,
            CapabilityRun::NoOp {
                state: CapabilityEvent::None
            }
        )
    }

    fn check_results(
        event: CapabilityRun,
        expected_event: CapabilityEvent,
        expected_index: Keyboard,
    ) {
        if let CapabilityRun::PixelAnimationIndex { state, index } = event {
            assert_eq!(state, expected_event);
            assert_eq!(index, expected_index.into());
        } else {
            panic!("failed to return a LayerState")
        }
    }
}