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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
use std::{collections::HashMap, fmt};

use serde::Serialize;
use zbus::zvariant::{self, OwnedObjectPath, Type, Value};

use crate::{Time, Uri};

/// Combined date and time.
///
/// This should be sent as strings in ISO 8601 extended
/// format (eg: 2007-04-29T14:35:51). If the timezone is known (eg: for
/// xesam:lastPlayed), the internet profile format of ISO 8601, as specified in
/// RFC 3339, should be used (eg: 2007-04-29T14:35:51+02:00).
///
/// For example: "2007-04-29T13:56+01:00" for 29th April 2007, four
/// minutes to 2pm, in a time zone 1 hour ahead of UTC.
pub type DateTime = String;

/// A mapping from metadata attribute names to values.
///
/// The [`mpris:trackid`] attribute must always be present.
///
/// If the length of the track is known, it should be provided in the metadata
/// property with the [`mpris:length`] key.
///
/// If there is an image associated with the track, a URL for it may be provided
/// using the [`mpris:artUrl`] key.
///
/// [`mpris:trackid`]: Metadata::set_trackid
/// [`mpris:length`]: Metadata::set_length
/// [`mpris:artUrl`]: Metadata::set_art_url
#[derive(Clone, PartialEq, Serialize, Type)]
#[serde(transparent)]
#[zvariant(signature = "a{sv}")]
#[doc(alias = "Metadata_Map")]
pub struct Metadata(HashMap<String, Value<'static>>);

impl fmt::Debug for Metadata {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&self.0, f)
    }
}

impl Default for Metadata {
    fn default() -> Self {
        Self::new()
    }
}

impl Metadata {
    /// Create an empty [`Metadata`].
    pub fn new() -> Self {
        Self(HashMap::new())
    }

    /// Creates a new builder-pattern struct instance to construct [`Metadata`].
    pub fn builder() -> MetadataBuilder {
        MetadataBuilder { m: Metadata::new() }
    }

    /// Insert a new key-value pair into the metadata.
    ///
    /// This will overwrite any existing value for the given key and will return
    /// the old value, if any.
    pub fn insert(
        &mut self,
        key: impl Into<String>,
        value: impl Into<Value<'static>>,
    ) -> Option<Value<'static>> {
        self.0.insert(key.into(), value.into())
    }

    /// Get the value of the given key and convert it to `V`, if it exists.
    pub fn get<'v, V>(&'v self, key: &str) -> Option<zvariant::Result<&'v V>>
    where
        &'v V: TryFrom<&'v Value<'v>>,
    {
        self.get_value(key)
            .map(|v| v.downcast_ref().ok_or(zvariant::Error::IncorrectType))
    }

    /// Get the value of the given key, if it exists.
    pub fn get_value(&self, key: &str) -> Option<&Value<'_>> {
        self.0.get(key)
    }

    /// A unique identity for this track within the context of an
    /// MPRIS object (eg: tracklist).
    ///
    /// This contains a D-Bus path that uniquely identifies the track
    /// within the scope of the playlist. There may or may not be an actual
    /// D-Bus object at that path; this specification says nothing about
    /// what interfaces such an object may implement.
    pub fn set_trackid(&mut self, trackid: impl Into<OwnedObjectPath>) {
        self.insert("mpris:trackid", trackid.into());
    }

    /// The duration of the track in microseconds.
    pub fn set_length(&mut self, length: Time) {
        self.insert("mpris:length", length);
    }

    /// The location of an image representing the track or album.
    ///
    /// Clients should not assume this will continue to exist when
    /// the media player stops giving out the URL.
    pub fn set_art_url(&mut self, art_url: impl Into<Uri>) {
        self.insert("mpris:artUrl", art_url.into());
    }

    /// The album name.
    pub fn set_album(&mut self, album: impl Into<String>) {
        self.insert("xesam:album", album.into());
    }

    /// The album artist(s).
    pub fn set_album_artist(&mut self, album_artist: impl IntoIterator<Item = impl Into<String>>) {
        self.insert(
            "xesam:albumArtist",
            album_artist
                .into_iter()
                .map(|i| i.into())
                .collect::<Vec<_>>(),
        );
    }

    /// The track artist(s).
    pub fn set_artist(&mut self, artist: impl IntoIterator<Item = impl Into<String>>) {
        self.insert(
            "xesam:artist",
            artist.into_iter().map(|i| i.into()).collect::<Vec<_>>(),
        );
    }

    /// The track lyrics.
    pub fn set_lyrics(&mut self, lyrics: impl Into<String>) {
        self.insert("xesam:asText", lyrics.into());
    }

    /// The speed of the music, in beats per minute.
    pub fn set_audio_bpm(&mut self, audio_bpm: i32) {
        self.insert("xesam:audioBPM", audio_bpm);
    }

    /// An automatically-generated rating, based on things such
    /// as how often it has been played. This should be in the
    /// range 0.0 to 1.0.
    pub fn set_auto_rating(&mut self, auto_rating: f64) {
        self.insert("xesam:autoRating", auto_rating);
    }

    /// A (list of) freeform comment(s).
    pub fn set_comment(&mut self, comment: impl IntoIterator<Item = impl Into<String>>) {
        self.insert(
            "xesam:comment",
            comment.into_iter().map(|i| i.into()).collect::<Vec<_>>(),
        );
    }

    /// The composer(s) of the track.
    pub fn set_composer(&mut self, composer: impl IntoIterator<Item = impl Into<String>>) {
        self.insert(
            "xesam:composer",
            composer.into_iter().map(|i| i.into()).collect::<Vec<_>>(),
        );
    }

    /// When the track was created. Usually only the year component
    /// will be useful.
    pub fn set_content_created(&mut self, content_created: impl Into<DateTime>) {
        self.insert("xesam:contentCreated", content_created.into());
    }

    /// The disc number on the album that this track is from.
    pub fn set_disc_number(&mut self, disc_number: i32) {
        self.insert("xesam:discNumber", disc_number);
    }

    /// When the track was first played.
    pub fn set_first_used(&mut self, first_used: impl Into<DateTime>) {
        self.insert("xesam:firstUsed", first_used.into());
    }

    /// The genre(s) of the track.
    pub fn set_genre(&mut self, genre: impl IntoIterator<Item = impl Into<String>>) {
        self.insert(
            "xesam:genre",
            genre.into_iter().map(|i| i.into()).collect::<Vec<_>>(),
        );
    }

    /// When the track was last played.
    pub fn set_last_used(&mut self, last_used: impl Into<DateTime>) {
        self.insert("xesam:lastUsed", last_used.into());
    }

    /// The lyricist(s) of the track.
    pub fn set_lyricist(&mut self, lyricist: impl IntoIterator<Item = impl Into<String>>) {
        self.insert(
            "xesam:lyricist",
            lyricist.into_iter().map(|i| i.into()).collect::<Vec<_>>(),
        );
    }

    /// The track title.
    pub fn set_title(&mut self, title: impl Into<String>) {
        self.insert("xesam:title", title.into());
    }

    /// The track number on the album disc.
    pub fn set_track_number(&mut self, track_number: i32) {
        self.insert("xesam:trackNumber", track_number);
    }

    /// The location of the media file.
    pub fn set_url(&mut self, url: impl Into<Uri>) {
        self.insert("xesam:url", url.into());
    }

    /// The number of times the track has been played.
    pub fn set_use_count(&mut self, use_count: i32) {
        self.insert("xesam:useCount", use_count);
    }

    /// A user-specified rating. This should be in the range 0.0 to 1.0.
    pub fn set_user_rating(&mut self, user_rating: f64) {
        self.insert("xesam:userRating", user_rating);
    }
}

