not-decky-store/src/storage/standard_sort.rs

62 lines
2.5 KiB
Rust
Raw Normal View History

2023-12-31 02:44:10 +00:00
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::<u64>());
match direction {
None | Some(StorePluginQuerySortDirection::Ascending) => {},
Some(StorePluginQuerySortDirection::Descending) => plugins.reverse(),
}
plugins
},
}
}
fn supported_params(&self, query: &str) -> String {
match serde_urlencoded::from_str::<StorePluginQuery>(query) {
Ok(q) => serde_urlencoded::to_string(&q).unwrap_or_default(),
Err(_) => "".to_owned()
}
}
2024-05-11 05:18:42 +01:00
fn descriptor(&self) -> String {
"standard_post_retrieval".to_owned()
}
2023-12-31 02:44:10 +00:00
}