|
1 | 1 | #[macro_use] |
2 | 2 | extern crate log; |
3 | 3 |
|
4 | | -#[macro_use] |
5 | | -extern crate async_trait; |
6 | | - |
7 | 4 | use protobuf::Message; |
| 5 | +use std::future::Future; |
8 | 6 |
|
9 | 7 | use librespot_core::{Error, Session, SpotifyUri}; |
10 | 8 |
|
@@ -39,20 +37,34 @@ pub use playlist::Playlist; |
39 | 37 | pub use show::Show; |
40 | 38 | pub use track::Track; |
41 | 39 |
|
42 | | -#[async_trait] |
43 | 40 | pub trait Metadata: Send + Sized + 'static { |
44 | | - type Message: protobuf::Message + std::fmt::Debug; |
| 41 | + type Message: Message + std::fmt::Debug; |
45 | 42 |
|
46 | 43 | // Request a protobuf |
47 | | - async fn request(session: &Session, id: &SpotifyUri) -> RequestResult; |
| 44 | + fn request( |
| 45 | + session: &Session, |
| 46 | + id: &SpotifyUri, |
| 47 | + ) -> impl Future<Output = RequestResult> + Send + Sized; |
48 | 48 |
|
49 | 49 | // Request a metadata struct |
50 | | - async fn get(session: &Session, id: &SpotifyUri) -> Result<Self, Error> { |
51 | | - let response = Self::request(session, id).await?; |
52 | | - let msg = Self::Message::parse_from_bytes(&response)?; |
53 | | - trace!("Received metadata: {msg:#?}"); |
54 | | - Self::parse(&msg, id) |
| 50 | + fn get( |
| 51 | + session: &Session, |
| 52 | + id: &SpotifyUri, |
| 53 | + ) -> impl Future<Output = Result<Self, Error>> + Send + Sized { |
| 54 | + map_request_to_message(Self::request(session, id), id) |
55 | 55 | } |
56 | 56 |
|
57 | 57 | fn parse(msg: &Self::Message, _: &SpotifyUri) -> Result<Self, Error>; |
58 | 58 | } |
| 59 | + |
| 60 | +async fn map_request_to_message<M, P, F>(response: F, uri: &SpotifyUri) -> Result<M, Error> |
| 61 | +where |
| 62 | + P: Message + std::fmt::Debug, |
| 63 | + M: Metadata<Message = P>, |
| 64 | + F: Future<Output = RequestResult> + Send + Sized, |
| 65 | +{ |
| 66 | + let response = response.await?; |
| 67 | + let msg = P::parse_from_bytes(&response)?; |
| 68 | + trace!("Received metadata: {msg:#?}"); |
| 69 | + M::parse(&msg, uri) |
| 70 | +} |
0 commit comments