feat: music search filter/cont, refactored paginator

This commit is contained in:
ThetaDev 2022-10-31 23:05:27 +01:00
parent d29bbd8b25
commit dac2b17dc2
38 changed files with 65313 additions and 247 deletions

View file

@ -1,34 +1,88 @@
use super::{Channel, ChannelId, ChannelItem, ChannelTag, PlaylistItem, VideoItem, YouTubeItem};
use super::{
AlbumItem, ArtistItem, Channel, ChannelId, ChannelItem, ChannelTag, MusicItem,
MusicPlaylistItem, PlaylistItem, TrackItem, VideoItem, YouTubeItem,
};
impl TryFrom<YouTubeItem> for VideoItem {
type Error = ();
pub trait FromYtItem: Sized {
fn from_yt_item(_item: YouTubeItem) -> Option<Self> {
None
}
fn from_ytm_item(_item: MusicItem) -> Option<Self> {
None
}
}
fn try_from(value: YouTubeItem) -> Result<Self, Self::Error> {
match value {
YouTubeItem::Video(video) => Ok(video),
_ => Err(()),
impl FromYtItem for YouTubeItem {
fn from_yt_item(item: YouTubeItem) -> Option<Self> {
Some(item)
}
}
impl FromYtItem for VideoItem {
fn from_yt_item(item: YouTubeItem) -> Option<Self> {
match item {
YouTubeItem::Video(video) => Some(video),
_ => None,
}
}
}
impl TryFrom<YouTubeItem> for PlaylistItem {
type Error = ();
fn try_from(value: YouTubeItem) -> Result<Self, Self::Error> {
match value {
YouTubeItem::Playlist(playlist) => Ok(playlist),
_ => Err(()),
impl FromYtItem for PlaylistItem {
fn from_yt_item(item: YouTubeItem) -> Option<Self> {
match item {
YouTubeItem::Playlist(playlist) => Some(playlist),
_ => None,
}
}
}
impl TryFrom<YouTubeItem> for ChannelItem {
type Error = ();
impl FromYtItem for ChannelItem {
fn from_yt_item(item: YouTubeItem) -> Option<Self> {
match item {
YouTubeItem::Channel(channel) => Some(channel),
_ => None,
}
}
}
fn try_from(value: YouTubeItem) -> Result<Self, Self::Error> {
match value {
YouTubeItem::Channel(channel) => Ok(channel),
_ => Err(()),
impl FromYtItem for MusicItem {
fn from_ytm_item(item: MusicItem) -> Option<Self> {
Some(item)
}
}
impl FromYtItem for TrackItem {
fn from_ytm_item(item: MusicItem) -> Option<Self> {
match item {
MusicItem::Track(track) => Some(track),
_ => None,
}
}
}
impl FromYtItem for AlbumItem {
fn from_ytm_item(item: MusicItem) -> Option<Self> {
match item {
MusicItem::Album(album) => Some(album),
_ => None,
}
}
}
impl FromYtItem for ArtistItem {
fn from_ytm_item(item: MusicItem) -> Option<Self> {
match item {
MusicItem::Artist(artist) => Some(artist),
_ => None,
}
}
}
impl FromYtItem for MusicPlaylistItem {
fn from_ytm_item(item: MusicItem) -> Option<Self> {
match item {
MusicItem::Playlist(playlist) => Some(playlist),
_ => None,
}
}
}

View file

@ -5,6 +5,7 @@ mod ordering;
mod paginator;
pub mod richtext;
pub(crate) use convert::FromYtItem;
pub use paginator::Paginator;
use serde_with::serde_as;
@ -1036,4 +1037,33 @@ pub struct MusicSearchResult {
/// for the corrected search term and displays it on top of the
/// search results page.
pub corrected_query: Option<String>,
pub order: Vec<MusicEntityType>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum MusicItem {
Track(TrackItem),
Album(AlbumItem),
Artist(ArtistItem),
Playlist(MusicPlaylistItem),
}
#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum MusicEntityType {
Track,
Album,
Artist,
Playlist,
}
/// Filtered YouTube Music search result
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct MusicSearchFiltered<T> {
pub items: Paginator<T>,
/// Corrected search query
///
/// If the search term containes a typo, YouTube instead searches
/// for the corrected search term and displays it on top of the
/// search results page.
pub corrected_query: Option<String>,
}