fix: reject incomplete M13 player matches and unsupported PlayPause

Verify: new regression tests failed before fixes; cargo test -p media --quiet: 4 passed; cargo test -p tui --quiet: 55 passed; cargo fmt --all passed. PlayPause capability checked against the MPRIS Player specification.
This commit is contained in:
Dandaedre 2026-10-18 23:00:42 +02:00
parent 431669c994
commit 195f3974c6
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
1 changed files with 52 additions and 5 deletions

View File

@ -12,6 +12,8 @@ pub enum MediaError {
Ambiguous,
#[error("unsupported player action")]
Unsupported,
#[error("player discovery incomplete: {0}")]
Unavailable(String),
#[error("invalid media configuration: {0}")]
Invalid(String),
}
@ -78,11 +80,19 @@ impl PlayerMatcher {
})
}
pub fn resolve<'a>(&self, players: &'a [Player]) -> Result<&'a Player, MediaError> {
if self.selector.field != PlayerField::BusName
&& let Some(error) = players.iter().find_map(|p| p.error.as_ref())
{
return Err(MediaError::Unavailable(error.clone()));
}
let mut matches = players.iter().filter(|p| self.matches(p));
let first = matches.next().ok_or(MediaError::Offline)?;
if matches.next().is_some() {
return Err(MediaError::Ambiguous);
}
if let Some(error) = &first.error {
return Err(MediaError::Unavailable(error.clone()));
}
Ok(first)
}
}
@ -142,11 +152,14 @@ impl Action {
match self {
Self::Play => p.can_play,
Self::Pause => p.can_pause,
Self::PlayPause => match p.playback {
Some(Playback::Playing) => p.can_pause,
Some(_) => p.can_play,
None => false,
},
Self::PlayPause => {
p.can_pause
&& match p.playback {
Some(Playback::Playing) => p.can_pause,
Some(_) => p.can_play,
None => false,
}
}
Self::Stop => true,
Self::Next => p.can_next,
Self::Previous => p.can_previous,
@ -357,6 +370,40 @@ impl Entry {
mod tests {
use super::*;
#[test]
fn failed_identity_reads_cannot_hide_an_ambiguous_target() {
let matcher = PlayerSelector {
field: PlayerField::Identity,
pattern: "QBZ".into(),
regex: false,
}
.compile()
.unwrap();
let players = [
Player {
identity: Some("QBZ".into()),
..Player::default()
},
Player {
error: Some("timed out".into()),
..Player::default()
},
];
assert!(matcher.resolve(&players).is_err());
}
#[test]
fn play_pause_requires_the_protocol_pause_capability() {
let player = Player {
playback: Some(Playback::Paused),
can_control: true,
can_play: true,
can_pause: false,
..Player::default()
};
assert!(!Action::PlayPause.supported(&player));
}
#[test]
fn matching_is_explicit_and_never_picks_an_ambiguous_player() {
let selector = PlayerSelector {