Thanks to visit codestin.com
Credit goes to github.com

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
36 changes: 30 additions & 6 deletions ohkami/src/handle/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ pub struct Path<T>(pub T);
/// ### required
/// - `type Error: IntoResponse`
/// - `fn from_param(param: Cow<'p, str>) -> Result<Self, Self::Error>`
///
/// ### default impls
/// - `String` ... own a param as it is, percent decoded
/// - `&str` ... borrow a param, only accepting non-encoded one, rejecting if percent encoded
/// - `Cow<'_, str>` ... own decoded if percent encoded, or borrow if not encoded
/// - `uuid::Uuid` ... parsed from a percent-decoded param
/// - primitive integers ... parsed from a percent-decoded param
pub trait FromParam<'p>: bound::Schema + Sized {
/// If this extraction never fails, `std::convert::Infallible` is recomended.
type Error: IntoResponse;
Expand Down Expand Up @@ -202,7 +209,8 @@ const _: () = {
ErrorMessage(format!(
"Unexpected path params `{param}`: percent encoded"
))
} unexpected(&param)
}
unexpected(&param)
}),
}
}
Expand All @@ -212,14 +220,22 @@ const _: () = {
($( $unsigned_int:ty ),*) => {
$(
impl<'p> FromParam<'p> for $unsigned_int {
type Error = ErrorMessage;
type Error = Response;

fn from_param(param: Cow<'p, str>) -> Result<Self, Self::Error> {
::byte_reader::Reader::new(param.as_bytes())
.read_uint()
.map(|i| Self::try_from(i).ok())
.flatten()
.ok_or_else(|| ErrorMessage(format!("Unexpected path param")))
.ok_or_else(|| {
#[cfg(debug_assertions)] {
crate::WARNING!(
"Failed to parse `{}` from path param `{}`",
stringify!($unsigned_int), param
);
}
Response::BadRequest().with_text("invalid path")
})
}
}
)*
Expand All @@ -231,14 +247,22 @@ const _: () = {
($( $signed_int:ty ),*) => {
$(
impl<'p> FromParam<'p> for $signed_int {
type Error = ErrorMessage;
type Error = Response;

fn from_param(param: Cow<'p, str>) -> Result<Self, Self::Error> {
::byte_reader::Reader::new(param.as_bytes())
.read_int()
.map(|i| Self::try_from(i).ok())
.flatten()
.ok_or_else(|| ErrorMessage(format!("Unexpected path param")))
.ok_or_else(|| {
#[cfg(debug_assertions)] {
crate::WARNING!(
"Failed to parse `{}` from path param `{}`",
stringify!($signed_int), param
);
}
Response::BadRequest().with_text("invalid path")
})
}
}
)*
Expand All @@ -256,7 +280,7 @@ const _: () = {
"Failed to parse UUID from path param `{param}`",
);
}
Response::BadRequest().with_text("unexpected path")
Response::BadRequest().with_text("invalid path")
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions ohkami/src/request/from_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::openapi;
/// "Retirieved from a `Request`".
///
/// ### required
/// - `type Errpr`
/// - `fn from_request`
/// - `type Errpr: IntoResponse`
/// - `fn from_request(req: &Request) -> Option<Result<Self, Self::Error>>`
///
/// Of course, you can manually implement for your structs that can be extracted from a request:
///
Expand Down