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
use std::{collections::HashMap, fmt};

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

use crate::TimeInUs;

/// Date/time fields should be sent as strings in ISO 8601 extended
/// format. If the timezone is known (eg: for xesam:lastPlayed), the
/// internet profile format of ISO 8601, as specified in RFC 3339,
///  should be used.
///
/// 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;

/// URIs should be sent as (UTF-8) strings. Local files should use the
/// "file://" schema.
pub type Uri = String;

#[derive(Clone, PartialEq, Serialize, Type)]
#[zvariant(signature = "a{sv}")]
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 {
    pub fn new() -> Self {
        Self(HashMap::new())
    }

    pub fn builder() -> MetadataBuilder {
        MetadataBuilder { m: Metadata::new() }
    }

    pub fn insert(
        &mut self,
        key: impl Into<String>,
        value: impl Into<Value<'static>>,
    ) -> Option<Value<'static>> {
        self.0.insert(key.into(), value.into())
    }

    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))
    }

    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).
    pub fn set_trackid(&mut self, trackid: OwnedObjectPath) {
        self.insert("mpris:trackid", trackid);
    }

    /// The duration of the track in microseconds.
    pub fn set_length(&mut self, length: TimeInUs) {
        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: Uri) {
        self.insert("mpris:artUrl", art_url);
    }

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

    /// The album artist(s).
    pub fn set_album_artist(&mut self, album_artist: Vec<String>) {
        self.insert("xesam:albumArtist", album_artist);
    }

    /// The track artist(s).
    pub fn set_artist(&mut self, artist: Vec<String>) {
        self.insert("xesam:artist", artist);
    }

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

    /// 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: Vec<String>) {
        self.insert("xesam:comment", comment);
    }

    /// The composer(s) of the track.
    pub fn set_composer(&mut self, composer: Vec<String>) {
        self.insert("xesam:composer", composer);
    }

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

    /// 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: DateTime) {
        self.insert("xesam:firstUsed", first_used);
    }

    /// The genre(s) of the track.
    pub fn set_genre(&mut self, genre: Vec<String>) {
        self.insert("xesam:genre", genre);
    }

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

    /// The lyricist(s) of the track.
    pub fn set_lyricist(&mut self, lyricist: Vec<String>) {
        self.insert("xesam:lyricist", lyricist);
    }

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

    /// 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: Uri) {
        self.insert("xesam:url", url);
    }

    /// 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);
    }
}

#[derive(Debug, Default, Clone)]
pub struct MetadataBuilder {
    m: Metadata,
}

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

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

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

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

    pub fn album_artist(mut self, album_artist: Vec<String>) -> Self {
        self.m.set_album_artist(album_artist);
        self
    }

    pub fn artist(mut self, artist: Vec<String>) -> Self {
        self.m.set_artist(artist);
        self
    }

    pub fn lyrics(mut self, lyrics: 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: Vec<String>) -> Self {
        self.m.set_comment(comment);
        self
    }

    pub fn composer(mut self, composer: Vec<String>) -> Self {
        self.m.set_composer(composer);
        self
    }

    pub fn content_created(mut self, content_created: 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: DateTime) -> Self {
        self.m.set_first_used(first_used);
        self
    }

    pub fn genre(mut self, genre: Vec<String>) -> Self {
        self.m.set_genre(genre);
        self
    }

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

    pub fn lyricist(mut self, lyricist: Vec<String>) -> Self {
        self.m.set_lyricist(lyricist);
        self
    }

    pub fn title(mut self, title: String) -> Self {
        self.m.set_title(title);
        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: Uri) -> Self {
        self.m.set_url(url);
        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)
    }
}