librespot_metadata/
image.rs

1use std::{
2    fmt::Debug,
3    ops::{Deref, DerefMut},
4};
5
6use crate::util::{impl_deref_wrapped, impl_from_repeated, impl_try_from_repeated};
7
8use librespot_core::{FileId, SpotifyId};
9
10use librespot_protocol as protocol;
11use protocol::metadata::Image as ImageMessage;
12use protocol::metadata::ImageGroup;
13pub use protocol::metadata::image::Size as ImageSize;
14use protocol::playlist_annotate3::TranscodedPicture as TranscodedPictureMessage;
15use protocol::playlist4_external::PictureSize as PictureSizeMessage;
16
17#[derive(Debug, Clone)]
18pub struct Image {
19    pub id: FileId,
20    pub size: ImageSize,
21    pub width: i32,
22    pub height: i32,
23}
24
25#[derive(Debug, Clone, Default)]
26pub struct Images(pub Vec<Image>);
27
28impl From<&ImageGroup> for Images {
29    fn from(image_group: &ImageGroup) -> Self {
30        Self(image_group.image.iter().map(|i| i.into()).collect())
31    }
32}
33
34impl_deref_wrapped!(Images, Vec<Image>);
35
36#[derive(Debug, Clone)]
37pub struct PictureSize {
38    pub target_name: String,
39    pub url: String,
40}
41
42#[derive(Debug, Clone, Default)]
43pub struct PictureSizes(pub Vec<PictureSize>);
44
45impl_deref_wrapped!(PictureSizes, Vec<PictureSize>);
46
47#[derive(Debug, Clone)]
48pub struct TranscodedPicture {
49    pub target_name: String,
50    pub uri: SpotifyId,
51}
52
53#[derive(Debug, Clone)]
54pub struct TranscodedPictures(pub Vec<TranscodedPicture>);
55
56impl_deref_wrapped!(TranscodedPictures, Vec<TranscodedPicture>);
57
58impl From<&ImageMessage> for Image {
59    fn from(image: &ImageMessage) -> Self {
60        Self {
61            id: image.into(),
62            size: image.size(),
63            width: image.width(),
64            height: image.height(),
65        }
66    }
67}
68
69impl_from_repeated!(ImageMessage, Images);
70
71impl From<&PictureSizeMessage> for PictureSize {
72    fn from(size: &PictureSizeMessage) -> Self {
73        Self {
74            target_name: size.target_name().to_owned(),
75            url: size.url().to_owned(),
76        }
77    }
78}
79
80impl_from_repeated!(PictureSizeMessage, PictureSizes);
81
82impl TryFrom<&TranscodedPictureMessage> for TranscodedPicture {
83    type Error = librespot_core::Error;
84    fn try_from(picture: &TranscodedPictureMessage) -> Result<Self, Self::Error> {
85        Ok(Self {
86            target_name: picture.target_name().to_owned(),
87            uri: picture.try_into()?,
88        })
89    }
90}
91
92impl_try_from_repeated!(TranscodedPictureMessage, TranscodedPictures);