Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

https://github.com/oxidecomputer/dropshot/compare/v0.16.5\...HEAD[Full list of commits]

* https://github.com/oxidecomputer/dropshot/pull/1490[#1490] Added `map` and `try_map` to `Query`, `Path`, `Header`, `HttpResponseOk`, `HttpResponseCreated`, `HttpResponseAccepted`, and `HttpResponseHeaders`, similar to `map` and `try_map` that already exist on `TypedBody`.

== 0.16.5 (released 2025-11-20)

https://github.com/oxidecomputer/dropshot/compare/v0.16.4\...v0.16.5[Full list of commits]
Expand Down
4 changes: 2 additions & 2 deletions dropshot/src/extractor/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<BodyType: JsonSchema + DeserializeOwned + Send + Sync>
pub fn map<T, F>(self, f: F) -> TypedBody<T>
where
T: JsonSchema + DeserializeOwned + Send + Sync,
F: Fn(BodyType) -> T,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this to be Fn since it's called just once -- FnOnce is more general.

F: FnOnce(BodyType) -> T,
{
TypedBody { inner: f(self.inner) }
}
Expand All @@ -60,7 +60,7 @@ impl<BodyType: JsonSchema + DeserializeOwned + Send + Sync>
pub fn try_map<T, E, F>(self, f: F) -> Result<TypedBody<T>, E>
where
T: JsonSchema + DeserializeOwned + Send + Sync,
F: Fn(BodyType) -> Result<T, E>,
F: FnOnce(BodyType) -> Result<T, E>,
{
Ok(TypedBody { inner: f(self.inner)? })
}
Expand Down
20 changes: 20 additions & 0 deletions dropshot/src/extractor/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ impl<HeaderType: DeserializeOwned + JsonSchema + Send + Sync>
pub fn into_inner(self) -> HeaderType {
self.inner
}

/// Convert this `Header` into one with a different type parameter; this
/// may be useful when multiple, related endpoints take header parameters
/// that are similar and convertible into a common type.
pub fn map<T, F>(self, f: F) -> Header<T>
where
T: DeserializeOwned + JsonSchema + Send + Sync,
F: FnOnce(HeaderType) -> T,
{
Header { inner: f(self.inner) }
}

/// Similar to [`Header::map`] but with support for fallibility.
pub fn try_map<T, E, F>(self, f: F) -> Result<Header<T>, E>
where
T: DeserializeOwned + JsonSchema + Send + Sync,
F: FnOnce(HeaderType) -> Result<T, E>,
{
Ok(Header { inner: f(self.inner)? })
}
}

/// Given an HTTP request, pull out the headers and attempt to deserialize
Expand Down
20 changes: 20 additions & 0 deletions dropshot/src/extractor/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ impl<PathType: JsonSchema + Send + Sync> Path<PathType> {
pub fn into_inner(self) -> PathType {
self.inner
}

/// Convert this `Path` into one with a different type parameter; this
/// may be useful when multiple, related endpoints take path parameters that
/// are similar and convertible into a common type.
pub fn map<T, F>(self, f: F) -> Path<T>
where
T: JsonSchema + Send + Sync,
F: FnOnce(PathType) -> T,
{
Path { inner: f(self.inner) }
}

/// Similar to [`Path::map`] but with support for fallibility.
pub fn try_map<T, E, F>(self, f: F) -> Result<Path<T>, E>
where
T: JsonSchema + Send + Sync,
F: FnOnce(PathType) -> Result<T, E>,
{
Ok(Path { inner: f(self.inner)? })
}
}

// The `SharedExtractor` implementation for Path<PathType> describes how to
Expand Down
20 changes: 20 additions & 0 deletions dropshot/src/extractor/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ impl<QueryType: DeserializeOwned + JsonSchema + Send + Sync> Query<QueryType> {
pub fn into_inner(self) -> QueryType {
self.inner
}

/// Convert this `Query` into one with a different type parameter; this
/// may be useful when multiple, related endpoints take query parameters
/// that are similar and convertible into a common type.
pub fn map<T, F>(self, f: F) -> Query<T>
where
T: DeserializeOwned + JsonSchema + Send + Sync,
F: FnOnce(QueryType) -> T,
{
Query { inner: f(self.inner) }
}

/// Similar to [`Query::map`] but with support for fallibility.
pub fn try_map<T, E, F>(self, f: F) -> Result<Query<T>, E>
where
T: DeserializeOwned + JsonSchema + Send + Sync,
F: FnOnce(QueryType) -> Result<T, E>,
{
Ok(Query { inner: f(self.inner)? })
}
}

