Skip to main content

mpris_server/
loop_status.rs

1use std::fmt;
2
3use serde::{Deserialize, Serialize};
4use zbus::{Type, Variant};
5
6/// A repeat / loop status.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Type, Deserialize, Serialize)]
8#[zvariant(signature = "s")]
9#[doc(alias = "Loop_Status")]
10pub enum LoopStatus {
11    /// The playback will stop when there are no more tracks to play.
12    None,
13    /// The current track will start again from the beginning once it has
14    /// finished playing.
15    Track,
16    /// The playback loops through a list of tracks.
17    Playlist,
18}
19
20impl LoopStatus {
21    /// Returns the string representation of this loop status.
22    pub fn as_str(&self) -> &'static str {
23        match self {
24            Self::None => "None",
25            Self::Track => "Track",
26            Self::Playlist => "Playlist",
27        }
28    }
29}
30
31impl fmt::Display for LoopStatus {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        f.write_str(self.as_str())
34    }
35}
36
37impl<'a> TryFrom<Variant<'a>> for LoopStatus {
38    type Error = zbus::Error;
39
40    fn try_from(value: Variant<'a>) -> Result<Self, Self::Error> {
41        match value {
42            Variant::Str(s) => match s.as_str() {
43                "None" => Ok(Self::None),
44                "Track" => Ok(Self::Track),
45                "Playlist" => Ok(Self::Playlist),
46                _ => Err(zbus::Error::Failure(format!("invalid loop status: {s}"))),
47            },
48            _ => Err(zbus::Error::IncorrectType),
49        }
50    }
51}
52
53impl From<LoopStatus> for Variant<'_> {
54    fn from(status: LoopStatus) -> Self {
55        Variant::new(status.as_str())
56    }
57}