Driver, Tracks: Cleanup of leaky types (#20)
Main goal: a lot of nested future/result folding. This mainly modifies error handling for Tracks and TrackHandles to be more consistent, and hides the underlying channel result passing in get_info. Errors returned should be far clearer, and are domain specific rather than falling back to a very opaque use of the underlying channel error. It should be clearer to users why their handle commands failed, or why they can't make a ytdl track loop or similar. Also fixed/cleaned up Songbird::join(_gateway) to return in a single await, sparing the user from the underlying channel details and repeated Errs. I was trying for some time to extend the same graces to `Call`, but could not figure out a sane way to get a 'static version of the first future in the chain (i.e., the gateway send) so that the whole thing could happen after dropping the lock around the Call. I really wanted to fix this to happen as a single folded await too, but I think this might need some crazy hack or redesign.
This commit is contained in:
39
src/tracks/error.rs
Normal file
39
src/tracks/error.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
use std::{error::Error, fmt};
|
||||
|
||||
/// Errors associated with control and manipulation of tracks.
|
||||
///
|
||||
/// Unless otherwise stated, these don't invalidate an existing track,
|
||||
/// but do advise on valid operations and commands.
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
#[non_exhaustive]
|
||||
pub enum TrackError {
|
||||
/// The operation failed because the track has ended, has been removed
|
||||
/// due to call closure, or some error within the driver.
|
||||
Finished,
|
||||
/// The supplied event listener can never be fired by a track, and should
|
||||
/// be attached to the driver instead.
|
||||
InvalidTrackEvent,
|
||||
/// The track's underlying [`Input`] doesn't support seeking operations.
|
||||
///
|
||||
/// [`Input`]: crate::input::Input
|
||||
SeekUnsupported,
|
||||
}
|
||||
|
||||
impl fmt::Display for TrackError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "Failed to operate on track (handle): ")?;
|
||||
match self {
|
||||
TrackError::Finished => write!(f, "track ended."),
|
||||
TrackError::InvalidTrackEvent =>
|
||||
write!(f, "given event listener can't be fired on a track."),
|
||||
TrackError::SeekUnsupported => write!(f, "track did not support seeking."),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for TrackError {}
|
||||
|
||||
/// Alias for most calls to a [`TrackHandle`].
|
||||
///
|
||||
/// [`TrackHandle`]: super::TrackHandle
|
||||
pub type TrackResult<T> = Result<T, TrackError>;
|
||||
Reference in New Issue
Block a user