| 1 |
use std::error::Error; |
| 2 |
use std::fmt; |
| 3 |
|
| 4 |
const INVALID_PORT_MSG: &str = "invalid port"; |
| 5 |
const PORT_OUT_OF_RANGE_MSG: &str = "provided port number was out of range"; |
| 6 |
const CANNOT_RETRIEVE_PORT_NAME_MSG: &str = "unknown error when trying to retrieve the port name"; |
| 7 |
|
| 8 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 9 |
/// An error that can occur during initialization (i.e., while |
| 10 |
/// creating a `MidiInput` or `MidiOutput` object). |
| 11 |
pub struct InitError; |
| 12 |
|
| 13 |
impl Error for InitError {} |
| 14 |
|
| 15 |
impl fmt::Display for InitError { |
| 16 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 17 |
"MIDI support could not be initialized".fmt(f) |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 22 |
/// An error that can occur when retrieving information about |
| 23 |
/// available ports. |
| 24 |
pub enum PortInfoError { |
| 25 |
PortNumberOutOfRange, // TODO: don't expose this |
| 26 |
InvalidPort, |
| 27 |
CannotRetrievePortName, |
| 28 |
} |
| 29 |
|
| 30 |
impl Error for PortInfoError {} |
| 31 |
|
| 32 |
impl fmt::Display for PortInfoError { |
| 33 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 34 |
match *self { |
| 35 |
PortInfoError::PortNumberOutOfRange => PORT_OUT_OF_RANGE_MSG.fmt(f), |
| 36 |
PortInfoError::InvalidPort => INVALID_PORT_MSG.fmt(f), |
| 37 |
PortInfoError::CannotRetrievePortName => CANNOT_RETRIEVE_PORT_NAME_MSG.fmt(f), |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 43 |
/// The kind of error for a `ConnectError`. |
| 44 |
pub enum ConnectErrorKind { |
| 45 |
InvalidPort, |
| 46 |
Other(&'static str) |
| 47 |
} |
| 48 |
|
| 49 |
impl ConnectErrorKind {} |
| 50 |
|
| 51 |
impl fmt::Display for ConnectErrorKind { |
| 52 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 53 |
match *self { |
| 54 |
ConnectErrorKind::InvalidPort => INVALID_PORT_MSG.fmt(f), |
| 55 |
ConnectErrorKind::Other(msg) => msg.fmt(f) |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/// An error that can occur when trying to connect to a port. |
| 61 |
pub struct ConnectError<T> { |
| 62 |
kind: ConnectErrorKind, |
| 63 |
inner: T |
| 64 |
} |
| 65 |
|
| 66 |
impl<T> ConnectError<T> { |
| 67 |
pub fn new(kind: ConnectErrorKind, inner: T) -> ConnectError<T> { |
| 68 |
ConnectError { kind: kind, inner: inner } |
| 69 |
} |
| 70 |
|
| 71 |
/// Helper method to create ConnectErrorKind::Other. |
| 72 |
pub fn other(msg: &'static str, inner: T) -> ConnectError<T> { |
| 73 |
Self::new(ConnectErrorKind::Other(msg), inner) |
| 74 |
} |
| 75 |
|
| 76 |
pub fn kind(&self) -> ConnectErrorKind { |
| 77 |
self.kind |
| 78 |
} |
| 79 |
|
| 80 |
pub fn into_inner(self) -> T { |
| 81 |
self.inner |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
impl<T> fmt::Debug for ConnectError<T> { |
| 86 |
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
| 87 |
self.kind.fmt(f) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
impl<T> fmt::Display for ConnectError<T> { |
| 92 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 93 |
self.kind.fmt(f) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
impl<T> Error for ConnectError<T> {} |
| 98 |
|
| 99 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 100 |
/// An error that can occur when sending MIDI messages. |
| 101 |
pub enum SendError { |
| 102 |
InvalidData(&'static str), |
| 103 |
Other(&'static str) |
| 104 |
} |
| 105 |
|
| 106 |
impl Error for SendError {} |
| 107 |
|
| 108 |
impl fmt::Display for SendError { |
| 109 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 110 |
match *self { |
| 111 |
SendError::InvalidData(msg) | SendError::Other(msg) => msg.fmt(f) |
| 112 |
} |
| 113 |
} |
| 114 |
} |