/// A builder used to create [`Metadata`].
#[derive(Debug, Default, Clone)]
pub struct MetadataBuilder {
    m: Metadata,
}

impl MetadataBuilder {
    /// Insert a new key-value pair into the metadata.
    pub fn other(mut self, key: impl Into<String>, value: impl Into<Value<'static>>) -> Self {
        self.m.insert(key, value);
        self
    }

    pub fn trackid(mut self, trackid: impl Into<OwnedObjectPath>) -> Self {
        self.m.set_trackid(trackid);
        self
    }

    pub fn length(mut self, length: Time) -> Self {
        self.m.set_length(length);
        self
    }

    pub fn art_url(mut self, art_url: impl Into<Uri>) -> Self {
        self.m.set_art_url(art_url);
        self
    }

    pub fn album(mut self, album: impl Into<String>) -> Self {
        self.m.set_album(album);
        self
    }

    pub fn album_artist(
        mut self,
        album_artist: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.m.set_album_artist(album_artist);
        self
    }

    pub fn artist(mut self, artist: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.m.set_artist(artist);
        self
    }

    pub fn lyrics(mut self, lyrics: impl Into<String>) -> Self {
        self.m.set_lyrics(lyrics);
        self
    }

    pub fn audio_bpm(mut self, audio_bpm: i32) -> Self {
        self.m.set_audio_bpm(audio_bpm);
        self
    }

    pub fn auto_rating(mut self, auto_rating: f64) -> Self {
        self.m.set_auto_rating(auto_rating);
        self
    }

    pub fn comment(mut self, comment: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.m.set_comment(comment);
        self
    }

    pub fn composer(mut self, composer: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.m.set_composer(composer);
        self
    }

    pub fn content_created(mut self, content_created: impl Into<DateTime>) -> Self {
        self.m.set_content_created(content_created);
        self
    }

    pub fn disc_number(mut self, disc_number: i32) -> Self {
        self.m.set_disc_number(disc_number);
        self
    }

    pub fn first_used(mut self, first_used: impl Into<DateTime>) -> Self {
        self.m.set_first_used(first_used);
        self
    }

    pub fn genre(mut self, genre: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.m.set_genre(genre);
        self
    }

    pub fn last_used(mut self, last_used: impl Into<DateTime>) -> Self {
        self.m.set_last_used(last_used);
        self
    }

    pub fn lyricist(mut self, lyricist: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.m.set_lyricist(lyricist);
        self
    }

    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.m.set_title(title.into());
        self
    }

    pub fn track_number(mut self, track_number: i32) -> Self {
        self.m.set_track_number(track_number);
        self
    }

    pub fn url(mut self, url: impl Into<Uri>) -> Self {
        self.m.set_url(url.into());
        self
    }

    pub fn use_count(mut self, use_count: i32) -> Self {
        self.m.set_use_count(use_count);
        self
    }

    pub fn user_rating(mut self, user_rating: f64) -> Self {
        self.m.set_user_rating(user_rating);
        self
    }

    pub fn build(self) -> Metadata {
        self.m
    }
}

impl<'a> From<Metadata> for Value<'a> {
    fn from(metainfo: Metadata) -> Self {
        Value::new(metainfo.0)
    }
}