mpris_server/
loop_status.rs

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