invidious/structs/global/
methods.rs1#![allow(unreachable_code)]
2#![allow(unreachable_patterns)]
3
4use std::{future::Future, pin::Pin};
5
6pub type MethodSyncCustom = Box<dyn Fn(&str) -> MethodReturn>;
8pub type AsyncMethodCustom =
10 Box<dyn Fn(String) -> Pin<Box<dyn Future<Output = MethodReturn> + Send>> + Send + Sync>;
11pub type MethodReturn = Result<String, Box<dyn std::error::Error>>;
13
14#[cfg(feature = "sync")]
30#[derive(Clone, Copy)]
31pub enum MethodSync {
32 #[cfg(feature = "httpreq_sync")]
34 HttpReq,
35 #[cfg(feature = "minreq_sync")]
37 MinReq,
38 #[cfg(feature = "ureq_sync")]
40 Ureq,
41 #[cfg(feature = "reqwest_sync")]
43 Reqwest,
44 #[cfg(feature = "isahc_sync")]
46 Isahc,
47 #[cfg(feature = "minreq_http_sync")]
49 MinReqHttp,
50}
51
52#[cfg(feature = "sync")]
53impl Default for MethodSync {
54 fn default() -> Self {
64 #[cfg(feature = "httpreq_sync")]
65 return MethodSync::HttpReq;
66 #[cfg(feature = "ureq_sync")]
67 return MethodSync::Ureq;
68 #[cfg(feature = "minreq_sync")]
69 return MethodSync::MinReq;
70 #[cfg(feature = "reqwest_sync")]
71 return MethodSync::Reqwest;
72 #[cfg(feature = "isahc_sync")]
73 return MethodSync::Isahc;
74 #[cfg(feature = "minreq_http_sync")]
75 return MethodSync::MinReqHttp;
76 panic!("No method selected");
77 }
78}
79
80#[cfg(feature = "sync")]
81impl MethodSync {
82 pub fn fetch(&self, url: &str) -> Result<String, Box<dyn std::error::Error>> {
84 Ok(match self {
85 #[cfg(feature = "reqwest_sync")]
86 MethodSync::Reqwest => reqwest::blocking::get(url)?.text()?,
87 #[cfg(feature = "ureq_sync")]
88 MethodSync::Ureq => ureq::get(url).call()?.into_string()?,
89 #[cfg(feature = "httpreq_sync")]
90 MethodSync::HttpReq => String::from_utf8(crate::functions::httpreq_get(url)?)?,
91 #[cfg(feature = "minreq_http_sync")]
92 MethodSync::MinReqHttp => String::from_utf8(minreq::get(url).send()?.into_bytes())?,
93 #[cfg(feature = "minreq_sync")]
94 MethodSync::MinReq => String::from_utf8(minreq::get(url).send()?.into_bytes())?,
95 #[cfg(feature = "isahc_sync")]
96 MethodSync::Isahc => {
97 use isahc::ReadResponseExt;
98 isahc::get(url)?.text()?
99 }
100 _ => panic!("No method selected"),
101 })
102 }
103}
104
105#[cfg(feature = "async")]
117#[derive(Clone, Copy)]
118pub enum MethodAsync {
119 #[cfg(feature = "reqwest_async")]
121 Reqwest,
122 #[cfg(feature = "isahc_async")]
124 Isahc,
125}
126
127#[cfg(feature = "async")]
128impl Default for MethodAsync {
129 fn default() -> Self {
135 #[cfg(feature = "reqwest_async")]
136 return MethodAsync::Reqwest;
137 #[cfg(feature = "isahc_async")]
138 return MethodAsync::Isahc;
139 panic!("No method selected");
140 }
141}
142
143#[cfg(feature = "async")]
144impl MethodAsync {
145 pub async fn fetch(&self, url: &str) -> Result<String, Box<dyn std::error::Error>> {
147 Ok(match self {
148 #[cfg(feature = "reqwest_async")]
149 MethodAsync::Reqwest => reqwest::get(url).await?.text().await?,
150 #[cfg(feature = "isahc_async")]
151 MethodAsync::Isahc => {
152 use isahc::AsyncReadResponseExt;
153 isahc::get_async(url).await?.text().await?
154 }
155 })
156 }
157}