Skip to content

Commit

Permalink
apply suggestions, improve errors
Browse files Browse the repository at this point in the history
  • Loading branch information
photovoltex committed Dec 17, 2024
1 parent 51eb100 commit 60378c6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
16 changes: 12 additions & 4 deletions connect/src/spirc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ use tokio::{sync::mpsc, time::sleep};
pub enum SpircError {
#[error("response payload empty")]
NoData,
#[error("{0} had no uri")]
NoUri(&'static str),
#[error("message pushed for another URI")]
InvalidUri(String),
#[error("tried resolving not allowed context: {0:?}")]
Expand All @@ -64,7 +66,7 @@ impl From<SpircError> for Error {
fn from(err: SpircError) -> Self {
use SpircError::*;
match err {
NoData | NotAllowedContext(_) => Error::unavailable(err),
NoData | NoUri(_) | NotAllowedContext(_) => Error::unavailable(err),
InvalidUri(_) | FailedDealerSetup => Error::aborted(err),
UnknownEndpoint(_) => Error::unimplemented(err),
}
Expand Down Expand Up @@ -1037,7 +1039,11 @@ impl SpircTask {
.map(|o| o.repeating_track.unwrap_or_default())
.unwrap_or_else(|| self.connect_state.repeat_track());

let context_uri = play.context.uri.as_ref().ok_or(SpircError::NoData)?.clone();
let context_uri = play
.context
.uri
.clone()
.ok_or(SpircError::NoUri("context"))?;

self.handle_load(
SpircLoadCommand {
Expand Down Expand Up @@ -1093,7 +1099,7 @@ impl SpircTask {

fn handle_transfer(&mut self, mut transfer: TransferState) -> Result<(), Error> {
let mut ctx_uri = match transfer.current_session.context.uri {
None => Err(SpircError::NoData)?,
None => Err(SpircError::NoUri("transfer context"))?,
Some(ref uri) => uri.clone(),
};

Expand Down Expand Up @@ -1517,7 +1523,9 @@ impl SpircTask {
&mut self,
playlist_modification_info: PlaylistModificationInfo,
) -> Result<(), Error> {
let uri = playlist_modification_info.uri.ok_or(SpircError::NoData)?;
let uri = playlist_modification_info
.uri
.ok_or(SpircError::NoUri("playlist modification"))?;
let uri = String::from_utf8(uri)?;

if self.connect_state.context_uri() != &uri {
Expand Down
8 changes: 4 additions & 4 deletions connect/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ const SPOTIFY_MAX_NEXT_TRACKS_SIZE: usize = 80;
pub enum StateError {
#[error("the current track couldn't be resolved from the transfer state")]
CouldNotResolveTrackFromTransfer,
#[error("message field {0} was not available")]
MessageFieldNone(String),
#[error("context is not available. type: {0:?}")]
NoContext(ContextType),
#[error("could not find track {0:?} in context of {1}")]
CanNotFindTrackInContext(Option<usize>, usize),
#[error("currently {action} is not allowed because {reason}")]
CurrentlyDisallowed { action: String, reason: String },
CurrentlyDisallowed {
action: &'static str,
reason: String,
},
#[error("the provided context has no tracks")]
ContextHasNoTracks,
#[error("playback of local files is not supported")]
Expand All @@ -65,7 +66,6 @@ impl From<StateError> for Error {
use StateError::*;
match err {
CouldNotResolveTrackFromTransfer
| MessageFieldNone(_)
| NoContext(_)
| CanNotFindTrackInContext(_, _)
| ContextHasNoTracks
Expand Down
6 changes: 3 additions & 3 deletions connect/src/state/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl ConnectState {
let mut new_context = self.state_context_from_page(
page,
context.restrictions.take(),
context.uri.as_ref(),
context.uri.as_deref(),
None,
);

Expand Down Expand Up @@ -236,7 +236,7 @@ impl ConnectState {
self.autoplay_context = Some(self.state_context_from_page(
page,
context.restrictions.take(),
context.uri.as_ref(),
context.uri.as_deref(),
Some(Provider::Autoplay),
))
}
Expand All @@ -249,7 +249,7 @@ impl ConnectState {
&mut self,
page: ContextPage,
restrictions: Option<Restrictions>,
new_context_uri: Option<&String>,
new_context_uri: Option<&str>,
provider: Option<Provider>,
) -> StateContext {
let new_context_uri = new_context_uri.unwrap_or(self.context_uri());
Expand Down
2 changes: 1 addition & 1 deletion connect/src/state/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl ConnectState {
.first()
{
Err(StateError::CurrentlyDisallowed {
action: "shuffle".to_string(),
action: "shuffle",
reason: reason.clone(),
})?
}
Expand Down

0 comments on commit 60378c6

Please sign in to comment.