midir/
errors.rs

1use std::error::Error;
2use std::fmt;
3
4const INVALID_PORT_MSG: &str = "invalid port";
5const PORT_OUT_OF_RANGE_MSG: &str = "provided port number was out of range";
6const 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).
11pub struct InitError;
12
13impl Error for InitError {}
14
15impl 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.
24pub enum PortInfoError {
25    PortNumberOutOfRange, // TODO: don't expose this
26    InvalidPort,
27    CannotRetrievePortName,
28}
29
30impl Error for PortInfoError {}
31
32impl 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`.
44pub enum ConnectErrorKind {
45    InvalidPort,
46    Other(&'static str),
47}
48
49impl ConnectErrorKind {}
50
51impl 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.
61pub struct ConnectError<T> {
62    kind: ConnectErrorKind,
63    inner: T,
64}
65
66impl<T> ConnectError<T> {
67    pub fn new(kind: ConnectErrorKind, inner: T) -> ConnectError<T> {
68        ConnectError { kind, 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
85impl<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
91impl<T> fmt::Display for ConnectError<T> {
92    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93        self.kind.fmt(f)
94    }
95}
96
97impl<T> Error for ConnectError<T> {}
98
99#[derive(Debug, Clone, Copy, PartialEq, Eq)]
100/// An error that can occur when sending MIDI messages.
101pub enum SendError {
102    InvalidData(&'static str),
103    Other(&'static str),
104}
105
106impl Error for SendError {}
107
108impl 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}