#![allow(unreachable_code)]
#![allow(unreachable_patterns)]
use std::{future::Future, pin::Pin};
pub type MethodSyncCustom = Box<dyn Fn(&str) -> MethodReturn>;
pub type AsyncMethodCustom =
Box<dyn Fn(String) -> Pin<Box<dyn Future<Output = MethodReturn> + Send>> + Send + Sync>;
pub type MethodReturn = Result<String, Box<dyn std::error::Error>>;
#[cfg(feature = "sync")]
#[derive(Clone, Copy)]
pub enum MethodSync {
#[cfg(feature = "httpreq_sync")]
HttpReq,
#[cfg(feature = "minreq_sync")]
MinReq,
#[cfg(feature = "ureq_sync")]
Ureq,
#[cfg(feature = "reqwest_sync")]
Reqwest,
#[cfg(feature = "isahc_sync")]
Isahc,
#[cfg(feature = "minreq_http_sync")]
MinReqHttp,
}
#[cfg(feature = "sync")]
impl Default for MethodSync {
fn default() -> Self {
#[cfg(feature = "httpreq_sync")]
return MethodSync::HttpReq;
#[cfg(feature = "ureq_sync")]
return MethodSync::Ureq;
#[cfg(feature = "minreq_sync")]
return MethodSync::MinReq;
#[cfg(feature = "reqwest_sync")]
return MethodSync::Reqwest;
#[cfg(feature = "isahc_sync")]
return MethodSync::Isahc;
#[cfg(feature = "minreq_http_sync")]
return MethodSync::MinReqHttp;
panic!("No method selected");
}
}
#[cfg(feature = "sync")]
impl MethodSync {
pub fn fetch(&self, url: &str) -> Result<String, Box<dyn std::error::Error>> {
Ok(match self {
#[cfg(feature = "reqwest_sync")]
MethodSync::Reqwest => reqwest::blocking::get(url)?.text()?,
#[cfg(feature = "ureq_sync")]
MethodSync::Ureq => ureq::get(url).call()?.into_string()?,
#[cfg(feature = "httpreq_sync")]
MethodSync::HttpReq => String::from_utf8(crate::functions::httpreq_get(url)?)?,
#[cfg(feature = "minreq_http_sync")]
MethodSync::MinReqHttp => String::from_utf8(minreq::get(url).send()?.into_bytes())?,
#[cfg(feature = "minreq_sync")]
MethodSync::MinReq => String::from_utf8(minreq::get(url).send()?.into_bytes())?,
#[cfg(feature = "isahc_sync")]
MethodSync::Isahc => {
use isahc::ReadResponseExt;
isahc::get(url)?.text()?
}
_ => panic!("No method selected"),
})
}
}
#[cfg(feature = "async")]
#[derive(Clone, Copy)]
pub enum MethodAsync {
#[cfg(feature = "reqwest_async")]
Reqwest,
#[cfg(feature = "isahc_async")]
Isahc,
}
#[cfg(feature = "async")]
impl Default for MethodAsync {
fn default() -> Self {
#[cfg(feature = "reqwest_async")]
return MethodAsync::Reqwest;
#[cfg(feature = "isahc_async")]
return MethodAsync::Isahc;
panic!("No method selected");
}
}
#[cfg(feature = "async")]
impl MethodAsync {
pub async fn fetch(&self, url: &str) -> Result<String, Box<dyn std::error::Error>> {
Ok(match self {
#[cfg(feature = "reqwest_async")]
MethodAsync::Reqwest => reqwest::get(url).await?.text().await?,
#[cfg(feature = "isahc_async")]
MethodAsync::Isahc => {
use isahc::AsyncReadResponseExt;
isahc::get_async(url).await?.text().await?
}
})
}
}