From baa99e5084c6f2deff4cd8303342866af2e8b9ec Mon Sep 17 00:00:00 2001 From: David Pedersen <david.pdrsn@gmail.com> Date: Mon, 16 Aug 2021 20:55:22 +0200 Subject: [PATCH] Make `RequestParts::{new, try_into_request}` public (#194) Fixes https://github.com/tokio-rs/axum/issues/147 --- CHANGELOG.md | 3 ++- src/extract/mod.rs | 25 +++++++++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8e6981e..c829056d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add `NestedUri` for extracting request URI in nested services ([#161](https://github.com/tokio-rs/axum/pull/161)) - Implement `FromRequest` for `http::Extensions` - Implement SSE as an `IntoResponse` instead of a service ([#98](https://github.com/tokio-rs/axum/pull/98)) -- Add `Redirect` response. +- Add `Redirect` response. ([#192](https://github.com/tokio-rs/axum/pull/192)) +- Make `RequestParts::{new, try_into_request}` public ([#194](https://github.com/tokio-rs/axum/pull/194)) ## Breaking changes diff --git a/src/extract/mod.rs b/src/extract/mod.rs index bec4954a..0308f183 100644 --- a/src/extract/mod.rs +++ b/src/extract/mod.rs @@ -374,7 +374,12 @@ pub struct RequestParts<B = crate::body::Body> { } impl<B> RequestParts<B> { - pub(crate) fn new(req: Request<B>) -> Self { + /// Create a new `RequestParts`. + /// + /// You generally shouldn't need to construct this type yourself, unless + /// using extractors outside of axum for example to implement a + /// [`tower::Service`]. + pub fn new(req: Request<B>) -> Self { let ( http::request::Parts { method, @@ -397,9 +402,21 @@ impl<B> RequestParts<B> { } } - // this method uses `Error` since we might make this method public one day and then - // `Error` is more flexible. - pub(crate) fn try_into_request(self) -> Result<Request<B>, Error> { + /// Convert this `RequestParts` back into a [`Request`]. + /// + /// Fails if + /// + /// - The full [`HeaderMap`] has been extracted, that is [`take_headers`] + /// have been called. + /// - The full [`Extensions`] has been extracted, that is + /// [`take_extensions`] have been called. + /// - The request body has been extracted, that is [`take_body`] have been + /// called. + /// + /// [`take_headers`]: RequestParts::take_headers + /// [`take_extensions`]: RequestParts::take_extensions + /// [`take_body`]: RequestParts::take_body + pub fn try_into_request(self) -> Result<Request<B>, Error> { let Self { method, uri,