From 114a86a3823a175875aa2aeb31a61a6799ef13bc Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Thu, 8 Aug 2024 03:22:04 +0200 Subject: [PATCH] feat: add YtEntity trait to YouTubeItem and MusicItem --- src/model/traits.rs | 116 +++++++++++++++++++++++++++++--------------- 1 file changed, 77 insertions(+), 39 deletions(-) diff --git a/src/model/traits.rs b/src/model/traits.rs index 950de5d..30451a8 100644 --- a/src/model/traits.rs +++ b/src/model/traits.rs @@ -227,50 +227,16 @@ macro_rules! yt_entity_owner_music { } fn channel_name(&self) -> Option<&str> { - self.artists.first().map(|a| a.name.as_str()) + if self.by_va { + Some(crate::util::VARIOUS_ARTISTS) + } else { + self.artists.first().map(|a| a.name.as_str()) + } } } }; } -/* -impl YtEntity for VideoPlayer { - fn id(&self) -> &str { - &self.details.id - } - - fn name(&self) -> &str { - &self.details.name - } - - fn channel_id(&self) -> Option<&str> { - Some(&self.details.channel_id) - } - - fn channel_name(&self) -> Option<&str> { - self.details.channel_name.as_deref() - } -} - -impl YtEntity for VideoPlayerDetails { - fn id(&self) -> &str { - &self.channel_id - } - - fn name(&self) -> &str { - &self.name - } - - fn channel_id(&self) -> Option<&str> { - Some(&self.channel_id) - } - - fn channel_name(&self) -> Option<&str> { - self.channel_name.as_deref() - } -} -*/ - impl YtEntity for Channel { fn id(&self) -> &str { &self.id @@ -289,6 +255,78 @@ impl YtEntity for Channel { } } +impl YtEntity for YouTubeItem { + fn id(&self) -> &str { + match self { + YouTubeItem::Video(v) => &v.id, + YouTubeItem::Playlist(p) => &p.id, + YouTubeItem::Channel(c) => &c.id, + } + } + + fn name(&self) -> &str { + match self { + YouTubeItem::Video(v) => &v.name, + YouTubeItem::Playlist(p) => &p.name, + YouTubeItem::Channel(c) => &c.name, + } + } + + fn channel_id(&self) -> Option<&str> { + match self { + YouTubeItem::Video(v) => v.channel_id(), + YouTubeItem::Playlist(p) => p.channel_id(), + YouTubeItem::Channel(_) => None, + } + } + + fn channel_name(&self) -> Option<&str> { + match self { + YouTubeItem::Video(v) => v.channel_name(), + YouTubeItem::Playlist(p) => p.channel_name(), + YouTubeItem::Channel(_) => None, + } + } +} + +impl YtEntity for MusicItem { + fn id(&self) -> &str { + match self { + MusicItem::Track(t) => &t.id, + MusicItem::Album(b) => &b.id, + MusicItem::Artist(a) => &a.id, + MusicItem::Playlist(p) => &p.id, + } + } + + fn name(&self) -> &str { + match self { + MusicItem::Track(t) => &t.name, + MusicItem::Album(b) => &b.name, + MusicItem::Artist(a) => &a.name, + MusicItem::Playlist(p) => &p.name, + } + } + + fn channel_id(&self) -> Option<&str> { + match self { + MusicItem::Track(t) => t.channel_id(), + MusicItem::Album(b) => b.channel_id(), + MusicItem::Artist(_) => None, + MusicItem::Playlist(p) => p.channel_id(), + } + } + + fn channel_name(&self) -> Option<&str> { + match self { + MusicItem::Track(t) => t.channel_name(), + MusicItem::Album(b) => b.channel_name(), + MusicItem::Artist(_) => None, + MusicItem::Playlist(p) => p.channel_id(), + } + } +} + yt_entity_owner_opt! {Playlist} yt_entity! {ChannelId} yt_entity_owner! {VideoDetails}