1#[derive(Debug, Eq, PartialEq, Clone)]
2pub struct Clip(Vec<u32>);
3
4#[derive(Debug, Eq, PartialEq, Clone)]
5pub enum Error {
6 IncorrectClipHeader,
7}
8
9impl std::fmt::Display for Error {
10 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
11 match self {
12 Self::IncorrectClipHeader => write!(f, "Incorrect MIDI 2.0 clip file header"),
13 }
14 }
15}
16
17impl std::error::Error for Error {}
18
19impl Clip {
20 pub fn read_clip_file<Read: std::io::Read>(stream: &mut Read) -> Result<Self, Error> {
21 let mut header_buffer = [0x0_u8; 8];
22 if stream.read_exact(&mut header_buffer).is_err()
23 || header_buffer != [0x53, 0x4D, 0x46, 0x32, 0x43, 0x4C, 0x49, 0x50]
24 {
25 return Err(Error::IncorrectClipHeader);
26 }
27 todo!()
28 }
29
30 pub fn write_clip_file<Write: std::io::Write>(&self, _output: &mut Write) -> Self {
31 todo!()
32 }
33
34 #[cfg(feature = "smf")]
35 pub fn read_smf<Read: std::io::Read>(_stream: &mut Read) -> Result<Self, Error> {
36 todo!()
37 }
38
39 #[cfg(feature = "smf")]
40 pub fn write_smf<Write: std::io::Write>(&self, _output: &mut Write) -> Self {
41 todo!()
42 }
43}
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48
49 #[test]
50 fn read_clip_checks_for_missing_header() {
51 let data: &[u8] = &[];
52 let mut read = std::io::Cursor::new(data);
53 assert_eq!(
54 Clip::read_clip_file(&mut read),
55 Err(Error::IncorrectClipHeader),
56 );
57 }
58
59 #[test]
60 fn read_clip_checks_for_incorrect_header() {
61 let data: &[u8] = &[0x53, 0x4D, 0x46, 0x32, 0x43, 0x4C, 0x49, 0x0];
62 let mut read = std::io::Cursor::new(data);
63 assert_eq!(
64 Clip::read_clip_file(&mut read),
65 Err(Error::IncorrectClipHeader),
66 );
67 }
68
69 #[test]
70 fn read_clip_checks_for_correct_header() {
71 let data: &[u8] = &[0x53, 0x4D, 0x46, 0x32, 0x43, 0x4C, 0x49, 0x50];
72 let mut read = std::io::Cursor::new(data);
73 assert_eq!(
74 Clip::read_clip_file(&mut read),
75 Ok(Clip(Vec::new()))
76 );
77 }
78}