use core::fmt::{Debug, Display};
use core::future::Future;
use core::ops::Deref;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Method {
Get,
Post,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct HttpMethod(Method);
impl HttpMethod {
pub const GET: Self = Self(Method::Get);
pub const POST: Self = Self(Method::Post);
}
#[cfg(feature = "bitreq")]
impl From<HttpMethod> for bitreq::Method {
fn from(method: HttpMethod) -> Self {
match method.0 {
Method::Get => bitreq::Method::Get,
Method::Post => bitreq::Method::Post,
}
}
}
pub trait Http {
type Body: AsRef<[u8]> + From<Vec<u8>>;
type Err: Debug + Display;
fn send<'a>(
&'a self,
method: HttpMethod,
url: &'a str,
body: impl Into<Self::Body>,
) -> impl Future<Output = Result<Self::Body, Self::Err>>
where
Self: 'a;
}
impl<T> Http for T
where
T: Deref,
T::Target: Http,
{
type Body = <T::Target as Http>::Body;
type Err = <T::Target as Http>::Err;
fn send<'a>(
&'a self,
method: HttpMethod,
url: &'a str,
body: impl Into<Self::Body>,
) -> impl Future<Output = Result<Self::Body, Self::Err>>
where
Self: 'a,
{
(**self).send(method, url, body)
}
}