mirror of
https://github.com/italicsjenga/usbd-midi.git
synced 2024-12-23 20:31:30 +11:00
Converts to using a std implementation of try_from.
This adds some very simple error types
This commit is contained in:
parent
e4e16bcbcd
commit
93ef2b7576
|
@ -1,16 +1,17 @@
|
||||||
use crate::util::try_from::{TryFrom};
|
use core::convert::TryFrom;
|
||||||
|
|
||||||
/// The Code Index Number(CIN) indicates the classification
|
/// The Code Index Number(CIN) indicates the classification
|
||||||
/// of the bytes in the MIDI_x fields
|
/// of the bytes in the MIDI_x fields
|
||||||
pub struct CodeIndexNumber(u8);
|
pub struct CodeIndexNumber(u8);
|
||||||
|
pub struct InvalidCodeIndexNumber(u8);
|
||||||
|
|
||||||
impl TryFrom<u8> for CodeIndexNumber {
|
impl TryFrom<u8> for CodeIndexNumber {
|
||||||
|
type Error = InvalidCodeIndexNumber;
|
||||||
fn try_from(value:u8) -> Option<Self> {
|
fn try_from(value:u8) -> Result<Self,Self::Error> {
|
||||||
if value > 0xF {
|
if value > 0xF {
|
||||||
None
|
Err(InvalidCodeIndexNumber(value))
|
||||||
} else {
|
} else {
|
||||||
Some(CodeIndexNumber(value))
|
Ok(CodeIndexNumber(value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::util::try_from::{TryFrom};
|
use core::convert::TryFrom;
|
||||||
|
|
||||||
/// The MidiChannel is a value ranging from 0x0 to 0xF
|
/// The MidiChannel is a value ranging from 0x0 to 0xF
|
||||||
/// This is a standard midi concept
|
/// This is a standard midi concept
|
||||||
|
@ -12,27 +12,29 @@ pub enum MidiChannel {
|
||||||
Channel13 = 0xC, Channel14 = 0xD, Channel15 = 0xE, Channel16 = 0xF
|
Channel13 = 0xC, Channel14 = 0xD, Channel15 = 0xE, Channel16 = 0xF
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<u8> for MidiChannel {
|
pub struct InvalidMidiChannel(u8);
|
||||||
|
|
||||||
fn try_from(value:u8) -> Option<Self> {
|
impl TryFrom<u8> for MidiChannel {
|
||||||
|
type Error = InvalidMidiChannel;
|
||||||
|
fn try_from(value:u8) -> Result<Self,Self::Error> {
|
||||||
match value {
|
match value {
|
||||||
x if x == MidiChannel::Channel1 as u8 => Some(MidiChannel::Channel1),
|
x if x == MidiChannel::Channel1 as u8 => Ok(MidiChannel::Channel1),
|
||||||
x if x == MidiChannel::Channel2 as u8 => Some(MidiChannel::Channel2),
|
x if x == MidiChannel::Channel2 as u8 => Ok(MidiChannel::Channel2),
|
||||||
x if x == MidiChannel::Channel3 as u8 => Some(MidiChannel::Channel3),
|
x if x == MidiChannel::Channel3 as u8 => Ok(MidiChannel::Channel3),
|
||||||
x if x == MidiChannel::Channel4 as u8 => Some(MidiChannel::Channel4),
|
x if x == MidiChannel::Channel4 as u8 => Ok(MidiChannel::Channel4),
|
||||||
x if x == MidiChannel::Channel5 as u8 => Some(MidiChannel::Channel5),
|
x if x == MidiChannel::Channel5 as u8 => Ok(MidiChannel::Channel5),
|
||||||
x if x == MidiChannel::Channel6 as u8 => Some(MidiChannel::Channel6),
|
x if x == MidiChannel::Channel6 as u8 => Ok(MidiChannel::Channel6),
|
||||||
x if x == MidiChannel::Channel7 as u8 => Some(MidiChannel::Channel7),
|
x if x == MidiChannel::Channel7 as u8 => Ok(MidiChannel::Channel7),
|
||||||
x if x == MidiChannel::Channel8 as u8 => Some(MidiChannel::Channel8),
|
x if x == MidiChannel::Channel8 as u8 => Ok(MidiChannel::Channel8),
|
||||||
x if x == MidiChannel::Channel9 as u8 => Some(MidiChannel::Channel9),
|
x if x == MidiChannel::Channel9 as u8 => Ok(MidiChannel::Channel9),
|
||||||
x if x == MidiChannel::Channel10 as u8 => Some(MidiChannel::Channel10),
|
x if x == MidiChannel::Channel10 as u8 => Ok(MidiChannel::Channel10),
|
||||||
x if x == MidiChannel::Channel11 as u8 => Some(MidiChannel::Channel11),
|
x if x == MidiChannel::Channel11 as u8 => Ok(MidiChannel::Channel11),
|
||||||
x if x == MidiChannel::Channel12 as u8 => Some(MidiChannel::Channel12),
|
x if x == MidiChannel::Channel12 as u8 => Ok(MidiChannel::Channel12),
|
||||||
x if x == MidiChannel::Channel13 as u8 => Some(MidiChannel::Channel13),
|
x if x == MidiChannel::Channel13 as u8 => Ok(MidiChannel::Channel13),
|
||||||
x if x == MidiChannel::Channel14 as u8 => Some(MidiChannel::Channel14),
|
x if x == MidiChannel::Channel14 as u8 => Ok(MidiChannel::Channel14),
|
||||||
x if x == MidiChannel::Channel15 as u8 => Some(MidiChannel::Channel15),
|
x if x == MidiChannel::Channel15 as u8 => Ok(MidiChannel::Channel15),
|
||||||
x if x == MidiChannel::Channel16 as u8 => Some(MidiChannel::Channel16),
|
x if x == MidiChannel::Channel16 as u8 => Ok(MidiChannel::Channel16),
|
||||||
_ => None
|
_ => Err(InvalidMidiChannel(value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,30 +1,27 @@
|
||||||
use crate::util::try_from::TryFrom;
|
use core::convert::TryFrom;
|
||||||
|
|
||||||
pub struct MidiVelocity(u8);
|
pub struct MidiVelocity(u8);
|
||||||
|
|
||||||
impl From<u8> for MidiVelocity{
|
pub struct InvalidMidiVelocity(u8);
|
||||||
fn from(value:u8) -> MidiVelocity{
|
|
||||||
let masked = value & 0x7F;
|
|
||||||
MidiVelocity(masked)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Into<u8> for MidiVelocity{
|
|
||||||
fn into(self) -> u8 {
|
|
||||||
self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<u8> for MidiVelocity{
|
impl TryFrom<u8> for MidiVelocity{
|
||||||
fn try_from(value:u8) -> Option<MidiVelocity> {
|
type Error = InvalidMidiVelocity;
|
||||||
|
|
||||||
|
fn try_from(value:u8) -> Result<Self,Self::Error> {
|
||||||
if value > 0x7F {
|
if value > 0x7F {
|
||||||
None
|
Err(InvalidMidiVelocity(value))
|
||||||
} else {
|
} else {
|
||||||
Some(MidiVelocity(value))
|
Ok(MidiVelocity(value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Into<u8> for MidiVelocity {
|
||||||
|
fn into(self) -> u8 {
|
||||||
|
self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl MidiVelocity {
|
impl MidiVelocity {
|
||||||
pub const MAX: MidiVelocity= MidiVelocity(0x7F);
|
pub const MAX: MidiVelocity= MidiVelocity(0x7F);
|
||||||
pub const MIN: MidiVelocity = MidiVelocity(0);
|
pub const MIN: MidiVelocity = MidiVelocity(0);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::util::try_from::{TryFrom};
|
use core::convert::TryFrom;
|
||||||
|
|
||||||
/// The Cable Number (CN) is a value ranging from 0x0 to 0xF
|
/// The Cable Number (CN) is a value ranging from 0x0 to 0xF
|
||||||
/// indicating the number assignment of the Embedded MIDI Jack associated
|
/// indicating the number assignment of the Embedded MIDI Jack associated
|
||||||
|
@ -11,28 +11,29 @@ pub enum CableNumber {
|
||||||
Cable8 = 0x8, Cable9 = 0x9, Cable10 = 0xA, Cable11 = 0xB,
|
Cable8 = 0x8, Cable9 = 0x9, Cable10 = 0xA, Cable11 = 0xB,
|
||||||
Cable12 = 0xC, Cable13 = 0xD, Cable14 = 0xE, Cable15 = 0xF
|
Cable12 = 0xC, Cable13 = 0xD, Cable14 = 0xE, Cable15 = 0xF
|
||||||
}
|
}
|
||||||
|
pub struct InvalidCableNumber(u8);
|
||||||
|
|
||||||
impl TryFrom<u8> for CableNumber {
|
impl TryFrom<u8> for CableNumber {
|
||||||
|
type Error = InvalidCableNumber;
|
||||||
fn try_from(value:u8) -> Option<Self> {
|
fn try_from(value:u8) -> Result<Self,Self::Error> {
|
||||||
match value {
|
match value {
|
||||||
x if x == CableNumber::Cable0 as u8 => Some(CableNumber::Cable0),
|
x if x == CableNumber::Cable0 as u8 => Ok(CableNumber::Cable0),
|
||||||
x if x == CableNumber::Cable1 as u8 => Some(CableNumber::Cable1),
|
x if x == CableNumber::Cable1 as u8 => Ok(CableNumber::Cable1),
|
||||||
x if x == CableNumber::Cable2 as u8 => Some(CableNumber::Cable2),
|
x if x == CableNumber::Cable2 as u8 => Ok(CableNumber::Cable2),
|
||||||
x if x == CableNumber::Cable3 as u8 => Some(CableNumber::Cable3),
|
x if x == CableNumber::Cable3 as u8 => Ok(CableNumber::Cable3),
|
||||||
x if x == CableNumber::Cable4 as u8 => Some(CableNumber::Cable4),
|
x if x == CableNumber::Cable4 as u8 => Ok(CableNumber::Cable4),
|
||||||
x if x == CableNumber::Cable5 as u8 => Some(CableNumber::Cable5),
|
x if x == CableNumber::Cable5 as u8 => Ok(CableNumber::Cable5),
|
||||||
x if x == CableNumber::Cable6 as u8 => Some(CableNumber::Cable6),
|
x if x == CableNumber::Cable6 as u8 => Ok(CableNumber::Cable6),
|
||||||
x if x == CableNumber::Cable7 as u8 => Some(CableNumber::Cable7),
|
x if x == CableNumber::Cable7 as u8 => Ok(CableNumber::Cable7),
|
||||||
x if x == CableNumber::Cable8 as u8 => Some(CableNumber::Cable8),
|
x if x == CableNumber::Cable8 as u8 => Ok(CableNumber::Cable8),
|
||||||
x if x == CableNumber::Cable9 as u8 => Some(CableNumber::Cable9),
|
x if x == CableNumber::Cable9 as u8 => Ok(CableNumber::Cable9),
|
||||||
x if x == CableNumber::Cable10 as u8 => Some(CableNumber::Cable10),
|
x if x == CableNumber::Cable10 as u8 => Ok(CableNumber::Cable10),
|
||||||
x if x == CableNumber::Cable11 as u8 => Some(CableNumber::Cable11),
|
x if x == CableNumber::Cable11 as u8 => Ok(CableNumber::Cable11),
|
||||||
x if x == CableNumber::Cable12 as u8 => Some(CableNumber::Cable12),
|
x if x == CableNumber::Cable12 as u8 => Ok(CableNumber::Cable12),
|
||||||
x if x == CableNumber::Cable13 as u8 => Some(CableNumber::Cable13),
|
x if x == CableNumber::Cable13 as u8 => Ok(CableNumber::Cable13),
|
||||||
x if x == CableNumber::Cable14 as u8 => Some(CableNumber::Cable14),
|
x if x == CableNumber::Cable14 as u8 => Ok(CableNumber::Cable14),
|
||||||
x if x == CableNumber::Cable15 as u8 => Some(CableNumber::Cable15),
|
x if x == CableNumber::Cable15 as u8 => Ok(CableNumber::Cable15),
|
||||||
_ => None
|
_ => Err(InvalidCableNumber(value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,2 +1 @@
|
||||||
pub mod try_from;
|
|
||||||
pub mod nibble;
|
pub mod nibble;
|
|
@ -1,6 +0,0 @@
|
||||||
|
|
||||||
/// A version of try from that works on options
|
|
||||||
/// useful for the bounded ints
|
|
||||||
pub trait TryFrom<T> : Sized{
|
|
||||||
fn try_from(value:T) -> Option<Self>;
|
|
||||||
}
|
|
Loading…
Reference in a new issue