use decky_api::{StorePluginQuery, StorePluginQuerySortColumn, StorePluginQuerySortDirection}; use super::IQueryHandler; pub struct StandardPostRetrievalSort; impl IQueryHandler for StandardPostRetrievalSort { fn filter(&self, query: &str, mut plugins: decky_api::StorePluginList) -> decky_api::StorePluginList { log::debug!("StandardPostRetrievalSort::filter got query string `{}`", query); let query: StorePluginQuery = match serde_urlencoded::from_str(query) { Ok(q) => q, Err(e) => { log::error!("Failed to parse query string {}: {}", query, e); Default::default() } }; match (query.sort_by, query.sort_direction) { (None, _) => plugins, (Some(StorePluginQuerySortColumn::Name), direction) => { plugins.sort_by_key(|p| p.name.to_lowercase()); match direction { None | Some(StorePluginQuerySortDirection::Ascending) => {}, Some(StorePluginQuerySortDirection::Descending) => plugins.reverse(), } plugins }, (Some(StorePluginQuerySortColumn::Date), direction) => { plugins.sort_by_key( |p| p.versions.iter() .max_by_key( |v| v.created.unwrap_or_default()) .map(|v| v.created.unwrap_or_default()) ); match direction { None | Some(StorePluginQuerySortDirection::Ascending) => {}, Some(StorePluginQuerySortDirection::Descending) => plugins.reverse(), } plugins }, (Some(StorePluginQuerySortColumn::Downloads), direction) => { plugins.sort_by_key(|p| p.versions.iter().map(|v| v.downloads.unwrap_or(0)).sum::()); match direction { None | Some(StorePluginQuerySortDirection::Ascending) => {}, Some(StorePluginQuerySortDirection::Descending) => plugins.reverse(), } plugins }, } } fn supported_params(&self, query: &str) -> String { match serde_urlencoded::from_str::(query) { Ok(q) => serde_urlencoded::to_string(&q).unwrap_or_default(), Err(_) => "".to_owned() } } fn descriptor(&self) -> String { "standard_post_retrieval".to_owned() } }