Skip to content

Commit

Permalink
Fixed a logic error when no players are present.
Browse files Browse the repository at this point in the history
  • Loading branch information
ray-kast committed Nov 14, 2021
1 parent 185343e commit 4677e99
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "empress"
version = "1.4.1"
version = "1.4.2"
authors = ["rookie1024 <rookie1286@gmail.com>"]
edition = "2021"
description = "A D-Bus MPRIS daemon for controlling media players."
Expand Down
26 changes: 15 additions & 11 deletions src/server/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl Server {
f: F,
) -> Result<Option<T>> {
let mut players = self.players.write().await;
let mut patch = Err(());
let mut patch = Err(None);

// TODO: use drain_filter or something less stupid than this
for player in players.iter() {
Expand All @@ -146,11 +146,16 @@ impl Server {
break;
},
Ok(None) => patch = Ok(None),
Err(e) => warn!("processing player failed: {:?}", e),
Err(e) => {
warn!("processing player failed: {:?}", e);
patch = patch.map_err(|_| Some(()));
},
}
}

if let Some((next, ret)) = patch.map_err(|()| anyhow!("all players failed to process"))? {
if let Some((next, ret)) = patch
.or_else(|e| e.map_or(Ok(None), |()| Err(anyhow!("all players failed to process"))))?
{
players.put(next);

return Ok(Some(ret));
Expand All @@ -164,21 +169,20 @@ impl Server {
f: F,
) -> Result<Option<T>> {
let players = self.players.read().await;
let mut any_ok = false;
let mut default = Err(None);

for player in players.iter() {
match f(player.clone()).await {
ok @ Ok(Some(_)) => return ok,
Ok(None) => any_ok = true,
Err(e) => warn!("peeking player failed: {:?}", e),
Ok(None) => default = Ok(None),
Err(e) => {
warn!("peeking player failed: {:?}", e);
default = default.map_err(|_| Some(()));
},
}
}

if any_ok {
Ok(None)
} else {
Err(anyhow!("all players failed to peek"))
}
default.or_else(|e| e.map_or(Ok(None), |()| Err(anyhow!("all players failed to peek"))))
}

async fn process_player<F: Fn(Player) -> FR, FR: Future<Output = Result<Option<Player>>>>(
Expand Down

0 comments on commit 4677e99

Please sign in to comment.