yamuse 0.3.2

typed async client for the yandex music api: catalogue, likes, playlists, radio, lossless downloads and ynison realtime
Documentation
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! typed parameter enums.
//!
//! the api takes these as bare strings, and a mistyped one comes back as a
//! puzzling 400 rather than anything that names the problem. as real enums, a
//! typo is a compile error instead.

use std::fmt;

use serde::{Deserialize, Serialize};

/// render an enum as the wire string the api expects.
macro_rules! wire_display {
    ($name:ident) => {
        impl fmt::Display for $name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str(self.as_str())
            }
        }
    };
}

/// response language.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Language {
    /// russian — the api default.
    #[default]
    Ru,
    /// english.
    En,
    /// ukrainian.
    Uk,
    /// uzbek.
    Uz,
    /// us english.
    Us,
    /// kazakh.
    Kk,
    /// armenian.
    Hy,
}

impl Language {
    /// the wire value.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Ru => "ru",
            Self::En => "en",
            Self::Uk => "uk",
            Self::Uz => "uz",
            Self::Us => "us",
            Self::Kk => "kk",
            Self::Hy => "hy",
        }
    }
}
wire_display!(Language);

/// entity type to search for.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SearchType {
    /// every entity type.
    #[default]
    All,
    /// artists only.
    Artist,
    /// users only.
    User,
    /// albums only.
    Album,
    /// playlists only.
    Playlist,
    /// tracks only.
    Track,
    /// podcasts only.
    Podcast,
    /// podcast episodes only.
    PodcastEpisode,
}

impl SearchType {
    /// the wire value.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::All => "all",
            Self::Artist => "artist",
            Self::User => "user",
            Self::Album => "album",
            Self::Playlist => "playlist",
            Self::Track => "track",
            Self::Podcast => "podcast",
            Self::PodcastEpisode => "podcast_episode",
        }
    }
}
wire_display!(SearchType);

/// lyrics format.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum LyricsFormat {
    /// plain text.
    #[default]
    #[serde(rename = "TEXT")]
    Text,
    /// time-synced lrc.
    #[serde(rename = "LRC")]
    Lrc,
}

impl LyricsFormat {
    /// the wire value.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Text => "TEXT",
            Self::Lrc => "LRC",
        }
    }
}
wire_display!(LyricsFormat);

/// visibility of a playlist.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Visibility {
    /// visible to everyone.
    #[default]
    Public,
    /// visible only to the owner.
    Private,
}

impl Visibility {
    /// the wire value.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Public => "public",
            Self::Private => "private",
        }
    }
}
wire_display!(Visibility);

/// how a listing is sorted.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SortBy {
    /// by release year.
    #[default]
    Year,
    /// by rating.
    Rating,
}

impl SortBy {
    /// the wire value.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Year => "year",
            Self::Rating => "rating",
        }
    }
}
wire_display!(SortBy);

/// the direction of a sort.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SortOrder {
    /// ascending.
    Asc,
    /// descending.
    #[default]
    Desc,
}

impl SortOrder {
    /// the wire value.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Asc => "asc",
            Self::Desc => "desc",
        }
    }
}
wire_display!(SortOrder);

/// entity type that can be liked.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Likeable {
    /// a track.
    Track,
    /// an artist.
    Artist,
    /// an album.
    Album,
    /// a playlist, identified as `ownerId:kind`.
    Playlist,
}

impl Likeable {
    /// the wire value, singular.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Track => "track",
            Self::Artist => "artist",
            Self::Album => "album",
            Self::Playlist => "playlist",
        }
    }
}
wire_display!(Likeable);

/// entity type that can be disliked.
///
/// narrower than [`Likeable`] on purpose: the api only accepts dislikes for
/// tracks and artists.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Dislikeable {
    /// a track.
    Track,
    /// an artist.
    Artist,
}

impl Dislikeable {
    /// the wire value, singular.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Track => "track",
            Self::Artist => "artist",
        }
    }
}
wire_display!(Dislikeable);

/// kind of feedback sent to the rotor while listening to a station.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RotorFeedback {
    /// the radio session started.
    RadioStarted,
    /// a track started playing.
    TrackStarted,
    /// a track played to the end.
    TrackFinished,
    /// a track was skipped.
    Skip,
}

impl RotorFeedback {
    /// the wire value.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::RadioStarted => "radioStarted",
            Self::TrackStarted => "trackStarted",
            Self::TrackFinished => "trackFinished",
            Self::Skip => "skip",
        }
    }
}
wire_display!(RotorFeedback);

