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
use crate::{
    channel_voice2::UMP_MESSAGE_TYPE,
    detail::{common_properties, schema},
    ux::{u4, u7},
};

pub(crate) const STATUS: u8 = 0b0011;

/// MIDI 2.0 Channel Voice Assignable Controller Message
///
/// See the [module docs](crate::channel_voice2) for more info.
#[midi2_proc::generate_message(Via(crate::channel_voice2::ChannelVoice2), FixedSize, MinSizeUmp(2))]
struct AssignableController {
    #[property(common_properties::UmpMessageTypeProperty<UMP_MESSAGE_TYPE>)]
    ump_type: (),
    #[property(common_properties::ChannelVoiceStatusProperty<STATUS>)]
    status: (),
    #[property(common_properties::UmpSchemaProperty<u4, schema::Ump<0x000F_0000, 0x0, 0x0, 0x0>>)]
    channel: u4,
    #[property(common_properties::GroupProperty)]
    group: u4,
    #[property(common_properties::UmpSchemaProperty<u7, schema::Ump<0x0000_7F00, 0x0, 0x0, 0x0>>)]
    bank: u7,
    #[property(common_properties::UmpSchemaProperty<u7, schema::Ump<0x0000_007F, 0x0, 0x0, 0x0>>)]
    index: u7,
    #[property(common_properties::UmpSchemaProperty<u32, schema::Ump<0x0000_0000, 0xFFFF_FFFF, 0x0, 0x0>>)]
    controller_data: u32,
}

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;

    #[test]
    fn setters() {
        use crate::traits::{Channeled, Grouped};

        let mut message = AssignableController::<[u32; 4]>::new();
        message.set_group(u4::new(0xC));
        message.set_channel(u4::new(0x8));
        message.set_bank(u7::new(0x51));
        message.set_index(u7::new(0x38));
        message.set_controller_data(0x3F3ADD42);
        assert_eq!(
            message,
            AssignableController([0x4C38_5138, 0x3F3ADD42, 0x0, 0x0]),
        );
    }

    #[test]
    fn group() {
        use crate::traits::Grouped;

        assert_eq!(
            AssignableController::try_from(&[0x4C38_5138, 0x3F3ADD42][..])
                .unwrap()
                .group(),
            u4::new(0xC),
        );
    }

    #[test]
    fn channel() {
        use crate::traits::Channeled;

        assert_eq!(
            AssignableController::try_from(&[0x4C38_5138, 0x3F3ADD42][..])
                .unwrap()
                .channel(),
            u4::new(0x8),
        );
    }

    #[test]
    pub fn bank() {
        assert_eq!(
            AssignableController::try_from(&[0x4C38_5138, 0x3F3ADD42][..])
                .unwrap()
                .bank(),
            u7::new(0x51),
        );
    }

    #[test]
    pub fn index() {
        assert_eq!(
            AssignableController::try_from(&[0x4C38_5138, 0x3F3ADD42][..])
                .unwrap()
                .index(),
            u7::new(0x38),
        );
    }

    #[test]
    pub fn controller_data() {
        assert_eq!(
            AssignableController::try_from(&[0x4C38_5138, 0x3F3ADD42][..])
                .unwrap()
                .controller_data(),
            0x3F3ADD42,
        );
    }

    #[test]
    fn packets() {
        use crate::Packets;

        let buffer = [0x4C38_5138, 0x3F3ADD42];
        let message = AssignableController::try_from(&buffer[..]).unwrap();
        let mut packets = message.packets();
        assert_eq!(packets.next(), Some(&[0x4C38_5138, 0x3F3ADD42][..]));
        assert_eq!(packets.next(), None);
    }
}