Skip to content

Commit

Permalink
connect: impl trait for player context
Browse files Browse the repository at this point in the history
  • Loading branch information
photovoltex committed Dec 23, 2024
1 parent 3cf4197 commit eb14e46
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 27 deletions.
34 changes: 8 additions & 26 deletions connect/src/context_resolver.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
use crate::state::context::ContextType;
use crate::{
core::{Error, Session},
protocol::{
autoplay_context_request::AutoplayContextRequest,
player::{Context, TransferState},
},
state::{context::UpdateContext, ConnectState},
state::{
context::{ContextType, UpdateContext},
ConnectState,
},
};
use std::cmp::PartialEq;
use std::{
collections::{HashMap, VecDeque},
fmt::{Display, Formatter},
hash::{Hash, Hasher},
hash::Hash,
time::Duration,
};
use thiserror::Error as ThisError;
use tokio::time::Instant;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
enum Resolve {
Uri(String),
Context(Context),
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub(super) enum ContextAction {
Append,
Replace,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub(super) struct ResolveContext {
resolve: Resolve,
fallback: Option<String>,
Expand Down Expand Up @@ -103,26 +105,6 @@ impl Display for ResolveContext {
}
}

impl PartialEq for ResolveContext {
fn eq(&self, other: &Self) -> bool {
let eq_context = self.context_uri() == other.context_uri();
let eq_resolve = self.resolve_uri() == other.resolve_uri();
let eq_autoplay = self.update == other.update;

eq_context && eq_resolve && eq_autoplay
}
}

impl Eq for ResolveContext {}

impl Hash for ResolveContext {
fn hash<H: Hasher>(&self, state: &mut H) {
self.context_uri().hash(state);
self.resolve_uri().hash(state);
self.update.hash(state);
}
}

#[derive(Debug, ThisError)]
enum ContextResolverError {
#[error("no next context to resolve")]
Expand Down
2 changes: 1 addition & 1 deletion connect/src/state/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub enum ContextType {
Autoplay,
}

#[derive(Debug, PartialEq, Hash, Copy, Clone)]
#[derive(Debug, Hash, Copy, Clone, PartialEq, Eq)]
pub enum UpdateContext {
Default,
Autoplay,
Expand Down
1 change: 1 addition & 0 deletions protocol/src/impl_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod player_context;
13 changes: 13 additions & 0 deletions protocol/src/impl_trait/player_context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use crate::player::Context;
use protobuf::Message;
use std::hash::{Hash, Hasher};

impl Hash for Context {
fn hash<H: Hasher>(&self, state: &mut H) {
if let Ok(ctx) = self.write_to_bytes() {
ctx.hash(state)
}
}
}

impl Eq for Context {}
2 changes: 2 additions & 0 deletions protocol/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// This file is parsed by build.rs
// Each included module will be compiled from the matching .proto definition.

mod impl_trait;

include!(concat!(env!("OUT_DIR"), "/mod.rs"));

0 comments on commit eb14e46

Please sign in to comment.