aboutsummaryrefslogtreecommitdiff
path: root/common/src/api.rs
diff options
context:
space:
mode:
Diffstat (limited to 'common/src/api.rs')
-rw-r--r--common/src/api.rs52
1 files changed, 36 insertions, 16 deletions
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 <metamuffin.org>
*/
-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<Node>, 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<NodeKind, StatsBin>,
+ 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<SortOrder>,
}
-#[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<Self>;
+}
+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<Self> { 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",
+ }
+);