Skip to content

Commit 57c1b90

Browse files
feat: Add #[derive(FromRequest)] macro
Minimize the amount of boilerplate that users have to write in order to extract information out of the incoming request.
1 parent e728b32 commit 57c1b90

28 files changed

+820
-2
lines changed

runtime/pavex/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ pub mod time {
4545
}
4646
pub mod tls;
4747

48+
pub use pavex_macros::FromRequest;
49+
4850
/// Define a [prebuilt type](https://pavex.dev/docs/guide/dependency_injection/prebuilt_types/).
4951
///
5052
/// Prebuilt types are constructed outside of Pavex's dependency injection framework

runtime/pavex/src/request/error.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pub struct FromRequestError(crate::Error);
2+
3+
impl FromRequestError {
4+
pub fn new(e: crate::Error) -> Self {
5+
Self(e)
6+
}
7+
}

runtime/pavex/src/request/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
//!
55
//! Check out [the guide](https://pavex.dev/docs/guide/request_data/)
66
//! for a thorough introduction to request-based data extractors.
7+
pub use error::FromRequestError;
78
pub use request_head::RequestHead;
89

910
pub mod body;
11+
mod error;
1012
pub mod path;
1113
pub mod query;
1214
mod request_head;

runtime/pavex/src/request/path/path_params.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl<T> PathParams<T> {
5252
/// If the extraction fails, an [`ExtractPathParamsError`] is returned.
5353
#[request_scoped(pavex = crate, id = "PATH_PARAMS_EXTRACT")]
5454
pub fn extract<'server, 'request>(
55-
params: RawPathParams<'server, 'request>,
55+
params: &'request RawPathParams<'server, 'request>,
5656
) -> Result<Self, ExtractPathParamsError>
5757
where
5858
T: Deserialize<'request>,

runtime/pavex/src/request/path/raw_path_params.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ impl<'server, 'request> Iterator for RawPathParamsIter<'_, 'server, 'request> {
7777
}
7878
}
7979

80+
impl<'server, 'request> ExactSizeIterator for RawPathParamsIter<'_, 'server, 'request> {
81+
fn len(&self) -> usize {
82+
self.0.len()
83+
}
84+
}
85+
8086
/// A wrapper around a percent-encoded path parameter, obtained via [`RawPathParams`].
8187
///
8288
/// Use [`decode`](Self::decode) to extract the percent-encoded value.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Testing
2+
3+
`pavex_macros` relies on [`trybuild`](https://github.com/dtolnay/trybuild) to verify that Pavex macros:
4+
5+
- Accept the expected input/parameter combinations
6+
- Reject invalid input/parameter combinations
7+
8+
In the latter case, we also verify the returned error message to ensure that
9+
diagnostics are accurate and actionable.
10+
11+
## Test filtering
12+
13+
`trybuild` is a custom test runner and it doesn't obey all `cargo test` built-in options.
14+
15+
For filtering, in particular, use
16+
17+
```bash
18+
cargo t -- ui trybuild=not_a_function.rs
19+
```
20+
21+
to run a subset of the whole UI test suite.
22+
The `trybuild` argument doesn't have to be the filename: you can only specify a fragment of the path (e.g. `from_request`) to run
23+
all tests in a specific sub-directory.
24+
25+
## Overwriting saved snapshots
26+
27+
To overwrite saved snapshots, set the `TRYBUILD=overwrite` environment variable:
28+
29+
```bash
30+
# Overwrite all saved snapshots, if changed
31+
TRYBUILD=overwrite cargo t
32+
# Overwrite snapshots for a subset of tests
33+
TRYBUILD=overwrite cargo t -- ui trybuild=from_request
34+
```

0 commit comments

Comments
 (0)