From f8097f4b752374d83a471581d162a31aeca29778 Mon Sep 17 00:00:00 2001 From: softprops Date: Tue, 26 May 2020 17:23:18 -0400 Subject: [PATCH 1/2] replace anyhow with std error and an type alias --- Cargo.lock | 7 ------- ...{basic.rs => hello-http-without-macros.rs} | 4 +--- .../examples/{hello.rs => hello-http.rs} | 4 +--- lambda-http/src/ext.rs | 4 +--- lambda-http/src/lib.rs | 20 ++++++------------- lambda/Cargo.toml | 1 - lambda/examples/hello-with-ctx.rs | 4 +--- lambda/examples/hello-without-macro.rs | 4 +--- lambda/examples/hello.rs | 4 +--- lambda/src/client.rs | 14 +++++++------ lambda/src/lib.rs | 14 ++++++------- lambda/src/requests.rs | 3 +-- lambda/src/types.rs | 3 +-- 13 files changed, 28 insertions(+), 58 deletions(-) rename lambda-http/examples/{basic.rs => hello-http-without-macros.rs} (76%) rename lambda-http/examples/{hello.rs => hello-http.rs} (50%) diff --git a/Cargo.lock b/Cargo.lock index 6e8f9d85..49c98444 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,10 +1,5 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -[[package]] -name = "anyhow" -version = "1.0.28" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "arc-swap" version = "0.4.4" @@ -286,7 +281,6 @@ dependencies = [ name = "lambda" version = "0.1.0" dependencies = [ - "anyhow 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "genawaiter 0.99.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -851,7 +845,6 @@ dependencies = [ ] [metadata] -"checksum anyhow 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)" = "d9a60d744a80c30fcb657dfe2c1b22bcb3e814c1a1e3674f32bf5820b570fbff" "checksum arc-swap 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d7b8a9123b8027467bce0099fe556c628a53c8d83df0507084c31e9ba2e39aff" "checksum autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" "checksum base64 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)" = "53d1ccbaf7d9ec9537465a97bf19edc1a4e158ecb49fc16178202238c569cc42" diff --git a/lambda-http/examples/basic.rs b/lambda-http/examples/hello-http-without-macros.rs similarity index 76% rename from lambda-http/examples/basic.rs rename to lambda-http/examples/hello-http-without-macros.rs index db740c8e..a1244a0f 100644 --- a/lambda-http/examples/basic.rs +++ b/lambda-http/examples/hello-http-without-macros.rs @@ -1,6 +1,4 @@ -use lambda_http::{handler, lambda, IntoResponse, Request, RequestExt, Response}; - -type Error = Box; +use lambda_http::{handler, lambda, Error, IntoResponse, Request, RequestExt, Response}; #[tokio::main] async fn main() -> Result<(), Error> { diff --git a/lambda-http/examples/hello.rs b/lambda-http/examples/hello-http.rs similarity index 50% rename from lambda-http/examples/hello.rs rename to lambda-http/examples/hello-http.rs index 978a50e3..fc65f9fd 100644 --- a/lambda-http/examples/hello.rs +++ b/lambda-http/examples/hello-http.rs @@ -1,6 +1,4 @@ -use lambda_http::{lambda, IntoResponse, Request}; - -type Error = Box; +use lambda_http::{lambda, Error, IntoResponse, Request}; #[lambda(http)] #[tokio::main] diff --git a/lambda-http/src/ext.rs b/lambda-http/src/ext.rs index 761a6417..ae416a26 100644 --- a/lambda-http/src/ext.rs +++ b/lambda-http/src/ext.rs @@ -67,11 +67,9 @@ impl Error for PayloadError { /// as well as `{"x":1, "y":2}` respectively. /// /// ```rust,no_run -/// use lambda_http::{handler, lambda, Body, IntoResponse, Request, Response, RequestExt}; +/// use lambda_http::{handler, lambda, Error, Body, IntoResponse, Request, Response, RequestExt}; /// use serde_derive::Deserialize; /// -/// type Error = Box; -/// /// #[derive(Debug,Deserialize,Default)] /// struct Args { /// #[serde(default)] diff --git a/lambda-http/src/lib.rs b/lambda-http/src/lib.rs index bca6287a..c5435e57 100644 --- a/lambda-http/src/lib.rs +++ b/lambda-http/src/lib.rs @@ -22,8 +22,7 @@ //! The full body of your `main` function will be executed on **every** invocation of your lambda task. //! //! ```rust,no_run -//! use lambda_http::{lambda, Request, IntoResponse}; -//! type Error = Box; +//! use lambda_http::{lambda, Error, Request, IntoResponse}; //! //! #[lambda(http)] //! #[tokio::main] @@ -39,8 +38,7 @@ //! Depending on the runtime cost of your dependency bootstrapping, this can reduce the overall latency of your functions execution path. //! //! ```rust,no_run -//! use lambda_http::{handler, lambda}; -//! type Error = Box; +//! use lambda_http::{handler, lambda, Error}; //! //! #[tokio::main] //! async fn main() -> Result<(), Error> { @@ -58,8 +56,7 @@ //! with the [`RequestExt`](trait.RequestExt.html) trait. //! //! ```rust,no_run -//! use lambda_http::{handler, lambda, IntoResponse, Request, RequestExt}; -//! type Error = Box; +//! use lambda_http::{handler, lambda, Error, IntoResponse, Request, RequestExt}; //! //! #[tokio::main] //! async fn main() -> Result<(), Error> { @@ -87,7 +84,7 @@ extern crate maplit; pub use http::{self, Response}; use lambda::Handler as LambdaHandler; -pub use lambda::{self}; +pub use lambda::{self, Error}; pub use lambda_attributes::lambda; //pub use lambda_http_attributes::lambda_http; mod body; @@ -98,15 +95,11 @@ mod strmap; pub use crate::{body::Body, ext::RequestExt, response::IntoResponse, strmap::StrMap}; use crate::{request::LambdaRequest, response::LambdaResponse}; use std::{ - error::Error, - fmt, future::Future, pin::Pin, task::{Context, Poll}, }; -type Err = Box; - /// Type alias for `http::Request`s with a fixed [`Body`](enum.Body.html) type pub type Request = http::Request; @@ -134,11 +127,10 @@ impl Handler for F where F: FnMut(Request) -> Fut, R: IntoResponse, - Fut: Future> + Send + 'static, - Err: Into> + fmt::Debug, + Fut: Future> + Send + 'static, { type Response = R; - type Error = Err; + type Error = Error; type Fut = Fut; fn call(&mut self, event: Request) -> Self::Fut { (*self)(event) diff --git a/lambda/Cargo.toml b/lambda/Cargo.toml index b3b82041..7acf8cbd 100644 --- a/lambda/Cargo.toml +++ b/lambda/Cargo.toml @@ -25,4 +25,3 @@ futures = "0.3" tracing = "0.1.13" tracing-futures = "0.2.3" tracing-error = "0.1.2" -anyhow = "1.0.27" diff --git a/lambda/examples/hello-with-ctx.rs b/lambda/examples/hello-with-ctx.rs index 957c2a58..3fb3adf6 100644 --- a/lambda/examples/hello-with-ctx.rs +++ b/lambda/examples/hello-with-ctx.rs @@ -1,8 +1,6 @@ -use lambda::lambda; +use lambda::{lambda, Error}; use serde_json::Value; -type Error = Box; - #[lambda] #[tokio::main] async fn main(event: Value) -> Result { diff --git a/lambda/examples/hello-without-macro.rs b/lambda/examples/hello-without-macro.rs index dd20bd10..e115abf4 100644 --- a/lambda/examples/hello-without-macro.rs +++ b/lambda/examples/hello-without-macro.rs @@ -1,8 +1,6 @@ -use lambda::handler_fn; +use lambda::{handler_fn, Error}; use serde_json::Value; -type Error = Box; - #[tokio::main] async fn main() -> Result<(), Error> { let func = handler_fn(func); diff --git a/lambda/examples/hello.rs b/lambda/examples/hello.rs index 957c2a58..3fb3adf6 100644 --- a/lambda/examples/hello.rs +++ b/lambda/examples/hello.rs @@ -1,8 +1,6 @@ -use lambda::lambda; +use lambda::{lambda, Error}; use serde_json::Value; -type Error = Box; - #[lambda] #[tokio::main] async fn main(event: Value) -> Result { diff --git a/lambda/src/client.rs b/lambda/src/client.rs index 7bc833a2..64313ebf 100644 --- a/lambda/src/client.rs +++ b/lambda/src/client.rs @@ -1,5 +1,7 @@ -use crate::requests::{IntoResponse, NextEventResponse}; -use anyhow::Error; +use crate::{ + requests::{IntoResponse, NextEventResponse}, + Error, +}; use http::{ uri::{PathAndQuery, Scheme}, HeaderValue, Method, Request, Response, StatusCode, Uri, @@ -172,8 +174,8 @@ mod endpoint_tests { requests::{EventCompletionRequest, EventErrorRequest, IntoRequest, NextEventRequest}, simulated::SimulatedConnector, types::Diagnostic, + Error, }; - use anyhow::Error; use http::{HeaderValue, StatusCode, Uri}; use std::convert::TryFrom; use tokio::sync; @@ -203,7 +205,7 @@ mod endpoint_tests { tx.send(()).expect("Receiver has been dropped"); match server.await { Ok(_) => Ok(()), - Err(e) if e.is_panic() => return Err::<(), anyhow::Error>(e.into()), + Err(e) if e.is_panic() => return Err::<(), Error>(e.into()), Err(_) => unreachable!("This branch shouldn't be reachable"), } } @@ -235,7 +237,7 @@ mod endpoint_tests { tx.send(()).expect("Receiver has been dropped"); match server.await { Ok(_) => Ok(()), - Err(e) if e.is_panic() => return Err::<(), anyhow::Error>(e.into()), + Err(e) if e.is_panic() => return Err::<(), Error>(e.into()), Err(_) => unreachable!("This branch shouldn't be reachable"), } } @@ -269,7 +271,7 @@ mod endpoint_tests { tx.send(()).expect("Receiver has been dropped"); match server.await { Ok(_) => Ok(()), - Err(e) if e.is_panic() => return Err::<(), anyhow::Error>(e.into()), + Err(e) if e.is_panic() => return Err::<(), Error>(e.into()), Err(_) => unreachable!("This branch shouldn't be reachable"), } } diff --git a/lambda/src/lib.rs b/lambda/src/lib.rs index 0c76f78f..d5e098a8 100644 --- a/lambda/src/lib.rs +++ b/lambda/src/lib.rs @@ -25,11 +25,9 @@ //! of [`lambda::LambdaCtx`]. //! //! ```no_run -//! use lambda::lambda; +//! use lambda::{lambda, Error}; //! use serde_json::Value; //! -//! type Error = Box; -//! //! #[lambda] //! #[tokio::main] //! async fn main(event: Value) -> Result { @@ -37,7 +35,6 @@ //! } //! ``` pub use crate::types::LambdaCtx; -use anyhow::Error; use client::Client; use futures::stream::{Stream, StreamExt}; use genawaiter::{sync::gen, yield_}; @@ -58,6 +55,9 @@ mod types; use requests::{EventCompletionRequest, EventErrorRequest, IntoRequest, NextEventRequest}; use types::Diagnostic; +/// Error type that lambdas may result in +pub type Error = Box; + /// Configuration derived from environment variables. #[derive(Debug, Default, Clone, PartialEq)] pub struct Config { @@ -119,7 +119,7 @@ impl Handler for HandlerFn where F: Fn(A) -> Fut, Fut: Future> + Send, - Error: Into> + fmt::Debug, + Error: Into + fmt::Debug, { type Error = Error; type Fut = Fut; @@ -134,11 +134,9 @@ where /// /// # Example /// ```no_run -/// use lambda::handler_fn; +/// use lambda::{handler_fn, Error}; /// use serde_json::Value; /// -/// type Error = Box; -/// /// #[tokio::main] /// async fn main() -> Result<(), Error> { /// let func = handler_fn(func); diff --git a/lambda/src/requests.rs b/lambda/src/requests.rs index 4eabc3c1..2d691b94 100644 --- a/lambda/src/requests.rs +++ b/lambda/src/requests.rs @@ -1,5 +1,4 @@ -use crate::types::Diagnostic; -use anyhow::Error; +use crate::{types::Diagnostic, Error}; use http::{Method, Request, Response, Uri}; use hyper::Body; use serde::Serialize; diff --git a/lambda/src/types.rs b/lambda/src/types.rs index b744cd46..2efcb3d1 100644 --- a/lambda/src/types.rs +++ b/lambda/src/types.rs @@ -1,5 +1,4 @@ -use crate::Config; -use anyhow::Error; +use crate::{Config, Error}; use http::HeaderMap; use serde::{Deserialize, Serialize}; use std::{collections::HashMap, convert::TryFrom}; From 2278170de6fa5019cc62c8198c4a6a392a501c7e Mon Sep 17 00:00:00 2001 From: softprops Date: Wed, 27 May 2020 18:15:41 -0400 Subject: [PATCH 2/2] walk back pub type alias for lambda::Error --- .../examples/hello-http-without-macros.rs | 4 +++- lambda-http/examples/hello-http.rs | 4 +++- lambda-http/src/ext.rs | 4 +++- lambda-http/src/lib.rs | 19 ++++++++++++++----- lambda/examples/hello-with-ctx.rs | 4 +++- lambda/examples/hello-without-macro.rs | 4 +++- lambda/examples/hello.rs | 4 +++- lambda/src/lib.rs | 10 +++++++--- 8 files changed, 39 insertions(+), 14 deletions(-) diff --git a/lambda-http/examples/hello-http-without-macros.rs b/lambda-http/examples/hello-http-without-macros.rs index a1244a0f..db740c8e 100644 --- a/lambda-http/examples/hello-http-without-macros.rs +++ b/lambda-http/examples/hello-http-without-macros.rs @@ -1,4 +1,6 @@ -use lambda_http::{handler, lambda, Error, IntoResponse, Request, RequestExt, Response}; +use lambda_http::{handler, lambda, IntoResponse, Request, RequestExt, Response}; + +type Error = Box; #[tokio::main] async fn main() -> Result<(), Error> { diff --git a/lambda-http/examples/hello-http.rs b/lambda-http/examples/hello-http.rs index fc65f9fd..978a50e3 100644 --- a/lambda-http/examples/hello-http.rs +++ b/lambda-http/examples/hello-http.rs @@ -1,4 +1,6 @@ -use lambda_http::{lambda, Error, IntoResponse, Request}; +use lambda_http::{lambda, IntoResponse, Request}; + +type Error = Box; #[lambda(http)] #[tokio::main] diff --git a/lambda-http/src/ext.rs b/lambda-http/src/ext.rs index ae416a26..761a6417 100644 --- a/lambda-http/src/ext.rs +++ b/lambda-http/src/ext.rs @@ -67,9 +67,11 @@ impl Error for PayloadError { /// as well as `{"x":1, "y":2}` respectively. /// /// ```rust,no_run -/// use lambda_http::{handler, lambda, Error, Body, IntoResponse, Request, Response, RequestExt}; +/// use lambda_http::{handler, lambda, Body, IntoResponse, Request, Response, RequestExt}; /// use serde_derive::Deserialize; /// +/// type Error = Box; +/// /// #[derive(Debug,Deserialize,Default)] /// struct Args { /// #[serde(default)] diff --git a/lambda-http/src/lib.rs b/lambda-http/src/lib.rs index c5435e57..077356bf 100644 --- a/lambda-http/src/lib.rs +++ b/lambda-http/src/lib.rs @@ -22,7 +22,9 @@ //! The full body of your `main` function will be executed on **every** invocation of your lambda task. //! //! ```rust,no_run -//! use lambda_http::{lambda, Error, Request, IntoResponse}; +//! use lambda_http::{lambda, Request, IntoResponse}; +//! +//! type Error = Box; //! //! #[lambda(http)] //! #[tokio::main] @@ -38,7 +40,9 @@ //! Depending on the runtime cost of your dependency bootstrapping, this can reduce the overall latency of your functions execution path. //! //! ```rust,no_run -//! use lambda_http::{handler, lambda, Error}; +//! use lambda_http::{handler, lambda}; +//! +//! type Error = Box; //! //! #[tokio::main] //! async fn main() -> Result<(), Error> { @@ -56,7 +60,9 @@ //! with the [`RequestExt`](trait.RequestExt.html) trait. //! //! ```rust,no_run -//! use lambda_http::{handler, lambda, Error, IntoResponse, Request, RequestExt}; +//! use lambda_http::{handler, lambda, IntoResponse, Request, RequestExt}; +//! +//! type Error = Box; //! //! #[tokio::main] //! async fn main() -> Result<(), Error> { @@ -84,9 +90,9 @@ extern crate maplit; pub use http::{self, Response}; use lambda::Handler as LambdaHandler; -pub use lambda::{self, Error}; +pub use lambda::{self}; pub use lambda_attributes::lambda; -//pub use lambda_http_attributes::lambda_http; + mod body; pub mod ext; pub mod request; @@ -100,6 +106,9 @@ use std::{ task::{Context, Poll}, }; +/// Error type that lambdas may result in +pub(crate) type Error = Box; + /// Type alias for `http::Request`s with a fixed [`Body`](enum.Body.html) type pub type Request = http::Request; diff --git a/lambda/examples/hello-with-ctx.rs b/lambda/examples/hello-with-ctx.rs index 3fb3adf6..957c2a58 100644 --- a/lambda/examples/hello-with-ctx.rs +++ b/lambda/examples/hello-with-ctx.rs @@ -1,6 +1,8 @@ -use lambda::{lambda, Error}; +use lambda::lambda; use serde_json::Value; +type Error = Box; + #[lambda] #[tokio::main] async fn main(event: Value) -> Result { diff --git a/lambda/examples/hello-without-macro.rs b/lambda/examples/hello-without-macro.rs index e115abf4..dd20bd10 100644 --- a/lambda/examples/hello-without-macro.rs +++ b/lambda/examples/hello-without-macro.rs @@ -1,6 +1,8 @@ -use lambda::{handler_fn, Error}; +use lambda::handler_fn; use serde_json::Value; +type Error = Box; + #[tokio::main] async fn main() -> Result<(), Error> { let func = handler_fn(func); diff --git a/lambda/examples/hello.rs b/lambda/examples/hello.rs index 3fb3adf6..957c2a58 100644 --- a/lambda/examples/hello.rs +++ b/lambda/examples/hello.rs @@ -1,6 +1,8 @@ -use lambda::{lambda, Error}; +use lambda::lambda; use serde_json::Value; +type Error = Box; + #[lambda] #[tokio::main] async fn main(event: Value) -> Result { diff --git a/lambda/src/lib.rs b/lambda/src/lib.rs index d5e098a8..17067d83 100644 --- a/lambda/src/lib.rs +++ b/lambda/src/lib.rs @@ -25,9 +25,11 @@ //! of [`lambda::LambdaCtx`]. //! //! ```no_run -//! use lambda::{lambda, Error}; +//! use lambda::{lambda}; //! use serde_json::Value; //! +//! type Error = Box; +//! //! #[lambda] //! #[tokio::main] //! async fn main(event: Value) -> Result { @@ -56,7 +58,7 @@ use requests::{EventCompletionRequest, EventErrorRequest, IntoRequest, NextEvent use types::Diagnostic; /// Error type that lambdas may result in -pub type Error = Box; +pub(crate) type Error = Box; /// Configuration derived from environment variables. #[derive(Debug, Default, Clone, PartialEq)] @@ -134,9 +136,11 @@ where /// /// # Example /// ```no_run -/// use lambda::{handler_fn, Error}; +/// use lambda::{handler_fn}; /// use serde_json::Value; /// +/// type Error = Box; +/// /// #[tokio::main] /// async fn main() -> Result<(), Error> { /// let func = handler_fn(func);