1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use std::{error, fmt, str::FromStr};

use serde::Deserialize;
use zbus::zvariant::{Type, Value};

/// Specifies the ordering of returned playlists.
///
/// # Rationale
///
/// Some media players may allow users to order playlists
/// as they wish. This ordering allows playlists to be retrieved
/// in that order.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Type)]
#[zvariant(signature = "s")]
pub enum PlaylistOrdering {
    /// Alphabetical ordering by name, ascending.
    Alphabetical,
    /// Ordering by creation date, oldest first.
    CreationDate,
    /// Ordering by last modified date, oldest first.
    ModifiedDate,
    /// Ordering by date of last playback, oldest first.
    LastPlayDate,
    /// A user-defined ordering.
    UserDefined,
}

impl PlaylistOrdering {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Alphabetical => "Alphabetical",
            Self::CreationDate => "Created",
            Self::ModifiedDate => "Modified",
            Self::LastPlayDate => "Played",
            Self::UserDefined => "User",
        }
    }
}

impl fmt::Display for PlaylistOrdering {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

#[derive(Debug, PartialEq, Eq)]
pub struct ParsePlaylistOrderingError;

impl fmt::Display for ParsePlaylistOrderingError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("invalid playlist ordering")
    }
}

impl error::Error for ParsePlaylistOrderingError {}

impl FromStr for PlaylistOrdering {
    type Err = ParsePlaylistOrderingError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "Alphabetical" => Ok(Self::Alphabetical),
            "Created" => Ok(Self::CreationDate),
            "Modified" => Ok(Self::ModifiedDate),
            "Played" => Ok(Self::LastPlayDate),
            "User" => Ok(Self::UserDefined),
            _ => Err(ParsePlaylistOrderingError),
        }
    }
}

impl<'a> From<PlaylistOrdering> for Value<'a> {
    fn from(status: PlaylistOrdering) -> Self {
        Value::new(status.as_str())
    }
}