Skip to content

Commit

Permalink
Use compile_error if no http features are enabled (#698)
Browse files Browse the repository at this point in the history
- Provide help so people know that they have to enable a feature.
- Put imports in place so conditional compilation doesn't try to compile unnecesary code.

Signed-off-by: David Calavera <[email protected]>
  • Loading branch information
calavera authored Sep 18, 2023
1 parent cf72bb0 commit 722e33f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
23 changes: 10 additions & 13 deletions lambda-http/src/deserializer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use crate::request::LambdaRequest;
use aws_lambda_events::{
alb::AlbTargetGroupRequest,
apigw::{ApiGatewayProxyRequest, ApiGatewayV2httpRequest, ApiGatewayWebsocketProxyRequest},
};
use serde::{de::Error, Deserialize};

const ERROR_CONTEXT: &str = "this function expects a JSON payload from Amazon API Gateway, Amazon Elastic Load Balancer, or AWS Lambda Function URLs, but the data doesn't match any of those services' events";
Expand All @@ -17,28 +13,29 @@ impl<'de> Deserialize<'de> for LambdaRequest {
Err(err) => return Err(err),
};
#[cfg(feature = "apigw_rest")]
if let Ok(res) =
ApiGatewayProxyRequest::deserialize(serde::__private::de::ContentRefDeserializer::<D::Error>::new(&content))
{
if let Ok(res) = aws_lambda_events::apigw::ApiGatewayProxyRequest::deserialize(
serde::__private::de::ContentRefDeserializer::<D::Error>::new(&content),
) {
return Ok(LambdaRequest::ApiGatewayV1(res));
}
#[cfg(feature = "apigw_http")]
if let Ok(res) = ApiGatewayV2httpRequest::deserialize(
if let Ok(res) = aws_lambda_events::apigw::ApiGatewayV2httpRequest::deserialize(
serde::__private::de::ContentRefDeserializer::<D::Error>::new(&content),
) {
return Ok(LambdaRequest::ApiGatewayV2(res));
}
#[cfg(feature = "alb")]
if let Ok(res) =
AlbTargetGroupRequest::deserialize(serde::__private::de::ContentRefDeserializer::<D::Error>::new(&content))
aws_lambda_events::alb::AlbTargetGroupRequest::deserialize(serde::__private::de::ContentRefDeserializer::<
D::Error,
>::new(&content))
{
return Ok(LambdaRequest::Alb(res));
}
#[cfg(feature = "apigw_websockets")]
if let Ok(res) = ApiGatewayWebsocketProxyRequest::deserialize(serde::__private::de::ContentRefDeserializer::<
D::Error,
>::new(&content))
{
if let Ok(res) = aws_lambda_events::apigw::ApiGatewayWebsocketProxyRequest::deserialize(
serde::__private::de::ContentRefDeserializer::<D::Error>::new(&content),
) {
return Ok(LambdaRequest::WebSocket(res));
}

Expand Down
31 changes: 22 additions & 9 deletions lambda-http/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
//! [`RequestExt`]: crate::RequestExt
#[cfg(any(feature = "apigw_rest", feature = "apigw_http", feature = "apigw_websockets"))]
use crate::ext::extensions::{PathParameters, StageVariables};
#[cfg(any(
feature = "apigw_rest",
feature = "apigw_http",
feature = "alb",
feature = "apigw_websockets"
))]
use crate::ext::extensions::{QueryStringParameters, RawHttpPath};
#[cfg(feature = "alb")]
use aws_lambda_events::alb::{AlbTargetGroupRequest, AlbTargetGroupRequestContext};
Expand All @@ -26,7 +32,7 @@ use serde_json::error::Error as JsonError;

use std::future::Future;
use std::pin::Pin;
use std::{env, io::Read, mem};
use std::{env, io::Read};
use url::Url;

/// Internal representation of an Lambda http event from
Expand Down Expand Up @@ -61,6 +67,13 @@ impl LambdaRequest {
LambdaRequest::Alb { .. } => RequestOrigin::Alb,
#[cfg(feature = "apigw_websockets")]
LambdaRequest::WebSocket { .. } => RequestOrigin::WebSocket,
#[cfg(not(any(
feature = "apigw_rest",
feature = "apigw_http",
feature = "alb",
feature = "apigw_websockets"
)))]
_ => compile_error!("Either feature `apigw_rest`, `apigw_http`, `alb`, or `apigw_websockets` must be enabled for the `lambda-http` crate."),
}
}
}
Expand Down Expand Up @@ -141,8 +154,8 @@ fn into_api_gateway_v2_request(ag: ApiGatewayV2httpRequest) -> http::Request<Bod
.expect("failed to build request");

// no builder method that sets headers in batch
let _ = mem::replace(req.headers_mut(), headers);
let _ = mem::replace(req.method_mut(), http_method);
let _ = std::mem::replace(req.headers_mut(), headers);
let _ = std::mem::replace(req.method_mut(), http_method);

req
}
Expand Down Expand Up @@ -203,8 +216,8 @@ fn into_proxy_request(ag: ApiGatewayProxyRequest) -> http::Request<Body> {
.expect("failed to build request");

// no builder method that sets headers in batch
let _ = mem::replace(req.headers_mut(), headers);
let _ = mem::replace(req.method_mut(), http_method);
let _ = std::mem::replace(req.headers_mut(), headers);
let _ = std::mem::replace(req.method_mut(), http_method);

req
}
Expand Down Expand Up @@ -255,8 +268,8 @@ fn into_alb_request(alb: AlbTargetGroupRequest) -> http::Request<Body> {
.expect("failed to build request");

// no builder method that sets headers in batch
let _ = mem::replace(req.headers_mut(), headers);
let _ = mem::replace(req.method_mut(), http_method);
let _ = std::mem::replace(req.headers_mut(), headers);
let _ = std::mem::replace(req.method_mut(), http_method);

req
}
Expand Down Expand Up @@ -319,8 +332,8 @@ fn into_websocket_request(ag: ApiGatewayWebsocketProxyRequest) -> http::Request<
.expect("failed to build request");

// no builder method that sets headers in batch
let _ = mem::replace(req.headers_mut(), headers);
let _ = mem::replace(req.method_mut(), http_method.unwrap_or(http::Method::GET));
let _ = std::mem::replace(req.headers_mut(), headers);
let _ = std::mem::replace(req.method_mut(), http_method.unwrap_or(http::Method::GET));

req
}
Expand Down
7 changes: 7 additions & 0 deletions lambda-http/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ impl LambdaResponse {
headers: headers.clone(),
multi_value_headers: headers,
}),
#[cfg(not(any(
feature = "apigw_rest",
feature = "apigw_http",
feature = "alb",
feature = "apigw_websockets"
)))]
_ => compile_error!("Either feature `apigw_rest`, `apigw_http`, `alb`, or `apigw_websockets` must be enabled for the `lambda-http` crate."),
}
}
}
Expand Down

0 comments on commit 722e33f

Please sign in to comment.