Skip to main content

deepmind_midi/transport/
error.rs

1//! What a blocking call fails with.
2
3use core::fmt;
4
5use crate::device::Request;
6
7/// Everything a [`Transport`](super::Transport) call can fail with.
8///
9/// Generic over the port's own error, which travels through unchanged, so a host
10/// asks its own backend what went wrong rather than parsing a string from here.
11///
12/// Waiting ends without an answer two ways: [`Timeout`](Error::Timeout) is the
13/// synthesizer's silence, and [`Elapsed`](Error::Elapsed) is the caller's own
14/// limit running out first.
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16#[non_exhaustive]
17pub enum Error<E> {
18    /// The port failed, and what it failed with.
19    Port(E),
20    /// The library refused what was asked of it.
21    ///
22    /// A queue with no room, an edit before any program is known, a value
23    /// outside a parameter's range: the same rejections
24    /// [`Device`](crate::device::Device) makes, since a blocking call is those
25    /// calls with a loop around them.
26    Protocol(crate::Error),
27    /// The synthesizer did not answer inside
28    /// [`Device::with_timeout`](crate::device::Device::with_timeout).
29    ///
30    /// Nothing is retried. Only the host knows whether asking again is the right
31    /// thing to do, and for a bank half-read it usually is not.
32    Timeout(Request),
33    /// The limit given to [`wait_for`](super::Transport::wait_for) passed.
34    ///
35    /// The synthesizer has not been declared late; the device timeout is what
36    /// says that. This is only the host's own limit running out.
37    Elapsed,
38}
39
40impl<E> Error<E> {
41    /// Returns the port's error, for the one variant that carries it.
42    #[must_use]
43    pub const fn port(&self) -> Option<&E> {
44        match self {
45            Self::Port(error) => Some(error),
46            _ => None,
47        }
48    }
49
50    /// Takes the port's error, for the one variant that carries it.
51    #[must_use]
52    pub fn into_port(self) -> Option<E> {
53        match self {
54            Self::Port(error) => Some(error),
55            _ => None,
56        }
57    }
58
59    /// Returns the library's rejection, for the one variant that carries it.
60    #[must_use]
61    pub const fn protocol(&self) -> Option<crate::Error> {
62        match self {
63            Self::Protocol(error) => Some(*error),
64            _ => None,
65        }
66    }
67
68    /// Returns the request that went unanswered, for the one variant about one.
69    #[must_use]
70    pub const fn request(&self) -> Option<Request> {
71        match self {
72            Self::Timeout(request) => Some(*request),
73            _ => None,
74        }
75    }
76}
77
78impl<E> From<crate::Error> for Error<E> {
79    fn from(error: crate::Error) -> Self {
80        Self::Protocol(error)
81    }
82}
83
84impl<E: fmt::Display> fmt::Display for Error<E> {
85    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86        match self {
87            Self::Port(error) => write!(f, "MIDI port: {error}"),
88            Self::Protocol(error) => error.fmt(f),
89            Self::Timeout(request) => write!(f, "no answer to the {request}"),
90            Self::Elapsed => f.write_str("waited longer than the caller allowed"),
91        }
92    }
93}
94
95impl<E: core::error::Error + 'static> core::error::Error for Error<E> {
96    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
97        match self {
98            Self::Port(error) => Some(error),
99            Self::Protocol(error) => Some(error),
100            _ => None,
101        }
102    }
103}