Skip to content

Commit

Permalink
Restrict FromContext Result to mendes::Error
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Dec 4, 2023
1 parent 65c7ece commit 043089b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
34 changes: 17 additions & 17 deletions mendes/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub trait Application: Send + Sized {

async fn handle(cx: Context<Self>) -> Response<Self::ResponseBody>;

fn from_query<'a, T: serde::Deserialize<'a>>(req: &'a Parts) -> Result<T, Self::Error> {
fn from_query<'a, T: serde::Deserialize<'a>>(req: &'a Parts) -> Result<T, Error> {
let query = req.uri.query().ok_or(Error::QueryMissing)?;
let data =
serde_urlencoded::from_bytes::<T>(query.as_bytes()).map_err(Error::QueryDecode)?;
Expand Down Expand Up @@ -244,7 +244,7 @@ where
req: &'a Parts,
state: &mut PathState,
body: &mut Option<A::RequestBody>,
) -> Result<Self, A::Error>;
) -> Result<Self, Error>;
}

macro_rules! from_context_from_str {
Expand All @@ -255,10 +255,10 @@ macro_rules! from_context_from_str {
req: &'a Parts,
state: &mut PathState,
_: &mut Option<A::RequestBody>,
) -> Result<Self, A::Error> {
) -> Result<Self, Error> {
let s = state
.next(req.uri.path())
.ok_or(Error::PathComponentMissing.into())?;
.ok_or(Error::PathComponentMissing)?;
<$self>::from_str(s).map_err(|_| Error::PathParse.into())
}
}
Expand All @@ -269,7 +269,7 @@ macro_rules! from_context_from_str {
req: &'a Parts,
state: &mut PathState,
_: &mut Option<A::RequestBody>,
) -> Result<Self, A::Error> {
) -> Result<Self, Error> {

Check warning on line 272 in mendes/src/application.rs

View check run for this annotation

Codecov / codecov/patch

mendes/src/application.rs#L272

Added line #L272 was not covered by tests
match state.next(req.uri.path()) {
Some(s) => match <$self>::from_str(s) {
Ok(v) => Ok(Some(v)),
Expand All @@ -288,7 +288,7 @@ impl<'a, A: Application> FromContext<'a, A> for &'a A {
_: &'a Parts,
_: &mut PathState,
_: &mut Option<A::RequestBody>,
) -> Result<Self, A::Error> {
) -> Result<Self, Error> {
Ok(app)
}
}
Expand All @@ -299,7 +299,7 @@ impl<'a, A: Application> FromContext<'a, A> for &'a Arc<A> {
_: &'a Parts,
_: &mut PathState,
_: &mut Option<A::RequestBody>,
) -> Result<Self, A::Error> {
) -> Result<Self, Error> {

Check warning on line 302 in mendes/src/application.rs

View check run for this annotation

Codecov / codecov/patch

mendes/src/application.rs#L302

Added line #L302 was not covered by tests
Ok(app)
}
}
Expand All @@ -310,7 +310,7 @@ impl<'a, A: Application> FromContext<'a, A> for &'a http::request::Parts {
req: &'a Parts,
_: &mut PathState,
_: &mut Option<A::RequestBody>,
) -> Result<Self, A::Error> {
) -> Result<Self, Error> {
Ok(req)
}
}
Expand All @@ -321,7 +321,7 @@ impl<'a, A: Application> FromContext<'a, A> for Option<&'a [u8]> {
req: &'a Parts,
state: &mut PathState,
_: &mut Option<A::RequestBody>,
) -> Result<Self, A::Error> {
) -> Result<Self, Error> {

Check warning on line 324 in mendes/src/application.rs

View check run for this annotation

Codecov / codecov/patch

mendes/src/application.rs#L324

Added line #L324 was not covered by tests
Ok(state.next(req.uri.path()).map(|s| s.as_bytes()))
}
}
Expand All @@ -332,7 +332,7 @@ impl<'a, A: Application> FromContext<'a, A> for &'a [u8] {
req: &'a Parts,
state: &mut PathState,
_: &mut Option<A::RequestBody>,
) -> Result<Self, A::Error> {
) -> Result<Self, Error> {

Check warning on line 335 in mendes/src/application.rs

View check run for this annotation

Codecov / codecov/patch

mendes/src/application.rs#L335

Added line #L335 was not covered by tests
state
.next(req.uri.path())
.ok_or_else(|| Error::PathComponentMissing.into())
Expand All @@ -346,7 +346,7 @@ impl<'a, A: Application> FromContext<'a, A> for Option<Cow<'a, str>> {
req: &'a Parts,
state: &mut PathState,
_: &mut Option<A::RequestBody>,
) -> Result<Self, A::Error> {
) -> Result<Self, Error> {

Check warning on line 349 in mendes/src/application.rs

View check run for this annotation

Codecov / codecov/patch

mendes/src/application.rs#L349

Added line #L349 was not covered by tests
Ok(path_str(req, state)?)
}
}
Expand All @@ -357,7 +357,7 @@ impl<'a, A: Application> FromContext<'a, A> for Cow<'a, str> {
req: &'a Parts,
state: &mut PathState,
_: &mut Option<A::RequestBody>,
) -> Result<Self, A::Error> {
) -> Result<Self, Error> {
match path_str(req, state)? {
Some(s) => Ok(s),
None => Err(Error::PathComponentMissing.into()),
Expand All @@ -371,7 +371,7 @@ impl<'a, A: Application> FromContext<'a, A> for Option<String> {
req: &'a Parts,
state: &mut PathState,
_: &mut Option<A::RequestBody>,
) -> Result<Self, A::Error> {
) -> Result<Self, Error> {

Check warning on line 374 in mendes/src/application.rs

View check run for this annotation

Codecov / codecov/patch

mendes/src/application.rs#L374

Added line #L374 was not covered by tests
Ok(path_str(req, state)?.map(|s| s.into_owned()))
}
}
Expand All @@ -382,7 +382,7 @@ impl<'a, A: Application> FromContext<'a, A> for String {
req: &'a Parts,
state: &mut PathState,
_: &mut Option<A::RequestBody>,
) -> Result<Self, A::Error> {
) -> Result<Self, Error> {
match path_str(req, state)? {
Some(s) => Ok(s.into_owned()),
None => Err(Error::PathComponentMissing.into()),
Expand Down Expand Up @@ -454,7 +454,7 @@ impl<'a, A: Application> FromContext<'a, A> for Rest<&'a [u8]> {
req: &'a Parts,
state: &mut PathState,
_: &mut Option<A::RequestBody>,
) -> Result<Self, A::Error> {
) -> Result<Self, Error> {

Check warning on line 457 in mendes/src/application.rs

View check run for this annotation

Codecov / codecov/patch

mendes/src/application.rs#L457

Added line #L457 was not covered by tests
Ok(Rest(state.rest(req.uri.path()).as_bytes()))
}
}
Expand All @@ -465,7 +465,7 @@ impl<'a, A: Application> FromContext<'a, A> for Rest<Cow<'a, str>> {
req: &'a Parts,
state: &mut PathState,
_: &mut Option<A::RequestBody>,
) -> Result<Self, A::Error> {
) -> Result<Self, Error> {
Ok(Rest(
percent_decode_str(state.rest(req.uri.path()))
.decode_utf8()
Expand All @@ -486,7 +486,7 @@ where
req: &'a Parts,
_: &mut PathState,
_: &mut Option<A::RequestBody>,
) -> Result<Self, A::Error> {
) -> Result<Self, Error> {
A::from_query(req).map(Query)
}
}
Expand Down
6 changes: 3 additions & 3 deletions mendes/src/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use hyper::server::conn::AddrStream;
use hyper::service::Service;

use super::Application;
use crate::application::{Context, FromContext, PathState};
use crate::application::{Context, Error, FromContext, PathState};

pub use hyper::Body;

Expand Down Expand Up @@ -107,7 +107,7 @@ where
_: &'a Parts,
_: &mut PathState,
body: &mut Option<Body>,
) -> Result<Self, A::Error> {
) -> Result<Self, Error> {
match body.take() {
Some(body) => Ok(body),
None => panic!("attempted to retrieve body twice"),
Expand All @@ -124,7 +124,7 @@ where
req: &'a Parts,
_: &mut PathState,
_: &mut Option<Body>,
) -> Result<Self, A::Error> {
) -> Result<Self, Error> {
// This is safe because we insert ClientAddr into the request extensions
// unconditionally in the ConnectionService::call method.
Ok(req.extensions.get::<ClientAddr>().copied().unwrap())
Expand Down

0 comments on commit 043089b

Please sign in to comment.