Skip to content

Commit

Permalink
Add lifecycle shorthands to Constructor (LukeMathWalker#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeMathWalker authored Apr 6, 2024
1 parent ba5be68 commit 092762a
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
```rust title="src/replace/blueprint.rs" hl_lines="9 10"
use pavex::blueprint::constructor::{Constructor, Lifecycle};
use pavex::blueprint::constructor::Constructor;
use pavex::blueprint::Blueprint;
use pavex::f;
use pavex::kit::ApiKit;
pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
let mut kit = ApiKit::new();
let c = Constructor::new(f!(crate::custom_path_params), Lifecycle::RequestScoped); // (1)!
let c = Constructor::request_scoped(f!(crate::custom_path_params)); // (1)!
kit.path_params = Some(c);
kit.register(&mut bp);
// [...]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use pavex::blueprint::constructor::{Constructor, Lifecycle};
use pavex::blueprint::constructor::Constructor;
use pavex::blueprint::Blueprint;
use pavex::f;
use pavex::kit::ApiKit;

pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
let mut kit = ApiKit::new();
let c = Constructor::new(f!(crate::custom_path_params), Lifecycle::RequestScoped); // (1)!
let c = Constructor::request_scoped(f!(crate::custom_path_params)); // (1)!
kit.path_params = Some(c);
kit.register(&mut bp);
bp
Expand Down
24 changes: 24 additions & 0 deletions libs/pavex/src/blueprint/constructor/unregistered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,30 @@ impl Constructor {
}
}

/// Create a new (unregistered) constructor with a [singleton](Lifecycle::Singleton) lifecycle.
///
/// It's a shorthand for [`Constructor::new(callable, Lifecycle::Singleton)`](Constructor::new).
#[track_caller]
pub fn singleton(callable: RawCallable) -> Self {
Constructor::new(callable, Lifecycle::Singleton)
}

/// Create a new (unregistered) constructor with a [request-scoped](Lifecycle::RequestScoped) lifecycle.
///
/// It's a shorthand for [`Constructor::new(callable, Lifecycle::RequestScoped)`](Constructor::new).
#[track_caller]
pub fn request_scoped(callable: RawCallable) -> Self {
Constructor::new(callable, Lifecycle::RequestScoped)
}

/// Create a new (unregistered) constructor with a [transient](Lifecycle::Transient) lifecycle.
///
/// It's a shorthand for [`Constructor::new(callable, Lifecycle::Transient)`](Constructor::new).
#[track_caller]
pub fn transient(callable: RawCallable) -> Self {
Constructor::new(callable, Lifecycle::Transient)
}

/// Register an error handler for this constructor.
///
/// Check out the documentation of [`RegisteredConstructor::error_handler`] for more details.
Expand Down
26 changes: 11 additions & 15 deletions libs/pavex/src/cookie/kit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::blueprint::constructor::{Constructor, Lifecycle};
use crate::blueprint::constructor::Constructor;
use crate::blueprint::linter::Lint;
use crate::blueprint::middleware::PostProcessingMiddleware;
use crate::blueprint::Blueprint;
Expand Down Expand Up @@ -70,20 +70,17 @@ pub struct CookieKit {
impl CookieKit {
/// Create a new [`CookieKit`] with all the bundled constructors and middlewares.
pub fn new() -> Self {
let request_cookies =
Constructor::new(f!(super::extract_request_cookies), Lifecycle::RequestScoped)
.error_handler(f!(super::errors::ExtractRequestCookiesError::into_response))
.ignore(Lint::Unused);
let request_cookies = Constructor::request_scoped(f!(super::extract_request_cookies))
.error_handler(f!(super::errors::ExtractRequestCookiesError::into_response))
.ignore(Lint::Unused);
let response_cookies =
Constructor::new(f!(super::ResponseCookies::new), Lifecycle::RequestScoped)
.ignore(Lint::Unused);
Constructor::request_scoped(f!(super::ResponseCookies::new)).ignore(Lint::Unused);
let response_cookie_injector =
PostProcessingMiddleware::new(f!(super::inject_response_cookies))
.error_handler(f!(super::errors::InjectResponseCookiesError::into_response));
let processor = Constructor::new(
f!(<super::Processor as std::convert::From<super::ProcessorConfig>>::from),
Lifecycle::Singleton,
)
let processor = Constructor::singleton(f!(<super::Processor as std::convert::From<
super::ProcessorConfig,
>>::from))
.ignore(Lint::Unused);
Self {
request_cookies: Some(request_cookies),
Expand All @@ -99,10 +96,9 @@ impl CookieKit {
/// [`ProcessorConfig`]: super::ProcessorConfig
/// [`ProcessorConfig::default`]: super::ProcessorConfig::default
pub fn with_default_processor_config(mut self) -> Self {
let constructor = Constructor::new(
f!(<super::ProcessorConfig as std::default::Default>::default),
Lifecycle::Singleton,
)
let constructor = Constructor::singleton(f!(
<super::ProcessorConfig as std::default::Default>::default
))
.ignore(Lint::Unused);
self.processor_config = Some(constructor);
self
Expand Down
4 changes: 2 additions & 2 deletions libs/pavex/src/request/body/buffered_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use http::header::CONTENT_LENGTH;
use http_body_util::{BodyExt, Limited};
use ubyte::ByteUnit;

use crate::blueprint::constructor::{Constructor, Lifecycle, RegisteredConstructor};
use crate::blueprint::constructor::{Constructor, RegisteredConstructor};
use crate::blueprint::Blueprint;
use crate::{f, request::body::errors::SizeLimitExceeded, request::RequestHead};

Expand Down Expand Up @@ -86,7 +86,7 @@ impl BufferedBody {
/// and [error handler](ExtractBufferedBodyError::into_response)
/// for [`BufferedBody`].
pub fn default_constructor() -> Constructor {
Constructor::new(f!(super::BufferedBody::extract), Lifecycle::RequestScoped)
Constructor::request_scoped(f!(super::BufferedBody::extract))
.error_handler(f!(super::errors::ExtractBufferedBodyError::into_response))
}

Expand Down
4 changes: 2 additions & 2 deletions libs/pavex/src/request/body/json.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use http::HeaderMap;
use serde::Deserialize;

use crate::blueprint::constructor::{Constructor, Lifecycle, RegisteredConstructor};
use crate::blueprint::constructor::{Constructor, RegisteredConstructor};
use crate::blueprint::Blueprint;
use crate::f;
use crate::request::RequestHead;
Expand Down Expand Up @@ -87,7 +87,7 @@ impl JsonBody<()> {
/// The [default constructor](JsonBody::extract)
/// and [error handler](ExtractJsonBodyError::into_response) for [`JsonBody`].
pub fn default_constructor() -> Constructor {
Constructor::new(f!(super::JsonBody::extract), Lifecycle::RequestScoped)
Constructor::request_scoped(f!(super::JsonBody::extract))
.error_handler(f!(super::errors::ExtractJsonBodyError::into_response))
}
}
Expand Down
7 changes: 2 additions & 5 deletions libs/pavex/src/request/body/limit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::blueprint::constructor::{Constructor, Lifecycle, RegisteredConstructor};
use crate::blueprint::constructor::{Constructor, RegisteredConstructor};
use crate::blueprint::Blueprint;
use crate::f;
use crate::unit::ByteUnit;
Expand Down Expand Up @@ -27,10 +27,7 @@ impl BodySizeLimit {

/// The [default constructor](Self::default) for [`BodySizeLimit`].
pub fn default_constructor() -> Constructor {
Constructor::new(
f!(<super::BodySizeLimit as std::default::Default>::default),
Lifecycle::RequestScoped,
)
Constructor::request_scoped(f!(<super::BodySizeLimit as std::default::Default>::default))
}
}

Expand Down
4 changes: 2 additions & 2 deletions libs/pavex/src/request/path/path_params.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::Deserialize;

use crate::blueprint::constructor::{Constructor, Lifecycle, RegisteredConstructor};
use crate::blueprint::constructor::{Constructor, RegisteredConstructor};
use crate::blueprint::Blueprint;
use crate::f;
use crate::request::path::deserializer::PathDeserializer;
Expand Down Expand Up @@ -102,7 +102,7 @@ impl PathParams<()> {
/// and [error handler](ExtractPathParamsError::into_response)
/// for [`PathParams`].
pub fn default_constructor() -> Constructor {
Constructor::new(f!(super::PathParams::extract), Lifecycle::RequestScoped)
Constructor::request_scoped(f!(super::PathParams::extract))
.error_handler(f!(super::errors::ExtractPathParamsError::into_response))
}
}
4 changes: 2 additions & 2 deletions libs/pavex/src/request/query/query_params.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::blueprint::constructor::{Constructor, Lifecycle, RegisteredConstructor};
use crate::blueprint::constructor::{Constructor, RegisteredConstructor};
use crate::blueprint::Blueprint;
use crate::f;
use crate::request::RequestHead;
Expand Down Expand Up @@ -67,7 +67,7 @@ impl QueryParams<()> {
/// and [error handler](ExtractQueryParamsError::into_response)
/// for [`QueryParams`].
pub fn default_constructor() -> Constructor {
Constructor::new(f!(super::QueryParams::extract), Lifecycle::RequestScoped)
Constructor::request_scoped(f!(super::QueryParams::extract))
.error_handler(f!(super::errors::ExtractQueryParamsError::into_response))
}
}
Expand Down
7 changes: 2 additions & 5 deletions libs/pavex/src/telemetry/server_request_id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::blueprint::constructor::{Constructor, Lifecycle, RegisteredConstructor};
use crate::blueprint::constructor::{Constructor, RegisteredConstructor};
use crate::blueprint::Blueprint;
use crate::f;
use crate::http::HeaderValue;
Expand Down Expand Up @@ -70,9 +70,6 @@ impl ServerRequestId {

/// The [default constructor](Self::generate) for [`ServerRequestId`].
pub fn default_constructor() -> Constructor {
Constructor::new(
f!(pavex::telemetry::ServerRequestId::generate),
Lifecycle::RequestScoped,
)
Constructor::request_scoped(f!(super::ServerRequestId::generate))
}
}
6 changes: 6 additions & 0 deletions libs/pavexc/src/diagnostic/registration_locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,18 @@ pub(crate) fn get_f_macro_invocation_span(
2
}
("Constructor", "new")
| ("Constructor", "request_scoped")
| ("Constructor", "transient")
| ("Constructor", "singleton")
| ("WrappingMiddleware", "new")
| ("PreProcessingMiddleware", "new")
| ("PostProcessingMiddleware", "new")
| ("ErrorObserver", "new")
| ("Fallback", "new") => {
// Constructor::new(constructor, lifecycle)
// Constructor::request_scoped(constructor)
// Constructor::transient(constructor)
// Constructor::singleton(constructor)
// WrappingMiddleware::new(mw)
// PreProcessingMiddleware::new(mw)
// PostProcessingMiddleware::new(mw)
Expand Down

0 comments on commit 092762a

Please sign in to comment.