midi_msg/
system_real_time.rs1use super::parse_error::*;
2use alloc::vec::Vec;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum SystemRealTimeMsg {
8 TimingClock,
10 Start,
12 Continue,
14 Stop,
16 ActiveSensing,
19 SystemReset,
25}
26
27impl SystemRealTimeMsg {
28 pub(crate) fn extend_midi(&self, v: &mut Vec<u8>) {
29 match self {
30 Self::TimingClock => v.push(0xF8),
31 Self::Start => v.push(0xFA),
32 Self::Continue => v.push(0xFB),
33 Self::Stop => v.push(0xFC),
34 Self::ActiveSensing => v.push(0xFE),
35 Self::SystemReset => v.push(0xFF),
36 }
37 }
38
39 pub(crate) fn from_midi(m: &[u8]) -> Result<(Self, usize), ParseError> {
40 match m.first() {
41 Some(0xF8) => Ok((Self::TimingClock, 1)),
42 Some(0xFA) => Ok((Self::Start, 1)),
43 Some(0xFB) => Ok((Self::Continue, 1)),
44 Some(0xFC) => Ok((Self::Stop, 1)),
45 Some(0xFE) => Ok((Self::ActiveSensing, 1)),
46 Some(0xFF) => Ok((Self::SystemReset, 1)),
47 Some(x) => Err(ParseError::UndefinedSystemRealTimeMessage(*x)),
48 None => panic!("Should not be reachable"),
49 }
50 }
51}
52
53#[cfg(test)]
54mod tests {
55 use crate::*;
56 extern crate std;
57 use std::vec;
58
59 #[test]
60 fn serialize_system_real_time_msg() {
61 assert_eq!(
62 MidiMsg::SystemRealTime {
63 msg: SystemRealTimeMsg::TimingClock
64 }
65 .to_midi(),
66 vec![0xF8]
67 );
68 }
69
70 #[test]
71 fn deserialize_system_real_time_msg() {
72 let mut ctx = ReceiverContext::new();
73
74 test_serialization(
75 MidiMsg::SystemRealTime {
76 msg: SystemRealTimeMsg::TimingClock,
77 },
78 &mut ctx,
79 );
80 }
81
82 #[test]
83 fn serde_system_reset() {
84 let system_reset = MidiMsg::SystemRealTime {
85 msg: SystemRealTimeMsg::SystemReset,
86 };
87 assert_eq!(
88 MidiMsg::from_midi_with_context(&system_reset.to_midi(), &mut ReceiverContext::new()),
89 Ok((system_reset, 1)),
90 );
91 }
92}