-
Notifications
You must be signed in to change notification settings - Fork 822
Expand file tree
/
Copy pathlib.rs
More file actions
70 lines (60 loc) · 1.57 KB
/
lib.rs
File metadata and controls
70 lines (60 loc) · 1.57 KB
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
#[macro_use]
extern crate log;
use protobuf::Message;
use std::future::Future;
use librespot_core::{Error, Session, SpotifyUri};
pub mod album;
pub mod artist;
pub mod audio;
pub mod availability;
pub mod content_rating;
pub mod copyright;
pub mod episode;
pub mod error;
pub mod external_id;
pub mod image;
pub mod lyrics;
pub mod playlist;
mod request;
pub mod restriction;
pub mod sale_period;
pub mod show;
pub mod track;
mod util;
pub mod video;
pub use error::MetadataError;
use request::RequestResult;
pub use album::Album;
pub use artist::Artist;
pub use episode::Episode;
pub use lyrics::Lyrics;
pub use playlist::Playlist;
pub use show::Show;
pub use track::Track;
pub trait Metadata: Send + Sized + 'static {
type Message: Message + std::fmt::Debug;
// Request a protobuf
fn request(
session: &Session,
id: &SpotifyUri,
) -> impl Future<Output = RequestResult> + Send + Sized;
// Request a metadata struct
fn get(
session: &Session,
id: &SpotifyUri,
) -> impl Future<Output = Result<Self, Error>> + Send + Sized {
map_request_to_message(Self::request(session, id), id)
}
fn parse(msg: &Self::Message, _: &SpotifyUri) -> Result<Self, Error>;
}
async fn map_request_to_message<M, P, F>(response: F, uri: &SpotifyUri) -> Result<M, Error>
where
P: Message + std::fmt::Debug,
M: Metadata<Message = P>,
F: Future<Output = RequestResult> + Send + Sized,
{
let response = response.await?;
let msg = P::parse_from_bytes(&response)?;
trace!("Received metadata: {msg:#?}");
M::parse(&msg, uri)
}