Skip to content

Commit

Permalink
Update FTL integration
Browse files Browse the repository at this point in the history
  • Loading branch information
novacrazy committed Sep 27, 2024
1 parent da80edc commit 6d0d5f8
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/api/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,8 +580,7 @@ macro_rules! command {

#[cfg(feature = "ftl")]
const _: () = {
use ftl::{Request, Response, IntoResponse};
use ftl::extract::{FromRequest, FromRequestParts, path::Path};
use ftl::{Request, extract::{FromRequest, FromRequestParts, path::Path}};

mod segments {
ftl::path_segment! {
Expand All @@ -592,37 +591,34 @@ macro_rules! command {
impl<S> FromRequest<S> for $name
where S: Send + Sync,
{
type Rejection = Response;
type Rejection = ftl::Error;

#[allow(unused_variables, clippy::manual_async_fn)]
fn from_request(req: Request, state: &S) -> impl std::future::Future<Output = Result<Self, Self::Rejection>> + Send {
async move {
let (mut parts, body) = req.into_parts();

if parts.method != <Self as $crate::api::Command>::HTTP_METHOD {
return Err(http::StatusCode::METHOD_NOT_ALLOWED.into_response());
return Err(ftl::Error::MethodNotAllowed);
}

$(
_ = stringify!($auth_struct);

if parts.extensions.get::<crate::api::AuthMarker>().is_none() {
return Err(http::StatusCode::UNAUTHORIZED.into_response());
return Err(ftl::Error::Unauthorized);
}
)?

let Path(($($field_name,)*)) = Path::<($(segments::[<$field_name:camel>],)*)>::from_request_parts(&mut parts, state)
.await.map_err(IntoResponse::into_response)?;
let Path(($($field_name,)*)) = Path::<($(segments::[<$field_name:camel>],)*)>::from_request_parts(&mut parts, state).await.map_err(ftl::Error::from)?;

Ok($name {
$($field_name,)*

$(body: if <Self as $crate::api::Command>::IS_QUERY {
ftl::extract::query::Query::<$body_name>::from_request_parts(&mut parts, state)
.await.map_err(IntoResponse::into_response)?.0
ftl::extract::query::Query::<$body_name>::from_request_parts(&mut parts, state).await.map_err(ftl::Error::from)?.0
} else {
ftl::extract::one_of::OneOfAny::<$body_name>::from_request(Request::from_parts(parts, body), state)
.await.map_err(IntoResponse::into_response)?.0
ftl::extract::one_of::OneOfAny::<$body_name>::from_request(Request::from_parts(parts, body), state).await.map_err(ftl::Error::from)?.0
})?
})
}
Expand Down

0 comments on commit 6d0d5f8

Please sign in to comment.