summaryrefslogtreecommitdiffstats
path: root/third_party/rust/midir/src/errors.rs
blob: 69331528c925f6497a918e7d09d4e5ace8610944 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use std::error::Error;
use std::fmt;

const INVALID_PORT_MSG: &str = "invalid port";
const PORT_OUT_OF_RANGE_MSG: &str = "provided port number was out of range";
const CANNOT_RETRIEVE_PORT_NAME_MSG: &str = "unknown error when trying to retrieve the port name";

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// An error that can occur during initialization (i.e., while
/// creating a `MidiInput` or `MidiOutput` object).
pub struct InitError;

impl Error for InitError {}

impl fmt::Display for InitError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        "MIDI support could not be initialized".fmt(f)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// An error that can occur when retrieving information about
/// available ports.
pub enum PortInfoError {
    PortNumberOutOfRange, // TODO: don't expose this
    InvalidPort,
    CannotRetrievePortName,
}

impl Error for PortInfoError {}

impl fmt::Display for PortInfoError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            PortInfoError::PortNumberOutOfRange => PORT_OUT_OF_RANGE_MSG.fmt(f),
            PortInfoError::InvalidPort => INVALID_PORT_MSG.fmt(f),
            PortInfoError::CannotRetrievePortName => CANNOT_RETRIEVE_PORT_NAME_MSG.fmt(f),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// The kind of error for a `ConnectError`.
pub enum ConnectErrorKind {
    InvalidPort,
    Other(&'static str)
}

impl ConnectErrorKind {}

impl fmt::Display for ConnectErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ConnectErrorKind::InvalidPort => INVALID_PORT_MSG.fmt(f),
            ConnectErrorKind::Other(msg) => msg.fmt(f)
        }
    }
}

/// An error that can occur when trying to connect to a port.
pub struct ConnectError<T> {
    kind: ConnectErrorKind,
    inner: T
}

impl<T> ConnectError<T> {
    pub fn new(kind: ConnectErrorKind, inner: T) -> ConnectError<T> {
        ConnectError { kind: kind, inner: inner }
    }
    
    /// Helper method to create ConnectErrorKind::Other.
    pub fn other(msg: &'static str, inner: T) -> ConnectError<T> {
        Self::new(ConnectErrorKind::Other(msg), inner)
    }
    
    pub fn kind(&self) -> ConnectErrorKind {
        self.kind
    }
    
    pub fn into_inner(self) -> T {
        self.inner
    }
}

impl<T> fmt::Debug for ConnectError<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        self.kind.fmt(f)
    }
}

impl<T> fmt::Display for ConnectError<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.kind.fmt(f)
    }
}

impl<T> Error for ConnectError<T> {}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// An error that can occur when sending MIDI messages.
pub enum SendError {
    InvalidData(&'static str),
    Other(&'static str)
}

impl Error for SendError {}

impl fmt::Display for SendError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            SendError::InvalidData(msg) | SendError::Other(msg) => msg.fmt(f)
        }
    }
}