src/config/search.rs
161 lines · 3705 bytes
1use crate::{config::ConfigTrait, global::traits::asurlstring::AsUrlString};
2use serde::{Deserialize, Serialize};
3use typemap::Key;
4
5// can be turned into URL Params for the search term with filters
6#[derive(Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
7pub struct Search {
8 pub query: String,
9 pub filters: SearchFilters,
10}
11
12impl Key for Search {
13 type Value = Self;
14}
15
16impl ConfigTrait for Search {
17 const LABEL: &'static str = "search";
18}
19
20impl ToString for Search {
21 fn to_string(&self) -> String {
22 vec![
23 format!("q={}", self.query),
24 self.filters.sort.as_url_string(),
25 self.filters.date.as_url_string(),
26 self.filters.duration.as_url_string(),
27 self.filters.r#type.as_url_string(),
28 ]
29 .join("&")
30 }
31}
32
33// Search filters
34
35#[derive(Default, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
36pub struct SearchFilters {
37 pub sort: SearchFilterSort,
38 pub date: SearchFilterDate,
39 pub duration: SearchFilterDuration,
40 pub r#type: SearchFilterType,
41}
42
43#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
44pub enum SearchFilterSort {
45 Relevance,
46 Rating,
47 Date,
48 Views,
49}
50
51#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
52pub enum SearchFilterDate {
53 None,
54 Hour,
55 Day,
56 Week,
57 Month,
58 Year,
59}
60
61#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
62pub enum SearchFilterDuration {
63 None,
64 Short,
65 Medium,
66 Long,
67}
68
69#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
70pub enum SearchFilterType {
71 All,
72 Video,
73 Channel,
74 Playlist,
75}
76
77impl Default for SearchFilterSort {
78 fn default() -> Self {
79 Self::Relevance
80 }
81}
82
83impl Default for SearchFilterDate {
84 fn default() -> Self {
85 Self::None
86 }
87}
88
89impl Default for SearchFilterDuration {
90 fn default() -> Self {
91 Self::None
92 }
93}
94
95impl Default for SearchFilterType {
96 fn default() -> Self {
97 Self::All
98 }
99}
100
101impl ToString for SearchFilterSort {
102 fn to_string(&self) -> String {
103 match self {
104 Self::Relevance => String::from("relevance"),
105 Self::Rating => String::from("rating"),
106 Self::Date => String::from("date"),
107 Self::Views => String::from("views"),
108 }
109 }
110}
111
112impl ToString for SearchFilterDate {
113 fn to_string(&self) -> String {
114 match self {
115 Self::None => String::from("none"),
116 Self::Hour => String::from("hour"),
117 Self::Day => String::from("day"),
118 Self::Week => String::from("week"),
119 Self::Month => String::from("month"),
120 Self::Year => String::from("year"),
121 }
122 }
123}
124
125impl ToString for SearchFilterDuration {
126 fn to_string(&self) -> String {
127 match self {
128 Self::None => String::from("none"),
129 Self::Short => String::from("short"),
130 Self::Medium => String::from("medium"),
131 Self::Long => String::from("long"),
132 }
133 }
134}
135
136impl ToString for SearchFilterType {
137 fn to_string(&self) -> String {
138 match self {
139 Self::All => String::from("all"),
140 Self::Video => String::from("video"),
141 Self::Channel => String::from("channel"),
142 Self::Playlist => String::from("playlist"),
143 }
144 }
145}
146
147impl AsUrlString for SearchFilterSort {
148 const TAG: &'static str = "sort";
149}
150
151impl AsUrlString for SearchFilterDate {
152 const TAG: &'static str = "date";
153}
154
155impl AsUrlString for SearchFilterDuration {
156 const TAG: &'static str = "duration";
157}
158
159impl AsUrlString for SearchFilterType {
160 const TAG: &'static str = "type";
161}