use std::error::Error;
use std::fmt::{Display, Formatter};
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct HostUnavailable;
impl Display for HostUnavailable {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str("the requested host is unavailable")
}
}
impl Error for HostUnavailable {}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct BackendSpecificError {
pub description: String,
}
impl Display for BackendSpecificError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"A backend-specific error has occurred: {}",
self.description
)
}
}
impl Error for BackendSpecificError {}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum DevicesError {
BackendSpecific { err: BackendSpecificError },
}
impl Display for DevicesError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::BackendSpecific { err } => err.fmt(f),
}
}
}
impl Error for DevicesError {}
impl From<BackendSpecificError> for DevicesError {
fn from(err: BackendSpecificError) -> Self {
Self::BackendSpecific { err }
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DeviceIdError {
BackendSpecific {
err: BackendSpecificError,
},
UnsupportedPlatform,
}
impl Display for DeviceIdError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::BackendSpecific { err } => err.fmt(f),
Self::UnsupportedPlatform => f.write_str("Device IDs are unsupported for this OS"),
}
}
}
impl Error for DeviceIdError {}
impl From<BackendSpecificError> for DeviceIdError {
fn from(err: BackendSpecificError) -> Self {
Self::BackendSpecific { err }
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum DeviceNameError {
BackendSpecific { err: BackendSpecificError },
}
impl Display for DeviceNameError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::BackendSpecific { err } => err.fmt(f),
}
}
}
impl Error for DeviceNameError {}
impl From<BackendSpecificError> for DeviceNameError {
fn from(err: BackendSpecificError) -> Self {
Self::BackendSpecific { err }
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum SupportedStreamConfigsError {
DeviceNotAvailable,
InvalidArgument,
BackendSpecific { err: BackendSpecificError },
}
impl Display for SupportedStreamConfigsError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::BackendSpecific { err } => err.fmt(f),
Self::DeviceNotAvailable => f.write_str("The requested device is no longer available. For example, it has been unplugged."),
Self::InvalidArgument => f.write_str("Invalid argument passed to the backend. For example, this happens when trying to read capture capabilities when the device does not support it.")
}
}
}
impl Error for SupportedStreamConfigsError {}
impl From<BackendSpecificError> for SupportedStreamConfigsError {
fn from(err: BackendSpecificError) -> Self {
Self::BackendSpecific { err }
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum DefaultStreamConfigError {
DeviceNotAvailable,
StreamTypeNotSupported,
BackendSpecific { err: BackendSpecificError },
}
impl Display for DefaultStreamConfigError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::BackendSpecific { err } => err.fmt(f),
Self::DeviceNotAvailable => f.write_str(
"The requested device is no longer available. For example, it has been unplugged.",
),
Self::StreamTypeNotSupported => {
f.write_str("The requested stream type is not supported by the device.")
}
}
}
}
impl Error for DefaultStreamConfigError {}
impl From<BackendSpecificError> for DefaultStreamConfigError {
fn from(err: BackendSpecificError) -> Self {
Self::BackendSpecific { err }
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum BuildStreamError {
DeviceNotAvailable,
StreamConfigNotSupported,
InvalidArgument,
StreamIdOverflow,
BackendSpecific { err: BackendSpecificError },
}
impl Display for BuildStreamError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::BackendSpecific { err } => err.fmt(f),
Self::DeviceNotAvailable => f.write_str(
"The requested device is no longer available. For example, it has been unplugged.",
),
Self::StreamConfigNotSupported => {
f.write_str("The requested stream configuration is not supported by the device.")
}
Self::InvalidArgument => f.write_str(
"The requested device does not support this capability (invalid argument)",
),
Self::StreamIdOverflow => f.write_str("Adding a new stream ID would cause an overflow"),
}
}
}
impl Error for BuildStreamError {}
impl From<BackendSpecificError> for BuildStreamError {
fn from(err: BackendSpecificError) -> Self {
Self::BackendSpecific { err }
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum PlayStreamError {
DeviceNotAvailable,
BackendSpecific { err: BackendSpecificError },
}
impl Display for PlayStreamError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::BackendSpecific { err } => err.fmt(f),
Self::DeviceNotAvailable => {
f.write_str("the device associated with the stream is no longer available")
}
}
}
}
impl Error for PlayStreamError {}
impl From<BackendSpecificError> for PlayStreamError {
fn from(err: BackendSpecificError) -> Self {
Self::BackendSpecific { err }
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum PauseStreamError {
DeviceNotAvailable,
BackendSpecific { err: BackendSpecificError },
}
impl Display for PauseStreamError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::BackendSpecific { err } => err.fmt(f),
Self::DeviceNotAvailable => {
f.write_str("the device associated with the stream is no longer available")
}
}
}
}
impl Error for PauseStreamError {}
impl From<BackendSpecificError> for PauseStreamError {
fn from(err: BackendSpecificError) -> Self {
Self::BackendSpecific { err }
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum StreamError {
DeviceNotAvailable,
StreamInvalidated,
BufferUnderrun,
BackendSpecific { err: BackendSpecificError },
}
impl Display for StreamError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::BackendSpecific { err } => err.fmt(f),
Self::StreamInvalidated => {
f.write_str("The stream configuration is no longer valid and must be rebuilt.")
}
Self::BufferUnderrun => f.write_str("Buffer underrun/overrun occurred."),
Self::DeviceNotAvailable => f.write_str(
"The requested device is no longer available. For example, it has been unplugged.",
),
}
}
}
impl Error for StreamError {}
impl From<BackendSpecificError> for StreamError {
fn from(err: BackendSpecificError) -> Self {
Self::BackendSpecific { err }
}
}