src/global/traits/search_provider.rs
203 lines · 5711 bytes
1use std::{
2 collections::HashMap,
3 error::Error,
4 fmt::{Debug, Display},
5 sync::OnceLock,
6};
7
8use crate::global::common::{
9 channel::Channel,
10 hidden::{PopularItem, SearchItem},
11 universal::Playlist,
12 video::Video,
13 CommonPlaylist, CommonVideo,
14};
15use dyn_clone::DynClone;
16
17use crate::{
18 config::{Search, SearchProvider},
19 MAIN_CONFIG,
20};
21
22#[allow(unused_variables)]
23pub trait SearchProviderTrait: DynClone + Send {
24 fn supports_channel(&self) -> bool {
25 false
26 }
27 fn channel(&self, id: &str) -> Result<Channel, Box<dyn Error>> {
28 unimplemented!("channel not implemented")
29 }
30
31 fn supports_channel_videos(&self) -> bool {
32 false
33 }
34 fn channel_videos(&self, id: &str) -> Result<Vec<CommonVideo>, Box<dyn Error>> {
35 unimplemented!("channel_videos not implemented")
36 }
37
38 fn supports_channel_playlists(&self) -> bool {
39 false
40 }
41 fn channel_playlists(&self, id: &str) -> Result<Vec<CommonPlaylist>, Box<dyn Error>> {
42 unimplemented!("channel_playlists not implemented")
43 }
44
45 fn supports_trending(&self) -> bool {
46 false
47 }
48 fn trending(&self) -> Result<Vec<CommonVideo>, Box<dyn Error>> {
49 unimplemented!("trending not implemented")
50 }
51
52 fn supports_popular(&self) -> bool {
53 false
54 }
55 fn popular(&self) -> Result<Vec<PopularItem>, Box<dyn Error>> {
56 unimplemented!("popular not implemented")
57 }
58
59 fn supports_search(&self) -> bool {
60 false
61 }
62 fn search(&self, filters: &Search) -> Result<Vec<SearchItem>, Box<dyn Error>> {
63 unimplemented!("search not implemented")
64 }
65
66 fn supports_video(&self) -> bool {
67 false
68 }
69 fn video(&self, id: &str) -> Result<Video, Box<dyn Error>> {
70 unimplemented!("video not implemented")
71 }
72
73 fn supports_playlist(&self) -> bool {
74 false
75 }
76 fn playlist(&self, id: &str) -> Result<Playlist, Box<dyn Error>> {
77 unimplemented!("playlist not implemented")
78 }
79
80 /*
81 fn supports_album(&self) -> bool {
82 false
83 }
84 fn album(&self) -> Result {
85
86 }
87 */
88}
89
90dyn_clone::clone_trait_object!(SearchProviderTrait);
91
92#[derive(Default, Clone)]
93pub struct SearchProviderWrapper(HashMap<SearchProvider, Box<dyn SearchProviderTrait>>);
94
95struct UnsupportedError(pub &'static str);
96
97impl Display for UnsupportedError {
98 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
99 f.write_fmt(format_args!(
100 "Operation `{}` is not supported by {}",
101 self.0,
102 unsafe { MAIN_CONFIG.get().unwrap() }
103 .search_provider
104 .as_str()
105 ))
106 }
107}
108
109impl Debug for UnsupportedError {
110 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
111 f.write_fmt(format_args!("{self}"))
112 }
113}
114
115impl Error for UnsupportedError {}
116
117static mut SEARCH_PROVIDER: OnceLock<SearchProviderWrapper> = OnceLock::new();
118
119impl SearchProviderWrapper {
120 pub fn init() {
121 unsafe {
122 if SEARCH_PROVIDER.get().is_some() {
123 *SEARCH_PROVIDER.get_mut().unwrap() = Self::default()
124 } else {
125 let _ = SEARCH_PROVIDER.set(Self::default());
126 }
127 }
128 }
129
130 #[allow(clippy::borrowed_box)]
131 fn get() -> &'static Box<dyn SearchProviderTrait> {
132 let provider = &unsafe { MAIN_CONFIG.get() }.unwrap().search_provider;
133 unsafe { SEARCH_PROVIDER.get_mut() }
134 .unwrap()
135 .0
136 .entry(*provider)
137 .or_insert(provider.create())
138 }
139
140 pub fn channel(id: &str) -> Result<Channel, Box<dyn Error>> {
141 let provider = Self::get();
142 if !provider.supports_channel() {
143 return Err(UnsupportedError("channel").into());
144 }
145 provider.channel(id)
146 }
147
148 pub fn channel_videos(id: &str) -> Result<Vec<CommonVideo>, Box<dyn Error>> {
149 let provider = Self::get();
150 if !provider.supports_channel_videos() {
151 return Err(UnsupportedError("channel_videos").into());
152 }
153 provider.channel_videos(id)
154 }
155
156 pub fn channel_playlists(id: &str) -> Result<Vec<CommonPlaylist>, Box<dyn Error>> {
157 let provider = Self::get();
158 if !provider.supports_channel_playlists() {
159 return Err(UnsupportedError("channel_playlists").into());
160 }
161 provider.channel_playlists(id)
162 }
163
164 pub fn trending() -> Result<Vec<CommonVideo>, Box<dyn Error>> {
165 let provider = Self::get();
166 if !provider.supports_trending() {
167 return Err(UnsupportedError("trending").into());
168 }
169 provider.trending()
170 }
171
172 pub fn popular() -> Result<Vec<PopularItem>, Box<dyn Error>> {
173 let provider = Self::get();
174 if !provider.supports_popular() {
175 return Err(UnsupportedError("popular").into());
176 }
177 provider.popular()
178 }
179
180 pub fn search(filters: &Search) -> Result<Vec<SearchItem>, Box<dyn Error>> {
181 let provider = Self::get();
182 if !provider.supports_search() {
183 return Err(UnsupportedError("search").into());
184 }
185 provider.search(filters)
186 }
187
188 pub fn video(id: &str) -> Result<Video, Box<dyn Error>> {
189 let provider = Self::get();
190 if !provider.supports_video() {
191 return Err(UnsupportedError("video").into());
192 }
193 provider.video(id)
194 }
195
196 pub fn playlist(id: &str) -> Result<Playlist, Box<dyn Error>> {
197 let provider = Self::get();
198 if !provider.supports_playlist() {
199 return Err(UnsupportedError("playlist").into());
200 }
201 provider.playlist(id)
202 }
203}