1
use std::{fmt::Display, str::FromStr};
2

            
3
use serde::{Deserialize, Serialize};
4

            
5
/// These are different parts of the canonical MPD server.
6
/// They are mostly used in the protocol with the `idle` command,
7
/// signalling that the client is waiting for changes in any, one or multiple of these subsystems.
8
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9
pub enum SubSystem {
10
    /// The song database has been modified after update.
11
    Database,
12
    /// A database update has started or finished. If the database was modified during the update, the database event is also emitted.
13
    Update,
14
    /// A stored playlist has been modified, renamed, created or deleted
15
    StoredPlaylist,
16
    /// The queue (i.e. the current playlist) has been modified
17
    Playlist,
18
    /// The player has been started, stopped or seeked or tags of the currently playing song have changed (e.g. received from stream)
19
    Player,
20
    /// The volume has been changed
21
    Mixer,
22
    /// An audio output has been added, removed or modified (e.g. renamed, enabled or disabled)
23
    Output,
24
    /// Options like repeat, random, crossfade, replay gain
25
    Options,
26
    /// A partition was added, removed or changed
27
    Partition,
28
    /// The sticker database has been modified.
29
    Sticker,
30
    /// A client has subscribed or unsubscribed to a channel
31
    Subscription,
32
    /// A message was received on a channel this client is subscribed to; this event is only emitted when the client’s message queue is empty
33
    Message,
34
    /// A neighbor was found or lost
35
    Neighbor,
36
    /// The mount list has changed
37
    Mount,
38

            
39
    /// Other subsystems not covered by the above
40
    Other(String),
41
}
42

            
43
impl FromStr for SubSystem {
44
    type Err = ();
45

            
46
    fn from_str(s: &str) -> Result<Self, Self::Err> {
47
        Ok(match s {
48
            "database" => Self::Database,
49
            "update" => Self::Update,
50
            "stored_playlist" => Self::StoredPlaylist,
51
            "playlist" => Self::Playlist,
52
            "player" => Self::Player,
53
            "mixer" => Self::Mixer,
54
            "output" => Self::Output,
55
            "options" => Self::Options,
56
            "partition" => Self::Partition,
57
            "sticker" => Self::Sticker,
58
            "subscription" => Self::Subscription,
59
            "message" => Self::Message,
60
            "neighbor" => Self::Neighbor,
61
            "mount" => Self::Mount,
62
            other => Self::Other(other.to_string()),
63
        })
64
    }
65
}
66

            
67
impl Display for SubSystem {
68
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69
        match self {
70
            Self::Database => write!(f, "database"),
71
            Self::Update => write!(f, "update"),
72
            Self::StoredPlaylist => write!(f, "stored_playlist"),
73
            Self::Playlist => write!(f, "playlist"),
74
            Self::Player => write!(f, "player"),
75
            Self::Mixer => write!(f, "mixer"),
76
            Self::Output => write!(f, "output"),
77
            Self::Options => write!(f, "options"),
78
            Self::Partition => write!(f, "partition"),
79
            Self::Sticker => write!(f, "sticker"),
80
            Self::Subscription => write!(f, "subscription"),
81
            Self::Message => write!(f, "message"),
82
            Self::Neighbor => write!(f, "neighbor"),
83
            Self::Mount => write!(f, "mount"),
84
            Self::Other(other) => write!(f, "{}", other),
85
        }
86
    }
87
}