use std::{
borrow::Cow,
fmt::{self, Debug},
io::{self, ErrorKind, Read, Write},
};
#[doc = r#"
There are only three types of midi message bytes:
```text
1. |--------|
| Status |
|--------|
2. |--------| |------|
| Status | - | Data |
|--------| |------|
3. |--------| |------| |------|
| Status | - | Data | - | Data |
|--------| |------| |------|
```
"#]
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum MidiMessageBytes {
Status(StatusByte),
Single(StatusByte, DataByte),
Double(StatusByte, DataByte, DataByte),
}
impl Read for MidiMessageBytes {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
use MidiMessageBytes::*;
match self {
Status(s) => {
let Some(byte) = buf.get_mut(0) else {
return Ok(0);
};
*byte = s.0;
Ok(1)
}
Single(s, d) => {
let Some(byte) = buf.get_mut(0) else {
return Ok(0);
};
*byte = s.0;
let Some(byte) = buf.get_mut(1) else {
return Ok(1);
};
*byte = d.0;
Ok(2)
}
Double(s, d1, d2) => {
let Some(byte) = buf.get_mut(0) else {
return Ok(0);
};
*byte = s.0;
let Some(byte) = buf.get_mut(1) else {
return Ok(1);
};
*byte = d1.0;
let Some(byte) = buf.get_mut(1) else {
return Ok(2);
};
*byte = d2.0;
Ok(3)
}
}
}
}
impl MidiMessageBytes {
pub fn write_to_writer<W: Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> {
use MidiMessageBytes::*;
match self {
Status(s) => {
writer.write_all(&[s.0])?;
Ok(1)
}
Single(s, d) => {
writer.write_all(&[s.0, d.0])?;
Ok(2)
}
Double(s, d1, d2) => {
writer.write_all(&[s.0, d1.0, d2.0])?;
Ok(3)
}
}
}
pub fn from_status<B, E>(status: B) -> Result<Self, io::Error>
where
B: TryInto<StatusByte, Error = E>,
E: Into<io::Error>,
{
let status = status.try_into().map_err(Into::into)?;
Ok(Self::Status(status))
}
}
#[doc = r#"
Status Byte is between [0x80 and 0xFF]
Status bytes are eight-bit binary numbers in which the Most Significant Bit (MSB) is set (binary 1).
Status bytes serve to identify the message type, that is, the purpose of the Data bytes which follow it.
Except for Real-Time messages, new Status bytes will always command a receiver to adopt a new status,
even if the last message was not completed.
"#]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct StatusByte(u8);
impl Debug for StatusByte {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "StatusByte(0x{:0X})", self.0)
}
}
impl StatusByte {
pub fn new(byte: u8) -> Result<Self, io::Error> {
byte.try_into()
}
pub(crate) fn new_unchecked(byte: u8) -> Self {
Self(byte)
}
pub fn byte(&self) -> u8 {
self.0
}
}
impl TryFrom<u8> for StatusByte {
type Error = io::Error;
fn try_from(byte: u8) -> Result<Self, Self::Error> {
(0x80..=0xFF)
.contains(&byte)
.then_some(Self(byte))
.ok_or(io::Error::new(
ErrorKind::InvalidData,
"Expected Status byte",
))
}
}
impl<'a> TryFrom<Cow<'a, u8>> for StatusByte {
type Error = io::Error;
fn try_from(byte: Cow<'a, u8>) -> Result<Self, Self::Error> {
(0x80..=0xFF)
.contains(byte.as_ref())
.then_some(Self(*byte))
.ok_or(io::Error::new(
ErrorKind::InvalidData,
"Expected Status byte",
))
}
}
impl<'a> TryFrom<&'a u8> for StatusByte {
type Error = io::Error;
fn try_from(byte: &'a u8) -> Result<Self, Self::Error> {
(0x80..=0xFF)
.contains(byte)
.then_some(Self(*byte))
.ok_or(io::Error::new(
ErrorKind::InvalidData,
"Expected Status byte",
))
}
}
impl fmt::Display for StatusByte {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:02X}", self.0)
}
}
#[doc = r#"
Data Byte is between [0x00 and 0x7F]
"#]
#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct DataByte(pub(crate) u8);
impl Debug for DataByte {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Debug(0x{:0X}", self.0)
}
}
impl DataByte {
pub fn new(byte: u8) -> Result<Self, io::Error> {
byte.try_into()
}
pub const fn new_unchecked(byte: u8) -> Self {
Self(byte)
}
pub fn value(&self) -> u8 {
self.0
}
}
impl TryFrom<u8> for DataByte {
type Error = io::Error;
fn try_from(byte: u8) -> Result<Self, Self::Error> {
(0x00..=0x7F)
.contains(&byte)
.then_some(Self(byte))
.ok_or(io::Error::new(
ErrorKind::InvalidData,
"Expected Status byte",
))
}
}
impl<'a> TryFrom<&'a u8> for DataByte {
type Error = io::Error;
fn try_from(byte: &'a u8) -> Result<Self, Self::Error> {
(0x00..=0x7F)
.contains(byte)
.then_some(Self(*byte))
.ok_or(io::Error::new(
ErrorKind::InvalidData,
"Expected Status byte",
))
}
}
impl<'a> TryFrom<Cow<'a, u8>> for DataByte {
type Error = io::Error;
fn try_from(byte: Cow<'a, u8>) -> Result<Self, Self::Error> {
(0x00..=0x7F)
.contains(byte.as_ref())
.then_some(Self(*byte))
.ok_or(io::Error::new(
ErrorKind::InvalidData,
"Expected Status byte",
))
}
}
impl fmt::Display for DataByte {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:02X}", self.0)
}
}