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
use serde::Serialize;
use zbus::zvariant::{ObjectPath, Type, Value};
use crate::{PlaylistId, Uri};
/// A data structure describing 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<'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(crate) 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 From<Option<Playlist>> for MaybePlaylist {
fn from(playlist: Option<Playlist>) -> Self {
match playlist {
Some(playlist) => Self {
valid: true,
playlist,
},
None => Self {
valid: false,
playlist: Playlist {
id: ObjectPath::from_static_str_unchecked("/").into(),
name: String::new(),
icon: Uri::new(),
},
},
}
}
}
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))");
}
}