/// Given an HTTP request, pull out the query string and attempt to deserialize
Expand Down
100 changes: 100 additions & 0 deletions dropshot/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,30 @@ where
pub struct HttpResponseCreated<T: HttpResponseContent + Send + Sync + 'static>(
pub T,
);

impl<T: HttpResponseContent + Send + Sync + 'static> HttpResponseCreated<T> {
/// Convert this response into one with a different type parameter; this
/// may be useful when multiple, related endpoints return similar response
/// types that are convertible into a common type.
pub fn map<U, F>(self, f: F) -> HttpResponseCreated<U>
where
U: HttpResponseContent + Send + Sync + 'static,
F: FnOnce(T) -> U,
{
HttpResponseCreated(f(self.0))
}

/// Similar to [`HttpResponseCreated::map`] but with support for
/// fallibility.
pub fn try_map<U, E, F>(self, f: F) -> Result<HttpResponseCreated<U>, E>
where
U: HttpResponseContent + Send + Sync + 'static,
F: FnOnce(T) -> Result<U, E>,
{
Ok(HttpResponseCreated(f(self.0)?))
}
}

impl<T: HttpResponseContent + Send + Sync + 'static> HttpCodedResponse
for HttpResponseCreated<T>
{
Expand All @@ -1099,6 +1123,30 @@ impl<T: HttpResponseContent + Send + Sync + 'static>
pub struct HttpResponseAccepted<T: HttpResponseContent + Send + Sync + 'static>(
pub T,
);

impl<T: HttpResponseContent + Send + Sync + 'static> HttpResponseAccepted<T> {
/// Convert this response into one with a different type parameter; this
/// may be useful when multiple, related endpoints return similar response
/// types that are convertible into a common type.
pub fn map<U, F>(self, f: F) -> HttpResponseAccepted<U>
where
U: HttpResponseContent + Send + Sync + 'static,
F: FnOnce(T) -> U,
{
HttpResponseAccepted(f(self.0))
}

/// Similar to [`HttpResponseAccepted::map`] but with support for
/// fallibility.
pub fn try_map<U, E, F>(self, f: F) -> Result<HttpResponseAccepted<U>, E>
where
U: HttpResponseContent + Send + Sync + 'static,
F: FnOnce(T) -> Result<U, E>,
{
Ok(HttpResponseAccepted(f(self.0)?))
}
}

impl<T: HttpResponseContent + Send + Sync + 'static> HttpCodedResponse
for HttpResponseAccepted<T>
{
Expand All @@ -1120,6 +1168,29 @@ impl<T: HttpResponseContent + Send + Sync + 'static>
pub struct HttpResponseOk<T: HttpResponseContent + Send + Sync + 'static>(
pub T,
);

impl<T: HttpResponseContent + Send + Sync + 'static> HttpResponseOk<T> {
/// Convert this response into one with a different type parameter; this
/// may be useful when multiple, related endpoints return similar response
/// types that are convertible into a common type.
pub fn map<U, F>(self, f: F) -> HttpResponseOk<U>
where
U: HttpResponseContent + Send + Sync + 'static,
F: FnOnce(T) -> U,
{
HttpResponseOk(f(self.0))
}

/// Similar to [`HttpResponseOk::map`] but with support for fallibility.
pub fn try_map<U, E, F>(self, f: F) -> Result<HttpResponseOk<U>, E>
where
U: HttpResponseContent + Send + Sync + 'static,
F: FnOnce(T) -> Result<U, E>,
{
Ok(HttpResponseOk(f(self.0)?))
}
}

impl<T: HttpResponseContent + Send + Sync + 'static> HttpCodedResponse
for HttpResponseOk<T>
{
Expand Down Expand Up @@ -1362,6 +1433,35 @@ impl<
pub fn headers_mut(&mut self) -> &mut HeaderMap {
&mut self.other_headers
}

/// Convert this response into one with a different body type parameter;
/// this may be useful when multiple, related endpoints return similar
/// response types that are convertible into a common type.
pub fn map<U, F>(self, f: F) -> HttpResponseHeaders<U, H>
where
U: HttpCodedResponse,
F: FnOnce(T) -> U,
{
HttpResponseHeaders {
body: f(self.body),
structured_headers: self.structured_headers,
other_headers: self.other_headers,
}
}

/// Similar to [`HttpResponseHeaders::map`] but with support for
/// fallibility.
pub fn try_map<U, E, F>(self, f: F) -> Result<HttpResponseHeaders<U, H>, E>
where
U: HttpCodedResponse,
F: FnOnce(T) -> Result<U, E>,
{
Ok(HttpResponseHeaders {
body: f(self.body)?,
structured_headers: self.structured_headers,
other_headers: self.other_headers,
})
}
}
impl<
T: HttpCodedResponse,
Expand Down
Loading