From 4b5bc01c0a0a3a38ca8c84bb6185b91732f2e3e1 Mon Sep 17 00:00:00 2001 From: Ae Date: Fri, 16 Feb 2024 17:28:28 -0800 Subject: [PATCH] feature/cargo-features --- Cargo.toml | 24 ++++++++++++++++-------- README.md | 9 ++++----- src/headers.rs | 12 ++++++++---- src/lib.rs | 7 +++++++ src/mongodb.rs | 14 ++++++++------ 5 files changed, 43 insertions(+), 23 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b38c98d..6fb0994 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,12 +10,20 @@ categories = ["utils"] license = "MIT" [dependencies] -aws-config = { version = "1.1.2", features = ["behavior-version-latest"] } -aws-sdk-sqs = "1.9.0" -aws-types = "1.1.2" -aws_lambda_events = "0.7.2" -lambda_http = "0.7.0" -mongodb = "2.3.1" -once_cell = "1.17.0" -serde_json = "1.0.79" +aws-config = { version = "1.1.2", features = ["behavior-version-latest"], optional = true } +aws-sdk-sqs = { version = "1.9.0", optional = true } +aws-types = { version = "1.1.2", optional = true } +aws_lambda_events = { version = "0.7.2", optional = true } +lambda_http = { version = "0.7.0", optional = true } +mongodb = { version = "2.3.1", optional = true } +once_cell = { version = "1.17.0", optional = true } +serde_json = { version = "1.0.79", optional = true } + +[dev-dependencies] tokio = { version = "1.17.0", features = ["full", "test-util"] } + +[features] +headers = ["lambda_http", "serde_json"] +mongodb = ["dep:mongodb", "once_cell"] +network = ["aws_lambda_events", "lambda_http"] +sqs = ["aws-sdk-sqs", "aws-config", "serde_json"] diff --git a/README.md b/README.md index ed3f735..77fd5a7 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Lambda Utils is a collection of utility modules designed to facilitate common ta ## Included Utils -### header.rs +### headers.rs - **Description:** A utility module for parsing HTTP headers. - **Usage:** Helps in extracting and processing headers from incoming HTTP requests. @@ -23,11 +23,10 @@ Lambda Utils is a collection of utility modules designed to facilitate common ta ## Getting Started To start using Lambda Utils in your AWS Rust Lambda projects, follow these steps: -1. Add Lambda Utils as a dependency in your `Cargo.toml` file: +1. Add Lambda Utils as a dependency in your project: - ```toml - [dependencies] - lambda-utils = "0.1.0" + ```sh + cargo add lambda-utils ``` 2. Import the desired utility modules into your Rust Lambda project: diff --git a/src/headers.rs b/src/headers.rs index bea9972..9097319 100644 --- a/src/headers.rs +++ b/src/headers.rs @@ -2,14 +2,18 @@ use lambda_http::{Request}; use std::collections::{HashMap}; use serde_json::{to_string}; -pub fn get_header_user_agent(request: &Request) -> String { +pub fn get_header_value(request: &Request, value: String) -> String { return request .headers() - .get("User-Agent") + .get(value) .map_or_else( - || "Unknown User-Agent".to_string(), - |header_value| header_value.to_str().unwrap_or("Invalid User-Agent").to_string(), + || "Unknown header value".to_string(), + |header_value| header_value.to_str().unwrap_or("Invalid header value").to_string(), ); + + +pub fn get_header_user_agent(request: &Request) -> String { + return get_header_value("User-Agent") } pub fn get_header_cookies(request: &Request) -> String { diff --git a/src/lib.rs b/src/lib.rs index 2703596..ab5ba10 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,11 @@ +#[cfg(feature = "headers")] pub mod headers; + +#[cfg(feature = "mongodb")] pub mod mongodb; + +#[cfg(feature = "network")] pub mod network; + +#[cfg(feature = "sqs")] pub mod sqs; diff --git a/src/mongodb.rs b/src/mongodb.rs index d5c87dc..274cb04 100644 --- a/src/mongodb.rs +++ b/src/mongodb.rs @@ -32,12 +32,14 @@ pub fn get_mongodb_client() -> Result<&'static Client, Box