/// mood and energy preset for a station.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum MoodEnergy {
    /// no preference.
    #[default]
    All,
    /// upbeat.
    Fun,
    /// energetic.
    Active,
    /// calm.
    Calm,
    /// melancholic.
    Sad,
}

impl MoodEnergy {
    /// the wire value.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::All => "all",
            Self::Fun => "fun",
            Self::Active => "active",
            Self::Calm => "calm",
            Self::Sad => "sad",
        }
    }
}
wire_display!(MoodEnergy);

/// track-diversity preset for a station.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum Diversity {
    /// the api default mix.
    #[default]
    Default,
    /// favour tracks the listener already likes.
    Favorite,
    /// favour popular tracks.
    Popular,
    /// favour unfamiliar tracks.
    Discover,
}

impl Diversity {
    /// the wire value.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Default => "default",
            Self::Favorite => "favorite",
            Self::Popular => "popular",
            Self::Discover => "discover",
        }
    }
}
wire_display!(Diversity);

/// language preset for a station.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum StationLanguage {
    /// any language.
    Any,
    /// russian only.
    Russian,
    /// everything but russian — the api's own default for this parameter.
    #[default]
    NotRussian,
}

impl StationLanguage {
    /// the wire value.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Any => "any",
            Self::Russian => "russian",
            Self::NotRussian => "not-russian",
        }
    }
}
wire_display!(StationLanguage);

/// station type.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum StationType {
    /// a regular rotor station.
    #[default]
    Rotor,
    /// a generative station.
    Generative,
}

impl StationType {
    /// the wire value.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Rotor => "rotor",
            Self::Generative => "generative",
        }
    }
}
wire_display!(StationType);

/// object kind addressable through the batch "list" endpoints.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum BatchObject {
    Track,
    Album,
    Artist,
    Playlist,
}

impl BatchObject {
    pub(crate) fn as_str(self) -> &'static str {
        match self {
            Self::Track => "track",
            Self::Album => "album",
            Self::Artist => "artist",
            Self::Playlist => "playlist",
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn wire_values_match_what_the_api_expects() {
        assert_eq!(SearchType::PodcastEpisode.as_str(), "podcast_episode");
        assert_eq!(SearchType::All.as_str(), "all");
        assert_eq!(LyricsFormat::Lrc.as_str(), "LRC");
        assert_eq!(LyricsFormat::Text.as_str(), "TEXT");
        assert_eq!(RotorFeedback::TrackFinished.as_str(), "trackFinished");
        assert_eq!(RotorFeedback::RadioStarted.as_str(), "radioStarted");
        assert_eq!(StationLanguage::NotRussian.as_str(), "not-russian");
        assert_eq!(Visibility::Private.as_str(), "private");
        assert_eq!(Language::Ru.as_str(), "ru");
        assert_eq!(SortBy::Year.as_str(), "year");
        assert_eq!(SortOrder::Desc.as_str(), "desc");
        assert_eq!(Likeable::Playlist.as_str(), "playlist");
        assert_eq!(Dislikeable::Artist.as_str(), "artist");
        assert_eq!(MoodEnergy::All.as_str(), "all");
        assert_eq!(Diversity::Default.as_str(), "default");
        assert_eq!(StationType::Generative.as_str(), "generative");
        assert_eq!(BatchObject::Playlist.as_str(), "playlist");
    }

    #[test]
    fn display_renders_the_wire_value() {
        assert_eq!(SearchType::Track.to_string(), "track");
        assert_eq!(StationLanguage::NotRussian.to_string(), "not-russian");
        assert_eq!(LyricsFormat::Lrc.to_string(), "LRC");
    }

    #[test]
    fn defaults_match_the_api_defaults() {
        assert_eq!(Language::default(), Language::Ru);
        assert_eq!(SearchType::default(), SearchType::All);
        assert_eq!(LyricsFormat::default(), LyricsFormat::Text);
        assert_eq!(Visibility::default(), Visibility::Public);
        assert_eq!(SortBy::default(), SortBy::Year);
        assert_eq!(SortOrder::default(), SortOrder::Desc);
        assert_eq!(MoodEnergy::default(), MoodEnergy::All);
        assert_eq!(Diversity::default(), Diversity::Default);
        assert_eq!(StationLanguage::default(), StationLanguage::NotRussian);
        assert_eq!(StationType::default(), StationType::Rotor);
    }
}