From 51761cbdefa39107b9e1f931f1aa8df6aebb2a94 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Mon, 28 Apr 2025 18:27:03 +0200 Subject: many much more generic refactor --- common/src/api.rs | 52 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 16 deletions(-) (limited to 'common/src/api.rs') diff --git a/common/src/api.rs b/common/src/api.rs index 6982e76..a58c445 100644 --- a/common/src/api.rs +++ b/common/src/api.rs @@ -3,11 +3,9 @@ which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2025 metamuffin */ -use crate::{user::NodeUserData, Node}; -#[cfg(feature = "rocket")] -use rocket::{FromForm, FromFormField, UriDisplayQuery}; +use crate::{user::NodeUserData, Node, NodeKind}; use serde::{Deserialize, Serialize}; -use std::sync::Arc; +use std::{collections::BTreeMap, sync::Arc, time::Duration}; type NodesWithUdata = Vec<(Arc, NodeUserData)>; @@ -23,6 +21,7 @@ pub struct ApiNodeResponse { pub struct ApiSearchResponse { pub count: usize, pub results: NodesWithUdata, + pub duration: Duration, } #[derive(Serialize, Deserialize)] @@ -38,6 +37,21 @@ pub struct ApiHomeResponse { pub categories: Vec<(String, NodesWithUdata)>, } +#[derive(Serialize, Deserialize)] +pub struct ApiStatsResponse { + pub kinds: BTreeMap, + pub total: StatsBin, +} + +#[derive(Default, Serialize, Deserialize)] +pub struct StatsBin { + pub runtime: f64, + pub size: u64, + pub count: usize, + pub max_runtime: (f64, String), + pub max_size: (u64, String), +} + #[derive(Debug, Default, Clone)] #[cfg_attr(feature = "rocket", derive(FromForm, UriDisplayQuery))] pub struct NodeFilterSort { @@ -46,24 +60,25 @@ pub struct NodeFilterSort { pub sort_order: Option, } -#[rustfmt::skip] -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -#[cfg_attr(feature = "rocket", derive(FromFormField, UriDisplayQuery))] -pub enum SortOrder { - #[field(value = "ascending")] Ascending, - #[field(value = "descending")] Descending, -} -macro_rules! form_enum { +pub trait UrlEnum: Sized { + fn to_str(&self) -> &'static str; + fn from_str(s: &str) -> Option; +} +macro_rules! url_enum { (enum $i:ident { $($vi:ident = $vk:literal),*, }) => { #[derive(Debug, Clone, Copy, PartialEq, Eq)] #[cfg_attr(feature = "rocket", derive(FromFormField, UriDisplayQuery))] pub enum $i { $(#[cfg_attr(feature = "rocket", field(value = $vk))] $vi),* } - impl $i { #[allow(unused)] const ALL: &'static [$i] = &[$($i::$vi),*]; } + impl $i { pub const ALL: &'static [$i] = &[$($i::$vi),*]; } + impl UrlEnum for $i { + fn to_str(&self) -> &'static str { match self { $(Self::$vi => $vk),* } } + fn from_str(s: &str) -> Option { match s { $($vk => Some(Self::$vi) ),*, _ => None } } + } }; } -form_enum!( +url_enum!( enum FilterProperty { FederationLocal = "fed_local", FederationRemote = "fed_remote", @@ -82,8 +97,7 @@ form_enum!( KindEpisode = "kind_episode", } ); - -form_enum!( +url_enum!( enum SortProperty { ReleaseDate = "release_date", Title = "title", @@ -100,3 +114,9 @@ form_enum!( RatingLikesDivViews = "rating_loved", } ); +url_enum!( + enum SortOrder { + Ascending = "ascending", + Descending = "descending", + } +); -- cgit v1.2.3-70-g09d2