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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
use serde::Serialize;
use zbus::zvariant::{ObjectPath, Type, Value};
use crate::{PlaylistId, Uri};
/// A data structure describing a playlist.
///
/// See [`MaybePlaylist`] for a data structure that may or may not contain a
/// playlist.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Type)]
pub struct Playlist {
/// A unique identifier for the playlist.
///
/// This should remain the same if the playlist is renamed.
pub id: PlaylistId,
/// The name of the playlist, typically given by the user.
pub name: String,
/// The URI of an (optional) icon.
pub icon: Uri,
}
impl From<MaybePlaylist> for Option<Playlist> {
fn from(mp: MaybePlaylist) -> Self {
if mp.valid {
Some(mp.playlist)
} else {
None
}
}
}
impl<'a> From<Playlist> for Value<'a> {
fn from(p: Playlist) -> Self {
Value::from((p.id, p.name, p.icon))
}
}
/// A data structure describing a playlist, or nothing.
///
/// <details><summary>Rationale</summary>
///
/// D-Bus does not (at the time of writing) support a MAYBE type, so we are
/// forced to invent our own.
///
/// </details>
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Type)]
#[doc(alias = "Maybe_Playlist")]
pub struct MaybePlaylist {
/// Whether this structure refers to a valid playlist.
valid: bool,
/// The playlist, providing `valid` is true, otherwise undefined.
///
/// When constructing this type, it should be noted that the playlist ID
/// must be a valid object path, or D-Bus implementations may reject it.
/// This is true even when Valid is false. It is suggested that "/" is
/// used as the playlist ID in this case.
playlist: Playlist,
}
impl MaybePlaylist {
/// Construct a valid `MaybePlaylist` from the given playlist.
pub fn just(playlist: Playlist) -> Self {
Self {
valid: true,
playlist,
}
}
/// Construct a `MaybePlaylist` that contains invalid/no playlist.
///
/// The playlist ID will be set to "/", and the name and icon will be empty.
pub fn nothing() -> Self {
Self {
valid: false,
playlist: Playlist {
id: ObjectPath::from_static_str_unchecked("/").into(),
name: String::new(),
icon: Uri::new(),
},
}
}
/// Returns [Some] if this structure contains a valid playlist, otherwise
/// [None].
pub fn get(&self) -> Option<&Playlist> {
if self.valid {
Some(&self.playlist)
} else {
None
}
}
}
impl From<Playlist> for MaybePlaylist {
fn from(playlist: Playlist) -> Self {
Self::just(playlist)
}
}
impl From<Option<Playlist>> for MaybePlaylist {
fn from(opt: Option<Playlist>) -> Self {
match opt {
Some(playlist) => Self::just(playlist),
None => Self::nothing(),
}
}
}
impl<'a> From<MaybePlaylist> for Value<'a> {
fn from(mp: MaybePlaylist) -> Self {
Value::from((mp.valid, mp.playlist))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn valid_signatures() {
assert_eq!(Playlist::signature(), "(oss)");
assert_eq!(MaybePlaylist::signature(), "(b(oss))");
}
}