From 722e33f2feb0f0f24f8f276b59d4ef875ccbc0e4 Mon Sep 17 00:00:00 2001 From: David Calavera Date: Mon, 18 Sep 2023 10:19:26 -0700 Subject: [PATCH] Use compile_error if no http features are enabled (#698) - 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 --- lambda-http/src/deserializer.rs | 23 ++++++++++------------- lambda-http/src/request.rs | 31 ++++++++++++++++++++++--------- lambda-http/src/response.rs | 7 +++++++ 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/lambda-http/src/deserializer.rs b/lambda-http/src/deserializer.rs index 1771ea7b..a77f68a5 100644 --- a/lambda-http/src/deserializer.rs +++ b/lambda-http/src/deserializer.rs @@ -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"; @@ -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::::new(&content)) - { + if let Ok(res) = aws_lambda_events::apigw::ApiGatewayProxyRequest::deserialize( + serde::__private::de::ContentRefDeserializer::::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::::new(&content), ) { return Ok(LambdaRequest::ApiGatewayV2(res)); } #[cfg(feature = "alb")] if let Ok(res) = - AlbTargetGroupRequest::deserialize(serde::__private::de::ContentRefDeserializer::::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::::new(&content), + ) { return Ok(LambdaRequest::WebSocket(res)); } diff --git a/lambda-http/src/request.rs b/lambda-http/src/request.rs index bdb755ed..ad86e5a5 100644 --- a/lambda-http/src/request.rs +++ b/lambda-http/src/request.rs @@ -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}; @@ -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 @@ -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."), } } } @@ -141,8 +154,8 @@ fn into_api_gateway_v2_request(ag: ApiGatewayV2httpRequest) -> http::Request 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); + let _ = std::mem::replace(req.headers_mut(), headers); + let _ = std::mem::replace(req.method_mut(), http_method); req } @@ -255,8 +268,8 @@ fn into_alb_request(alb: AlbTargetGroupRequest) -> 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); + let _ = std::mem::replace(req.headers_mut(), headers); + let _ = std::mem::replace(req.method_mut(), http_method); req } @@ -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 } diff --git a/lambda-http/src/response.rs b/lambda-http/src/response.rs index 1a2ede5c..a51d1b2d 100644 --- a/lambda-http/src/response.rs +++ b/lambda-http/src/response.rs @@ -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."), } } }