use std::fmt::Display; use crate::global::{structs::Message, traits::*}; use serde::{Deserialize, Serialize}; use typemap::Key; use urlencoding::encode; // can be turned into URL Params for the search term with filters /// Search query & filters #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] pub struct Search { pub query: String, pub filters: SearchFilters, pub page: u16, } impl Default for Search { fn default() -> Self { Self { query: String::new(), filters: SearchFilters::default(), page: 1, } } } impl Key for Search { type Value = Self; } impl ConfigTrait for Search { const LABEL: &'static str = "search"; } impl Display for Search { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_fmt(format_args!( "{}&page={}", [ format!("q={}", encode(&self.query)), self.filters.sort.as_url_string(), self.filters.date.as_url_string(), self.filters.duration.as_url_string(), self.filters.r#type.as_url_string() ] .join("&"), self.page )) } } /// Search filters, read comments for function docs #[derive(Default, Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)] pub struct SearchFilters { pub sort: SearchFilterSort, pub date: SearchFilterDate, pub duration: SearchFilterDuration, pub r#type: SearchFilterType, } // These functions are used in the search filter item // Think of the ordering of stuff in the popup menu impl SearchFilters { // This function returns [(Option name, [Options])] // Option name is a string like "sory by" and "type" // Options are the options that you can select like "relevance" and "upload date" for sorting pub fn get_all() -> [(&'static str, Vec<&'static str>); 5] { [ (SearchFilterSort::NAME, SearchFilterSort::ordering()), (SearchFilterDate::NAME, SearchFilterDate::ordering()), (SearchFilterDuration::NAME, SearchFilterDuration::ordering()), (SearchFilterType::NAME, SearchFilterType::ordering()), ("Reset filters", vec!["Are you sure?"]), ] } // uses the index of the selected option name to get the index of the selected option // aka using the hover location of the left textlist to get the hover location of the right text list pub fn get_selected(&self, index: usize) -> usize { match index { 0 => self.sort.selected_index(), 1 => self.date.selected_index(), 2 => self.duration.selected_index(), 3 => self.r#type.selected_index(), 4 => 0, _ => unreachable!("`{index}` is not a valid index"), } } // saves the settings for a filter given the index of the option (among other options) and the index of the selected options // v left textlist v right textlist pub fn set_index(&mut self, index_at: usize, set_index: usize, message: &mut Message) { match index_at { 0 => self.sort = SearchFilterSort::at_index(set_index), 1 => self.date = SearchFilterDate::at_index(set_index), 2 => self.duration = SearchFilterDuration::at_index(set_index), 3 => self.r#type = SearchFilterType::at_index(set_index), 4 => { if set_index == 0 { self.reset(message) } else { unreachable!("index can only be `0` for option `4` (reset all)") } } _ => unreachable!("`{index_at}` is not a valid index"), } } pub fn reset(&mut self, message: &mut Message) { *self = Self::default(); *message = Message::Success(String::from("Search filters has been reset")); } } #[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)] #[derive(Default)] pub enum SearchFilterSort { #[default] Relevance, Rating, Date, Views, } #[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)] #[derive(Default)] pub enum SearchFilterDate { #[default] None, Hour, Day, Week, Month, Year, } #[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)] #[derive(Default)] pub enum SearchFilterDuration { #[default] None, Short, Medium, Long, } #[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)] #[derive(Default)] pub enum SearchFilterType { #[default] All, Video, Channel, Playlist, } impl Display for SearchFilterSort { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(match self { Self::Relevance => "relevance", Self::Rating => "rating", Self::Date => "date", Self::Views => "views", }) } } impl Display for SearchFilterDate { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(match self { Self::None => "none", Self::Hour => "hour", Self::Day => "day", Self::Week => "week", Self::Month => "month", Self::Year => "year", }) } } impl Display for SearchFilterDuration { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(match self { Self::None => "none", Self::Short => "short", Self::Medium => "medium", Self::Long => "long", }) } } impl Display for SearchFilterType { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(match self { Self::All => "all", Self::Video => "video", Self::Channel => "channel", Self::Playlist => "playlist", }) } } impl AsUrlString for SearchFilterSort { const TAG: &'static str = "sort"; } impl AsUrlString for SearchFilterDate { const TAG: &'static str = "date"; } impl AsUrlString for SearchFilterDuration { const TAG: &'static str = "duration"; } impl AsUrlString for SearchFilterType { const TAG: &'static str = "type"; } const SORT_ORDERING: [SearchFilterSort; 4] = [ SearchFilterSort::Relevance, SearchFilterSort::Rating, SearchFilterSort::Date, SearchFilterSort::Views, ]; const DATE_ORDERING: [SearchFilterDate; 6] = [ SearchFilterDate::None, SearchFilterDate::Hour, SearchFilterDate::Day, SearchFilterDate::Week, SearchFilterDate::Month, SearchFilterDate::Year, ]; const DURATION_ORDERING: [SearchFilterDuration; 4] = [ SearchFilterDuration::None, SearchFilterDuration::Short, SearchFilterDuration::Medium, SearchFilterDuration::Long, ]; const TYPE_ORDERING: [SearchFilterType; 4] = [ SearchFilterType::All, SearchFilterType::Video, SearchFilterType::Channel, SearchFilterType::Playlist, ]; impl SearchFilterItem for SearchFilterSort { const NAME: &'static str = "Sort by"; fn option_name(&self) -> &'static str { match self { Self::Relevance => "Relevance", Self::Rating => "Rating", Self::Date => "Upload date", Self::Views => "View count", } } fn ordering() -> Vec<&'static str> { SORT_ORDERING .iter() .map(|item| item.option_name()) .collect::>() } fn selected_index(&self) -> usize { match self { Self::Relevance => 0, Self::Rating => 1, Self::Date => 2, Self::Views => 3, } } fn at_index(index: usize) -> Self { SORT_ORDERING[index] } } impl SearchFilterItem for SearchFilterDate { const NAME: &'static str = "Upload date"; fn option_name(&self) -> &'static str { match self { Self::None => "Any date", Self::Hour => "Last hour", Self::Day => "Today", Self::Week => "This week", Self::Month => "This month", Self::Year => "This year", } } fn ordering() -> Vec<&'static str> { DATE_ORDERING .iter() .map(|item| item.option_name()) .collect::>() } fn selected_index(&self) -> usize { match self { Self::None => 0, Self::Hour => 1, Self::Day => 2, Self::Week => 3, Self::Month => 4, Self::Year => 5, } } fn at_index(index: usize) -> Self { DATE_ORDERING[index] } } impl SearchFilterItem for SearchFilterDuration { const NAME: &'static str = "Duration"; fn option_name(&self) -> &'static str { match self { Self::None => "Any duration", Self::Short => "Short (< 4 minutes)", Self::Medium => "Medium (4 - 20 minutes)", Self::Long => "Long (> 20 minutes)", } } fn ordering() -> Vec<&'static str> { DURATION_ORDERING .iter() .map(|item| item.option_name()) .collect::>() } fn selected_index(&self) -> usize { match self { Self::None => 0, Self::Short => 1, Self::Medium => 2, Self::Long => 3, } } fn at_index(index: usize) -> Self { DURATION_ORDERING[index] } } impl SearchFilterItem for SearchFilterType { const NAME: &'static str = "Type"; fn option_name(&self) -> &'static str { match self { Self::All => "Any type", Self::Video => "Video", Self::Channel => "Channel", Self::Playlist => "Playlist", } } fn ordering() -> Vec<&'static str> { TYPE_ORDERING .iter() .map(|item| item.option_name()) .collect::>() } fn selected_index(&self) -> usize { match self { Self::All => 0, Self::Video => 1, Self::Channel => 2, Self::Playlist => 3, } } fn at_index(index: usize) -> Self { TYPE_ORDERING[index] } }