Skip to content

Commit

Permalink
connect: apply review suggestions
Browse files Browse the repository at this point in the history
- use drain instead of for with pop
- use for instead of loop
- use or_else instead of match
- use Self::Error instead of the value
- free memory for metadata and restrictions
  • Loading branch information
photovoltex committed Dec 23, 2024
1 parent 309ca8d commit 3cf4197
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
17 changes: 7 additions & 10 deletions connect/src/context_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,30 +198,27 @@ impl ContextResolver {

pub fn remove_used_and_invalid(&mut self) {
if let Some((_, _, remove)) = self.find_next() {
for _ in 0..remove {
let _ = self.queue.pop_front();
}
let _ = self.queue.drain(0..remove); // remove invalid
}
self.queue.pop_front();
self.queue.pop_front(); // remove used
}

pub fn clear(&mut self) {
self.queue = VecDeque::new()
}

fn find_next(&self) -> Option<(&ResolveContext, &str, usize)> {
let mut idx = 0;
loop {
for idx in 0..self.queue.len() {
let next = self.queue.get(idx)?;
match next.resolve_uri() {
None if idx < self.queue.len() => {
warn!("skipped {idx} because of no valid resolve_uri: {next}");
idx += 1;
None => {
warn!("skipped {idx} because of invalid resolve_uri: {next}");
continue;
}
value => break value.map(|uri| (next, uri, idx)),
Some(uri) => return Some((next, uri, idx)),
}
}
None
}

pub fn has_next(&self) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion connect/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub enum PlayingTrack {
impl TryFrom<SkipTo> for PlayingTrack {
type Error = ();

fn try_from(value: SkipTo) -> Result<Self, ()> {
fn try_from(value: SkipTo) -> Result<Self, Self::Error> {
// order of checks is important, as the index can be 0, but still has an uid or uri provided,
// so we only use the index as last resort
if let Some(uri) = value.track_uri {
Expand Down
14 changes: 7 additions & 7 deletions connect/src/state/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,21 @@ impl ConnectState {
}

pub fn get_context_uri_from_context(context: &Context) -> Option<&str> {
match Self::valid_resolve_uri(&context.uri) {
Some(uri) => Some(uri),
None => context
Self::valid_resolve_uri(&context.uri).or_else(|| {
context
.pages
.first()
.and_then(|p| p.tracks.first().map(|t| t.uri.as_ref())),
}
.and_then(|p| p.tracks.first().map(|t| t.uri.as_ref()))
})
}

pub fn set_active_context(&mut self, new_context: ContextType) {
self.active_context = new_context;

let player = self.player_mut();
player.context_metadata.clear();
player.restrictions.clear();

player.context_metadata = Default::default();
player.restrictions = Some(Default::default()).into();

let ctx = match self.get_context(new_context) {
Err(why) => {
Expand Down

0 comments on commit 3cf4197

Please sign in to comment.