Merge pull request #340 from mitchmindtree/thiserror

Switch to dtolnay's `thiserror` for handling error boilerplate
This commit is contained in:
est31 2019-10-13 12:58:52 +02:00 committed by GitHub
commit 5fa5ae8cff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 52 additions and 107 deletions

View File

@ -12,11 +12,12 @@ keywords = ["audio", "sound"]
asio = ["asio-sys"] # Only available on Windows. See README for setup instructions. asio = ["asio-sys"] # Only available on Windows. See README for setup instructions.
[dependencies] [dependencies]
failure = "0.1.5" thiserror = "1.0.2"
lazy_static = "1.3" lazy_static = "1.3"
num-traits = "0.2.6" num-traits = "0.2.6"
[dev-dependencies] [dev-dependencies]
anyhow = "1.0.12"
hound = "3.4" hound = "3.4"
ringbuf = "0.1.6" ringbuf = "0.1.6"

View File

@ -1,9 +1,9 @@
extern crate anyhow;
extern crate cpal; extern crate cpal;
extern crate failure;
use cpal::traits::{DeviceTrait, EventLoopTrait, HostTrait}; use cpal::traits::{DeviceTrait, EventLoopTrait, HostTrait};
fn main() -> Result<(), failure::Error> { fn main() -> Result<(), anyhow::Error> {
let host = cpal::default_host(); let host = cpal::default_host();
let device = host.default_output_device().expect("failed to find a default output device"); let device = host.default_output_device().expect("failed to find a default output device");
let format = device.default_output_format()?; let format = device.default_output_format()?;

View File

@ -1,9 +1,9 @@
extern crate cpal; extern crate cpal;
extern crate failure; extern crate anyhow;
use cpal::traits::{DeviceTrait, HostTrait}; use cpal::traits::{DeviceTrait, HostTrait};
fn main() -> Result<(), failure::Error> { fn main() -> Result<(), anyhow::Error> {
println!("Supported hosts:\n {:?}", cpal::ALL_HOSTS); println!("Supported hosts:\n {:?}", cpal::ALL_HOSTS);
let available_hosts = cpal::available_hosts(); let available_hosts = cpal::available_hosts();
println!("Available hosts:\n {:?}", available_hosts); println!("Available hosts:\n {:?}", available_hosts);

View File

@ -6,8 +6,8 @@
//! Uses a delay of `LATENCY_MS` milliseconds in case the default input and output streams are not //! Uses a delay of `LATENCY_MS` milliseconds in case the default input and output streams are not
//! precisely synchronised. //! precisely synchronised.
extern crate anyhow;
extern crate cpal; extern crate cpal;
extern crate failure;
extern crate ringbuf; extern crate ringbuf;
use cpal::traits::{DeviceTrait, EventLoopTrait, HostTrait}; use cpal::traits::{DeviceTrait, EventLoopTrait, HostTrait};
@ -15,7 +15,7 @@ use ringbuf::RingBuffer;
const LATENCY_MS: f32 = 150.0; const LATENCY_MS: f32 = 150.0;
fn main() -> Result<(), failure::Error> { fn main() -> Result<(), anyhow::Error> {
let host = cpal::default_host(); let host = cpal::default_host();
let event_loop = host.event_loop(); let event_loop = host.event_loop();

View File

@ -2,13 +2,13 @@
//! //!
//! The input data is recorded to "$CARGO_MANIFEST_DIR/recorded.wav". //! The input data is recorded to "$CARGO_MANIFEST_DIR/recorded.wav".
extern crate anyhow;
extern crate cpal; extern crate cpal;
extern crate failure;
extern crate hound; extern crate hound;
use cpal::traits::{DeviceTrait, EventLoopTrait, HostTrait}; use cpal::traits::{DeviceTrait, EventLoopTrait, HostTrait};
fn main() -> Result<(), failure::Error> { fn main() -> Result<(), anyhow::Error> {
// Use the default host for working with audio devices. // Use the default host for working with audio devices.
let host = cpal::default_host(); let host = cpal::default_host();

View File

@ -1,8 +1,8 @@
use failure::Fail; use thiserror::Error;
/// The requested host, although supported on this platform, is unavailable. /// The requested host, although supported on this platform, is unavailable.
#[derive(Clone, Debug, Fail)] #[derive(Clone, Debug, Error)]
#[fail(display = "the requested host is unavailable")] #[error("the requested host is unavailable")]
pub struct HostUnavailable; pub struct HostUnavailable;
/// Some error has occurred that is specific to the backend from which it was produced. /// Some error has occurred that is specific to the backend from which it was produced.
@ -17,101 +17,95 @@ pub struct HostUnavailable;
/// **Note:** If you notice a `BackendSpecificError` that you believe could be better handled in a /// **Note:** If you notice a `BackendSpecificError` that you believe could be better handled in a
/// cross-platform manner, please create an issue or submit a pull request with a patch that adds /// cross-platform manner, please create an issue or submit a pull request with a patch that adds
/// the necessary error variant to the appropriate error enum. /// the necessary error variant to the appropriate error enum.
#[derive(Clone, Debug, Fail)] #[derive(Clone, Debug, Error)]
#[fail(display = "A backend-specific error has occurred: {}", description)] #[error("A backend-specific error has occurred: {description}")]
pub struct BackendSpecificError { pub struct BackendSpecificError {
pub description: String, pub description: String,
} }
/// An error that might occur while attempting to enumerate the available devices on a system. /// An error that might occur while attempting to enumerate the available devices on a system.
#[derive(Debug, Fail)] #[derive(Debug, Error)]
pub enum DevicesError { pub enum DevicesError {
/// See the `BackendSpecificError` docs for more information about this error variant. /// See the `BackendSpecificError` docs for more information about this error variant.
#[fail(display = "{}", err)] #[error("{err}")]
BackendSpecific { BackendSpecific {
#[fail(cause)] #[from]
err: BackendSpecificError, err: BackendSpecificError,
}, },
} }
/// An error that may occur while attempting to retrieve a device name. /// An error that may occur while attempting to retrieve a device name.
#[derive(Debug, Fail)] #[derive(Debug, Error)]
pub enum DeviceNameError { pub enum DeviceNameError {
/// See the `BackendSpecificError` docs for more information about this error variant. /// See the `BackendSpecificError` docs for more information about this error variant.
#[fail(display = "{}", err)] #[error("{err}")]
BackendSpecific { BackendSpecific {
#[fail(cause)] #[from]
err: BackendSpecificError, err: BackendSpecificError,
}, },
} }
/// Error that can happen when enumerating the list of supported formats. /// Error that can happen when enumerating the list of supported formats.
#[derive(Debug, Fail)] #[derive(Debug, Error)]
pub enum SupportedFormatsError { pub enum SupportedFormatsError {
/// The device no longer exists. This can happen if the device is disconnected while the /// The device no longer exists. This can happen if the device is disconnected while the
/// program is running. /// program is running.
#[fail( #[error("The requested device is no longer available. For example, it has been unplugged.")]
display = "The requested device is no longer available. For example, it has been unplugged."
)]
DeviceNotAvailable, DeviceNotAvailable,
/// We called something the C-Layer did not understand /// We called something the C-Layer did not understand
#[fail( #[error(
display = "Invalid argument passed to the backend. For example, this happens when trying to read capture capabilities when the device does not support it." "Invalid argument passed to the backend. For example, this happens when trying to read capture capabilities when the device does not support it."
)] )]
InvalidArgument, InvalidArgument,
/// See the `BackendSpecificError` docs for more information about this error variant. /// See the `BackendSpecificError` docs for more information about this error variant.
#[fail(display = "{}", err)] #[error("{err}")]
BackendSpecific { BackendSpecific {
#[fail(cause)] #[from]
err: BackendSpecificError, err: BackendSpecificError,
}, },
} }
/// May occur when attempting to request the default input or output stream format from a `Device`. /// May occur when attempting to request the default input or output stream format from a `Device`.
#[derive(Debug, Fail)] #[derive(Debug, Error)]
pub enum DefaultFormatError { pub enum DefaultFormatError {
/// The device no longer exists. This can happen if the device is disconnected while the /// The device no longer exists. This can happen if the device is disconnected while the
/// program is running. /// program is running.
#[fail( #[error("The requested device is no longer available. For example, it has been unplugged.")]
display = "The requested device is no longer available. For example, it has been unplugged."
)]
DeviceNotAvailable, DeviceNotAvailable,
/// Returned if e.g. the default input format was requested on an output-only audio device. /// Returned if e.g. the default input format was requested on an output-only audio device.
#[fail(display = "The requested stream type is not supported by the device.")] #[error("The requested stream type is not supported by the device.")]
StreamTypeNotSupported, StreamTypeNotSupported,
/// See the `BackendSpecificError` docs for more information about this error variant. /// See the `BackendSpecificError` docs for more information about this error variant.
#[fail(display = "{}", err)] #[error("{err}")]
BackendSpecific { BackendSpecific {
#[fail(cause)] #[from]
err: BackendSpecificError, err: BackendSpecificError,
}, },
} }
/// Error that can happen when creating a `Stream`. /// Error that can happen when creating a `Stream`.
#[derive(Debug, Fail)] #[derive(Debug, Error)]
pub enum BuildStreamError { pub enum BuildStreamError {
/// The device no longer exists. This can happen if the device is disconnected while the /// The device no longer exists. This can happen if the device is disconnected while the
/// program is running. /// program is running.
#[fail( #[error("The requested device is no longer available. For example, it has been unplugged.")]
display = "The requested device is no longer available. For example, it has been unplugged."
)]
DeviceNotAvailable, DeviceNotAvailable,
/// The required format is not supported. /// The required format is not supported.
#[fail(display = "The requested stream format is not supported by the device.")] #[error("The requested stream format is not supported by the device.")]
FormatNotSupported, FormatNotSupported,
/// We called something the C-Layer did not understand /// We called something the C-Layer did not understand
/// ///
/// On ALSA device functions called with a feature they do not support will yield this. E.g. /// On ALSA device functions called with a feature they do not support will yield this. E.g.
/// Trying to use capture capabilities on an output only format yields this. /// Trying to use capture capabilities on an output only format yields this.
#[fail(display = "The requested device does not support this capability (invalid argument)")] #[error("The requested device does not support this capability (invalid argument)")]
InvalidArgument, InvalidArgument,
/// Occurs if adding a new Stream ID would cause an integer overflow. /// Occurs if adding a new Stream ID would cause an integer overflow.
#[fail(display = "Adding a new stream ID would cause an overflow")] #[error("Adding a new stream ID would cause an overflow")]
StreamIdOverflow, StreamIdOverflow,
/// See the `BackendSpecificError` docs for more information about this error variant. /// See the `BackendSpecificError` docs for more information about this error variant.
#[fail(display = "{}", err)] #[error("{err}")]
BackendSpecific { BackendSpecific {
#[fail(cause)] #[from]
err: BackendSpecificError, err: BackendSpecificError,
}, },
} }
@ -121,15 +115,15 @@ pub enum BuildStreamError {
/// As of writing this, only macOS may immediately return an error while calling this method. This /// As of writing this, only macOS may immediately return an error while calling this method. This
/// is because both the alsa and wasapi backends only enqueue these commands and do not process /// is because both the alsa and wasapi backends only enqueue these commands and do not process
/// them immediately. /// them immediately.
#[derive(Debug, Fail)] #[derive(Debug, Error)]
pub enum PlayStreamError { pub enum PlayStreamError {
/// The device associated with the stream is no longer available. /// The device associated with the stream is no longer available.
#[fail(display = "the device associated with the stream is no longer available")] #[error("the device associated with the stream is no longer available")]
DeviceNotAvailable, DeviceNotAvailable,
/// See the `BackendSpecificError` docs for more information about this error variant. /// See the `BackendSpecificError` docs for more information about this error variant.
#[fail(display = "{}", err)] #[error("{err}")]
BackendSpecific { BackendSpecific {
#[fail(cause)] #[from]
err: BackendSpecificError, err: BackendSpecificError,
}, },
} }
@ -139,80 +133,30 @@ pub enum PlayStreamError {
/// As of writing this, only macOS may immediately return an error while calling this method. This /// As of writing this, only macOS may immediately return an error while calling this method. This
/// is because both the alsa and wasapi backends only enqueue these commands and do not process /// is because both the alsa and wasapi backends only enqueue these commands and do not process
/// them immediately. /// them immediately.
#[derive(Debug, Fail)] #[derive(Debug, Error)]
pub enum PauseStreamError { pub enum PauseStreamError {
/// The device associated with the stream is no longer available. /// The device associated with the stream is no longer available.
#[fail(display = "the device associated with the stream is no longer available")] #[error("the device associated with the stream is no longer available")]
DeviceNotAvailable, DeviceNotAvailable,
/// See the `BackendSpecificError` docs for more information about this error variant. /// See the `BackendSpecificError` docs for more information about this error variant.
#[fail(display = "{}", err)] #[error("{err}")]
BackendSpecific { BackendSpecific {
#[fail(cause)] #[from]
err: BackendSpecificError, err: BackendSpecificError,
}, },
} }
/// Errors that might occur while a stream is running. /// Errors that might occur while a stream is running.
#[derive(Debug, Fail)] #[derive(Debug, Error)]
pub enum StreamError { pub enum StreamError {
/// The device no longer exists. This can happen if the device is disconnected while the /// The device no longer exists. This can happen if the device is disconnected while the
/// program is running. /// program is running.
#[fail( #[error("The requested device is no longer available. For example, it has been unplugged.")]
display = "The requested device is no longer available. For example, it has been unplugged."
)]
DeviceNotAvailable, DeviceNotAvailable,
/// See the `BackendSpecificError` docs for more information about this error variant. /// See the `BackendSpecificError` docs for more information about this error variant.
#[fail(display = "{}", err)] #[error("{err}")]
BackendSpecific { BackendSpecific {
#[fail(cause)] #[from]
err: BackendSpecificError, err: BackendSpecificError,
}, },
} }
impl From<BackendSpecificError> for DevicesError {
fn from(err: BackendSpecificError) -> Self {
DevicesError::BackendSpecific { err }
}
}
impl From<BackendSpecificError> for DeviceNameError {
fn from(err: BackendSpecificError) -> Self {
DeviceNameError::BackendSpecific { err }
}
}
impl From<BackendSpecificError> for SupportedFormatsError {
fn from(err: BackendSpecificError) -> Self {
SupportedFormatsError::BackendSpecific { err }
}
}
impl From<BackendSpecificError> for DefaultFormatError {
fn from(err: BackendSpecificError) -> Self {
DefaultFormatError::BackendSpecific { err }
}
}
impl From<BackendSpecificError> for BuildStreamError {
fn from(err: BackendSpecificError) -> Self {
BuildStreamError::BackendSpecific { err }
}
}
impl From<BackendSpecificError> for PlayStreamError {
fn from(err: BackendSpecificError) -> Self {
PlayStreamError::BackendSpecific { err }
}
}
impl From<BackendSpecificError> for PauseStreamError {
fn from(err: BackendSpecificError) -> Self {
PauseStreamError::BackendSpecific { err }
}
}
impl From<BackendSpecificError> for StreamError {
fn from(err: BackendSpecificError) -> Self {
StreamError::BackendSpecific { err }
}
}

View File

@ -140,7 +140,6 @@
#![recursion_limit = "512"] #![recursion_limit = "512"]
extern crate failure;
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
@ -148,6 +147,7 @@ extern crate lazy_static;
#[cfg(target_os = "emscripten")] #[cfg(target_os = "emscripten")]
#[macro_use] #[macro_use]
extern crate stdweb; extern crate stdweb;
extern crate thiserror;
pub use error::*; pub use error::*;
pub use platform::{ pub use platform::{