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
use std;

#[derive(Clone,Debug)]
pub struct SystemExclusiveData {
    sys_exclusive: Vec<u8>,
}


impl SystemExclusiveData {
    pub fn newo<T: Into<Vec<u8>>>(systemid: u16, data: T) -> Self {
        let mut data = data.into();
        let syshigh: u8 = (systemid >> 8) as u8;
        let syslow: u8 = (systemid & 0xff) as u8;
        let mut header = vec![syshigh, syslow];
        header.append(&mut data);
        SystemExclusiveData { sys_exclusive: header }
    }

    pub fn get_system_id(&self) -> u16 {
        if self.sys_exclusive.len() < 2 {
            0
        } else {
            ((self.sys_exclusive[1] as u16) << 8) + (self.sys_exclusive[0] as u16)
        }
    }

    pub fn get_data(&self) -> &[u8] {
        if self.sys_exclusive.len() < 2 {
            &self.sys_exclusive[0..0]
        } else {
            &self.sys_exclusive[2..]
        }
    }
}


impl std::convert::From<Vec<u8>> for SystemExclusiveData {
    fn from(t: Vec<u8>) -> SystemExclusiveData {
        SystemExclusiveData { sys_exclusive: t }
    }
}

impl std::convert::From<SystemExclusiveData> for Vec<u8> {
    fn from(t: SystemExclusiveData) -> Vec<u8> {
        t.sys_exclusive
    }
}