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
80 changes: 54 additions & 26 deletions ohkami/src/fang/handler/into_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::openapi;


pub trait IntoHandler<T> {
fn n_params(&self) -> usize;
fn into_handler(self) -> Handler;
}

Expand All @@ -33,6 +34,8 @@ const _: (/* no args */) = {
Body: IntoResponse,
Fut: Future<Output = Body> + SendOnNative + 'static,
{
fn n_params(&self) -> usize {0}

fn into_handler(self) -> Handler {
Handler::new(move |_| {
let res = self();
Expand All @@ -53,6 +56,8 @@ const _: (/* FromParam */) = {
Body: IntoResponse,
Fut: Future<Output = Body> + SendOnNative + 'static,
{
fn n_params(&self) -> usize {1}

fn into_handler(self) -> Handler {
Handler::new(move |req| {
match P1::from_raw_param(unsafe {req.path.assume_one_param()}) {
Expand All @@ -75,10 +80,11 @@ const _: (/* FromParam */) = {
Body: IntoResponse,
Fut: Future<Output = Body> + SendOnNative + 'static,
{
fn n_params(&self) -> usize {1}

fn into_handler(self) -> Handler {
Handler::new(move |req| {
// SAFETY: Due to the architecture of `Router`,
// `params` has already `append`ed once before this code
// SAFETY: `crate::Route` has already checked the number of params
match P1::from_raw_param(unsafe {req.path.assume_one_param()}) {
Ok(p1) => {
let res = self((p1,));
Expand All @@ -98,6 +104,8 @@ const _: (/* FromParam */) = {
F: Fn((P1, P2)) -> Fut + SendSyncOnNative + 'static,
Fut: Future<Output = Body> + SendOnNative + 'static,
{
fn n_params(&self) -> usize {2}

fn into_handler(self) -> Handler {
Handler::new(move |req| {
let (p1, p2) = unsafe {req.path.assume_two_params()};
Expand All @@ -123,6 +131,8 @@ const _: (/* FromRequest items */) = {
F: Fn(Item1) -> Fut + SendSyncOnNative + 'static,
Fut: Future<Output = Body> + SendOnNative + 'static,
{
fn n_params(&self) -> usize {0}

fn into_handler(self) -> Handler {
Handler::new(move |req| {
match from_request::<Item1>(req) {
Expand All @@ -144,6 +154,8 @@ const _: (/* FromRequest items */) = {
F: Fn(Item1, Item2) -> Fut + SendSyncOnNative + 'static,
Fut: Future<Output = Body> + SendOnNative + 'static,
{
fn n_params(&self) -> usize {0}

fn into_handler(self) -> Handler {
Handler::new(move |req| {
match (from_request::<Item1>(req), from_request::<Item2>(req)) {
Expand All @@ -167,6 +179,8 @@ const _: (/* FromRequest items */) = {
F: Fn(Item1, Item2, Item3) -> Fut + SendSyncOnNative + 'static,
Fut: Future<Output = Body> + SendOnNative + 'static,
{
fn n_params(&self) -> usize {0}

fn into_handler(self) -> Handler {
Handler::new(move |req| {
match (from_request::<Item1>(req), from_request::<Item2>(req), from_request::<Item3>(req)) {
Expand All @@ -192,6 +206,8 @@ const _: (/* FromRequest items */) = {
F: Fn(Item1, Item2, Item3, Item4) -> Fut + SendSyncOnNative + 'static,
Fut: Future<Output = Body> + SendOnNative + 'static,
{
fn n_params(&self) -> usize {0}

fn into_handler(self) -> Handler {
Handler::new(move |req| {
match (from_request::<Item1>(req), from_request::<Item2>(req), from_request::<Item3>(req), from_request::<Item4>(req)) {
Expand Down Expand Up @@ -221,10 +237,11 @@ const _: (/* one FromParam without tuple and FromRequest items */) = {
F: Fn(P1, Item1) -> Fut + SendSyncOnNative + 'static,
Fut: Future<Output = Body> + SendOnNative + 'static,
{
fn n_params(&self) -> usize {1}

fn into_handler(self) -> Handler {
Handler::new(move |req| {
// SAFETY: Due to the architecture of `Router`,
// `params` has already `append`ed once before this code
// SAFETY: `crate::Route` has already checked the number of params
let p1 = unsafe {req.path.assume_one_param()};

match (P1::from_raw_param(p1), from_request(req)) {
Expand All @@ -248,10 +265,11 @@ const _: (/* one FromParam without tuple and FromRequest items */) = {
F: Fn(P1, Item1, Item2) -> Fut + SendSyncOnNative + 'static,
Fut: Future<Output = Body> + SendOnNative + 'static,
{
fn n_params(&self) -> usize {1}

fn into_handler(self) -> Handler {
Handler::new(move |req| {
// SAFETY: Due to the architecture of `Router`,
// `params` has already `append`ed once before this code
// SAFETY: `crate::Route` has already checked the number of params
let p1 = unsafe {req.path.assume_one_param()};

match (P1::from_raw_param(p1), from_request::<Item1>(req), from_request::<Item2>(req)) {
Expand All @@ -277,10 +295,11 @@ const _: (/* one FromParam without tuple and FromRequest items */) = {
F: Fn(P1, Item1, Item2, Item3) -> Fut + SendSyncOnNative + 'static,
Fut: Future<Output = Body> + SendOnNative + 'static,
{
fn n_params(&self) -> usize {1}

fn into_handler(self) -> Handler {
Handler::new(move |req| {
// SAFETY: Due to the architecture of `Router`,
// `params` has already `append`ed once before this code
// SAFETY: `crate::Route` has already checked the number of params
let p1 = unsafe {req.path.assume_one_param()};

match (P1::from_raw_param(p1), from_request::<Item1>(req), from_request::<Item2>(req), from_request::<Item3>(req)) {
Expand Down Expand Up @@ -308,10 +327,11 @@ const _: (/* one FromParam without tuple and FromRequest items */) = {
F: Fn(P1, Item1, Item2, Item3, Item4) -> Fut + SendSyncOnNative + 'static,
Fut: Future<Output = Body> + SendOnNative + 'static,
{
fn n_params(&self) -> usize {1}

fn into_handler(self) -> Handler {
Handler::new(move |req| {
// SAFETY: Due to the architecture of `Router`,
// `params` has already `append`ed once before this code
// SAFETY: `crate::Route` has already checked the number of params
let p1 = unsafe {req.path.assume_one_param()};

match (P1::from_raw_param(p1), from_request::<Item1>(req), from_request::<Item2>(req), from_request::<Item3>(req), from_request::<Item4>(req)) {
Expand Down Expand Up @@ -343,10 +363,11 @@ const _: (/* one FromParam and FromRequest items */) = {
F: Fn((P1,), Item1) -> Fut + SendSyncOnNative + 'static,
Fut: Future<Output = Body> + SendOnNative + 'static,
{
fn n_params(&self) -> usize {1}

fn into_handler(self) -> Handler {
Handler::new(move |req| {
// SAFETY: Due to the architecture of `Router`,
// `params` has already `append`ed once before this code
// SAFETY: `crate::Route` has already checked the number of params
let p1 = unsafe {req.path.assume_one_param()};

match (P1::from_raw_param(p1), from_request::<Item1>(req)) {
Expand All @@ -370,10 +391,11 @@ const _: (/* one FromParam and FromRequest items */) = {
F: Fn((P1,), Item1, Item2) -> Fut + SendSyncOnNative + 'static,
Fut: Future<Output = Body> + SendOnNative + 'static,
{
fn n_params(&self) -> usize {1}

fn into_handler(self) -> Handler {
Handler::new(move |req| {
// SAFETY: Due to the architecture of `Router`,
// `params` has already `append`ed once before this code
// SAFETY: `crate::Route` has already checked the number of params
let p1 = unsafe {req.path.assume_one_param()};

match (P1::from_raw_param(p1), from_request::<Item1>(req), from_request::<Item2>(req)) {
Expand All @@ -399,10 +421,11 @@ const _: (/* one FromParam and FromRequest items */) = {
F: Fn((P1,), Item1, Item2, Item3) -> Fut + SendSyncOnNative + 'static,
Fut: Future<Output = Body> + SendOnNative + 'static,
{
fn n_params(&self) -> usize {1}

fn into_handler(self) -> Handler {
Handler::new(move |req| {
// SAFETY: Due to the architecture of `Router`,
// `params` has already `append`ed once before this code
// SAFETY: `crate::Route` has already checked the number of params
let p1 = unsafe {req.path.assume_one_param()};

match (P1::from_raw_param(p1), from_request::<Item1>(req), from_request::<Item2>(req), from_request::<Item3>(req)) {
Expand Down Expand Up @@ -430,10 +453,11 @@ const _: (/* one FromParam and FromRequest items */) = {
F: Fn((P1,), Item1, Item2, Item3, Item4) -> Fut + SendSyncOnNative + 'static,
Fut: Future<Output = Body> + SendOnNative + 'static,
{
fn n_params(&self) -> usize {1}

fn into_handler(self) -> Handler {
Handler::new(move |req| {
// SAFETY: Due to the architecture of `Router`,
// `params` has already `append`ed once before this code
// SAFETY: `crate::Route` has already checked the number of params
let p1 = unsafe {req.path.assume_one_param()};

match (P1::from_raw_param(p1), from_request::<Item1>(req), from_request::<Item2>(req), from_request::<Item3>(req), from_request::<Item4>(req)) {
Expand Down Expand Up @@ -465,10 +489,11 @@ const _: (/* two PathParams and FromRequest items */) = {
F: Fn((P1, P2), Item1) -> Fut + SendSyncOnNative + 'static,
Fut: Future<Output = Body> + SendOnNative + 'static,
{
fn n_params(&self) -> usize {2}

fn into_handler(self) -> Handler {
Handler::new(move |req| {
// SAFETY: Due to the architecture of `Router`,
// `params` has already `append`ed twice before this code
// SAFETY: `crate::Route` has already checked the number of params
let (p1, p2) = unsafe {req.path.assume_two_params()};

match (FromParam::from_raw_param(p1), FromParam::from_raw_param(p2), from_request::<Item1>(req)) {
Expand All @@ -494,10 +519,11 @@ const _: (/* two PathParams and FromRequest items */) = {
F: Fn((P1, P2), Item1, Item2) -> Fut + SendSyncOnNative + 'static,
Fut: Future<Output = Body> + SendOnNative + 'static,
{
fn n_params(&self) -> usize {2}

fn into_handler(self) -> Handler {
Handler::new(move |req| {
// SAFETY: Due to the architecture of `Router`,
// `params` has already `append`ed twice before this code
// SAFETY: `crate::Route` has already checked the number of params
let (p1, p2) = unsafe {req.path.assume_two_params()};

match (FromParam::from_raw_param(p1), FromParam::from_raw_param(p2), from_request::<Item1>(req), from_request::<Item2>(req)) {
Expand Down Expand Up @@ -525,10 +551,11 @@ const _: (/* two PathParams and FromRequest items */) = {
F: Fn((P1, P2), Item1, Item2, Item3) -> Fut + SendSyncOnNative + 'static,
Fut: Future<Output = Body> + SendOnNative + 'static,
{
fn n_params(&self) -> usize {2}

fn into_handler(self) -> Handler {
Handler::new(move |req| {
// SAFETY: Due to the architecture of `Router`,
// `params` has already `append`ed twice before this code
// SAFETY: `crate::Route` has already checked the number of params
let (p1, p2) = unsafe {req.path.assume_two_params()};

match (FromParam::from_raw_param(p1), FromParam::from_raw_param(p2), from_request::<Item1>(req), from_request::<Item2>(req), from_request::<Item3>(req)) {
Expand Down Expand Up @@ -558,10 +585,11 @@ const _: (/* two PathParams and FromRequest items */) = {
F: Fn((P1, P2), Item1, Item2, Item3, Item4) -> Fut + SendSyncOnNative + 'static,
Fut: Future<Output = Body> + SendOnNative + 'static,
{
fn n_params(&self) -> usize {2}

fn into_handler(self) -> Handler {
Handler::new(move |req| {
// SAFETY: Due to the architecture of `Router`,
// `params` has already `append`ed twice before this code
// SAFETY: `crate::Route` has already checked the number of params
let (p1, p2) = unsafe {req.path.assume_two_params()};

match (FromParam::from_raw_param(p1), FromParam::from_raw_param(p2), from_request::<Item1>(req), from_request::<Item2>(req), from_request::<Item3>(req), from_request::<Item4>(req)) {
Expand Down
8 changes: 8 additions & 0 deletions ohkami/src/fang/handler/with_local_fangs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ impl<H: IntoHandler<T>, T, F1> IntoHandler<(F1, H, T)> for (F1, H)
where
F1: Fang<BoxedFPC>
{
fn n_params(&self) -> usize {self.1.n_params()}

fn into_handler(self) -> Handler {
let (f, h) = self;
let h = h.into_handler();
Expand All @@ -22,6 +24,8 @@ where
F1: Fang<F2::Proc>,
F2: Fang<BoxedFPC>,
{
fn n_params(&self) -> usize {self.2.n_params()}

fn into_handler(self) -> Handler {
let (f1, f2, h) = self;
let h = h.into_handler();
Expand All @@ -40,6 +44,8 @@ where
F2: Fang<F3::Proc>,
F3: Fang<BoxedFPC>,
{
fn n_params(&self) -> usize {self.3.n_params()}

fn into_handler(self) -> Handler {
let (f1, f2, f3, h) = self;
let h = h.into_handler();
Expand All @@ -59,6 +65,8 @@ where
F3: Fang<F4::Proc>,
F4: Fang<BoxedFPC>,
{
fn n_params(&self) -> usize {self.4.n_params()}

fn into_handler(self) -> Handler {
let (f1, f2, f3, f4, h) = self;
let h = h.into_handler();
Expand Down
43 changes: 43 additions & 0 deletions ohkami/src/ohkami/_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,3 +749,46 @@ fn method_dependent_fang_applying() {
}
});
}

#[test]
#[should_panic =
"handler `ohkami::ohkami::_test::panics_unexpected_path_params::hello_name` \
requires 1 path param(s) \
BUT the route `/hello` captures only 0 param(s)"
]
fn panics_unexpected_path_params() {
async fn hello_name(name: &str) -> String {
format!("Hello, {name}!")
}

let _ = Ohkami::new((
"/hello".GET(hello_name),
)).test(); /* panics here on finalize */
}

#[test]
#[should_panic =
"handler `ohkami::ohkami::_test::check_path_params_counted_accumulatedly::hello_name_age` \
requires 2 path param(s) \
BUT the route `/hello/:name` captures only 1 param(s)"
]
fn check_path_params_counted_accumulatedly() {
async fn hello_name(name: &str) -> String {
format!("Hello, {name}!")
}
async fn hello_name_age((name, age): (&str, u8)) -> String {
format!("Hello, {name} ({age})!")
}

let _ = Ohkami::new((
"/hello/:name".By(Ohkami::new((
"/".GET(hello_name),
))),
)).test(); /* NOT panics here */

let _ = Ohkami::new((
"/hello/:name".By(Ohkami::new((
"/".GET(hello_name_age),
))),
)).test(); /* panics here */
}
2 changes: 1 addition & 1 deletion ohkami/src/ohkami/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ impl Ohkami {

crate::DEBUG!("[openapi_document_bytes] routes = {routes:#?}, router = {router:#?}");

let doc = router.gen_openapi_doc(routes, openapi);
let doc = router.gen_openapi_doc(routes.keys().map(|r| &**r), openapi);

let mut bytes = ::serde_json::to_vec_pretty(&doc).expect("failed to serialize OpenAPI document");
bytes.push(b'\n');
Expand Down
Loading