#[cfg(feature = "openapi")]
use crate::openapi::operation::OperationId;
use crate::{IntoResponse, RequestBody};
use futures_util::future::BoxFuture;
use gotham::{
extractor::{PathExtractor, QueryStringExtractor},
hyper::{Body, Method, Response},
router::response::StaticResponseExtender,
state::{State, StateData}
};
#[cfg(feature = "openapi")]
use openapi_type::{OpenapiType, Visitor};
use serde::{Deserialize, Deserializer};
use std::borrow::Cow;
#[derive(Debug, Clone, Copy)]
pub struct NoopExtractor;
impl<'de> Deserialize<'de> for NoopExtractor {
fn deserialize<D: Deserializer<'de>>(_: D) -> Result<Self, D::Error> {
Ok(Self)
}
}
#[cfg(feature = "openapi")]
impl OpenapiType for NoopExtractor {
fn visit_type<V: Visitor>(visitor: &mut V) {
warn!(
"You're asking for the OpenAPI Schema for gotham_restful::NoopExtractor. This is probably not what you want."
);
visitor.visit_unit();
}
}
impl StateData for NoopExtractor {}
impl StaticResponseExtender for NoopExtractor {
type ResBody = Body;
fn extend(_: &mut State, _: &mut Response<Body>) {}
}
#[_private_openapi_trait(EndpointWithSchema)]
pub trait Endpoint {
fn http_method() -> Method;
fn uri() -> Cow<'static, str>;
#[openapi_only]
fn operation_verb() -> Option<&'static str>;
#[openapi_bound(Output: crate::ResponseSchema)]
type Output: IntoResponse + Send;
fn has_placeholders() -> bool {
false
}
#[openapi_bound(Placeholders: OpenapiType)]
type Placeholders: PathExtractor<Body> + Clone + Sync;
fn needs_params() -> bool {
false
}
#[openapi_bound(Params: OpenapiType)]
type Params: QueryStringExtractor<Body> + Clone + Sync;
fn needs_body() -> bool {
false
}
type Body: RequestBody + Send;
fn wants_auth() -> bool {
false
}
#[openapi_only]
fn operation_id() -> OperationId {
OperationId::FullAuto
}
#[openapi_only]
fn description() -> Option<String> {
None
}
fn handle(
state: &mut State,
placeholders: Self::Placeholders,
params: Self::Params,
body: Option<Self::Body>
) -> BoxFuture<'_, Self::Output>;
}
#[cfg(feature = "openapi")]
impl<E: EndpointWithSchema> Endpoint for E {
fn http_method() -> Method {
E::http_method()
}
fn uri() -> Cow<'static, str> {
E::uri()
}
type Output = E::Output;
fn has_placeholders() -> bool {
E::has_placeholders()
}
type Placeholders = E::Placeholders;
fn needs_params() -> bool {
E::needs_params()
}
type Params = E::Params;
fn needs_body() -> bool {
E::needs_body()
}
type Body = E::Body;
fn wants_auth() -> bool {
E::wants_auth()
}
fn handle<'a>(
state: &'a mut State,
placeholders: Self::Placeholders,
params: Self::Params,
body: Option<Self::Body>
) -> BoxFuture<'a, Self::Output> {
E::handle(state, placeholders, params, body)
}
}