use super::{ AlbumItem, ArtistId, ArtistItem, Channel, ChannelId, ChannelItem, ChannelRssVideo, ChannelTag, MusicArtist, MusicItem, MusicPlaylistItem, PlaylistItem, PlaylistVideo, TrackItem, VideoId, VideoItem, YouTubeItem, }; /// Trait for casting generic YouTube/YouTube music items to a specific kind. /// /// Returns [`None`] if the item does not match. pub trait FromYtItem: Sized { /// Casting from a generic YouTube item to a specific kind /// /// Returns [`None`] if the item does not match. fn from_yt_item(_item: YouTubeItem) -> Option { None } /// Casting from a generic YouTube Music item to a specific kind /// /// Returns [`None`] if the item does not match. fn from_ytm_item(_item: MusicItem) -> Option { None } } impl FromYtItem for YouTubeItem { fn from_yt_item(item: YouTubeItem) -> Option { Some(item) } } impl FromYtItem for VideoItem { fn from_yt_item(item: YouTubeItem) -> Option { match item { YouTubeItem::Video(video) => Some(video), _ => None, } } } impl FromYtItem for PlaylistItem { fn from_yt_item(item: YouTubeItem) -> Option { match item { YouTubeItem::Playlist(playlist) => Some(playlist), _ => None, } } } impl FromYtItem for ChannelItem { fn from_yt_item(item: YouTubeItem) -> Option { match item { YouTubeItem::Channel(channel) => Some(channel), _ => None, } } } impl FromYtItem for MusicItem { fn from_ytm_item(item: MusicItem) -> Option { Some(item) } } impl FromYtItem for TrackItem { fn from_ytm_item(item: MusicItem) -> Option { match item { MusicItem::Track(track) => Some(track), _ => None, } } } impl FromYtItem for AlbumItem { fn from_ytm_item(item: MusicItem) -> Option { match item { MusicItem::Album(album) => Some(album), _ => None, } } } impl FromYtItem for ArtistItem { fn from_ytm_item(item: MusicItem) -> Option { match item { MusicItem::Artist(artist) => Some(artist), _ => None, } } } impl FromYtItem for MusicPlaylistItem { fn from_ytm_item(item: MusicItem) -> Option { match item { MusicItem::Playlist(playlist) => Some(playlist), _ => None, } } } impl From> for ChannelTag { fn from(channel: Channel) -> Self { Self { id: channel.id, name: channel.name, avatar: channel.avatar, verification: channel.verification, subscriber_count: channel.subscriber_count, } } } impl From for ChannelId { fn from(channel: ChannelTag) -> Self { Self { id: channel.id, name: channel.name, } } } impl From> for ChannelId { fn from(channel: Channel) -> Self { Self { id: channel.id, name: channel.name, } } } impl From for ChannelId { fn from(artist: MusicArtist) -> Self { Self { id: artist.id, name: artist.name, } } } impl TryFrom for ChannelId { type Error = (); fn try_from(artist: ArtistId) -> Result { match artist.id { Some(id) => Ok(Self { id, name: artist.name, }), None => Err(()), } } } impl From for VideoId { fn from(video: VideoItem) -> Self { Self { id: video.id, name: video.name, } } } impl From for VideoId { fn from(video: PlaylistVideo) -> Self { Self { id: video.id, name: video.name, } } } impl From for VideoId { fn from(video: ChannelRssVideo) -> Self { Self { id: video.id, name: video.name, } } } impl From for VideoId { fn from(track: TrackItem) -> Self { Self { id: track.id, name: track.name, } } }