#![warn(missing_docs)]
use std::borrow::Cow;
pub mod query;
use query::ToQuery;
pub mod methods;
use methods::*;
pub mod data;
pub use data::{Decodable, Encodable};
mod get;
pub use get::*;
mod head;
pub use head::*;
mod post;
pub use post::*;
mod patch;
pub use patch::*;
mod delete;
pub use delete::*;
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Method {
Get,
Head,
Post,
Put,
Delete,
Connect,
Options,
Trace,
Patch,
}
#[cfg(feature = "http")]
impl From<Method> for http::Method {
fn from(method: Method) -> Self {
match method {
Method::Get => http::Method::GET,
Method::Head => http::Method::HEAD,
Method::Post => http::Method::POST,
Method::Put => http::Method::PUT,
Method::Delete => http::Method::DELETE,
Method::Connect => http::Method::CONNECT,
Method::Options => http::Method::OPTIONS,
Method::Trace => http::Method::TRACE,
Method::Patch => http::Method::PATCH,
}
}
}
pub trait Request {
type Request: Encodable;
type Response: Decodable;
type Query: ToQuery;
fn path(&self) -> Cow<'_, str>;
fn body(&self) -> Self::Request;
fn query(&self) -> Self::Query;
fn method(&self) -> Method;
fn uri(&self) -> String {
let mut path = self.path().into_owned();
let query = self.query();
let query_string = query.encode();
if !query_string.is_empty() {
path.push('?');
path.push_str(&query_string);
}
path
}
}
pub trait RequestType: Sized {
type Request: Request;
fn borrow(&self) -> &Self::Request;
}
pub trait RequestMethod: Sized
where
Self::Method: From<Self>,
for<'a> &'a Self::Method: From<&'a Self>,
{
type Method: Request;
fn as_request(&self) -> &Self::Method {
self.into()
}
fn into_request(self) -> Self::Method {
self.into()
}
}
impl<T: RequestMethod> RequestType for T
where
for<'a> &'a <T as RequestMethod>::Method: From<&'a T>,
{
type Request = <T as RequestMethod>::Method;
fn borrow(&self) -> &Self::Request {
self.into()
}
}
impl<T: RequestType> Request for T {
type Request = <<T as RequestType>::Request as Request>::Request;
type Response = <<T as RequestType>::Request as Request>::Response;
type Query = <<T as RequestType>::Request as Request>::Query;
fn path(&self) -> Cow<'_, str> {
self.borrow().path()
}
fn body(&self) -> Self::Request {
self.borrow().body()
}
fn query(&self) -> Self::Query {
self.borrow().query()
}
fn method(&self) -> Method {
self.borrow().method()
}
}