-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from kana-rus/add_derive_from_request
Add derive from request
- Loading branch information
Showing
8 changed files
with
153 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "derive_from_request" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
ohkami = { workspace = true } | ||
tokio = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#![allow(unused)] | ||
fn main() {} | ||
|
||
use ohkami::{FromRequest, Method}; | ||
|
||
|
||
struct RequestMethod(Method); | ||
impl<'req> FromRequest<'req> for RequestMethod { | ||
type Error = std::convert::Infallible; | ||
fn from_request(req: &'req ohkami::prelude::Request) -> Result<Self, Self::Error> { | ||
Ok(Self(req.method())) | ||
} | ||
} | ||
|
||
struct RequestPath<'req>(std::borrow::Cow<'req, str>); | ||
impl<'req> FromRequest<'req> for RequestPath<'req> { | ||
type Error = std::convert::Infallible; | ||
fn from_request(req: &'req ohkami::prelude::Request) -> Result<Self, Self::Error> { | ||
Ok(Self(req.path())) | ||
} | ||
} | ||
|
||
struct RequestPathOwned(String); | ||
impl<'req> FromRequest<'req> for RequestPathOwned { | ||
type Error = std::convert::Infallible; | ||
fn from_request(req: &'req ohkami::prelude::Request) -> Result<Self, Self::Error> { | ||
Ok(Self(req.path().into())) | ||
} | ||
} | ||
|
||
|
||
#[derive(FromRequest)] | ||
struct MethodAndPathA { | ||
method: RequestMethod, | ||
path: RequestPathOwned, | ||
} | ||
|
||
#[derive(FromRequest)] | ||
struct MethodAndPathB<'req> { | ||
method: RequestMethod, | ||
path: RequestPath<'req>, | ||
} | ||
|
||
#[derive(FromRequest)] | ||
struct MethodAndPathC( | ||
RequestMethod, | ||
RequestPathOwned, | ||
); | ||
|
||
#[derive(FromRequest)] | ||
struct MethodAndPathD<'req>( | ||
RequestMethod, | ||
RequestPath<'req>, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "ohkami" | ||
version = "0.15.0" | ||
version = "0.15.1" | ||
edition = "2021" | ||
authors = ["kanarus <[email protected]>"] | ||
description = "Build web app in intuitive and declarative code" | ||
|
@@ -17,7 +17,7 @@ features = ["rt_tokio", "custom-header"] | |
|
||
[dependencies] | ||
ohkami_lib = { version = "=0.1.1", path = "../ohkami_lib" } | ||
ohkami_macros = { version = "=0.6.0", path = "../ohkami_macros" } | ||
ohkami_macros = { version = "=0.6.1", path = "../ohkami_macros" } | ||
tokio = { version = "1", optional = true, features = ["net", "rt", "io-util", "sync", "time"] } | ||
async-std = { version = "1", optional = true } | ||
byte_reader = { version = "2.0.0", features = ["text"] } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ proc-macro = true | |
|
||
[package] | ||
name = "ohkami_macros" | ||
version = "0.6.0" | ||
version = "0.6.1" | ||
edition = "2021" | ||
authors = ["kanarus <[email protected]>"] | ||
description = "Proc macros for ohkami - intuitive and declarative web framework" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use proc_macro2::{Span, TokenStream}; | ||
use syn::{Field, ItemStruct, Result}; | ||
use quote::quote; | ||
use crate::components::from_request_lifetime; | ||
|
||
|
||
pub(super) fn derive_from_request(target: TokenStream) -> Result<TokenStream> { | ||
let s: ItemStruct = syn::parse2(target)?; | ||
|
||
let name = &s.ident; | ||
|
||
let generics_params_r = &s.generics.params; | ||
let generics_params_l = &mut generics_params_r.clone(); | ||
let generics_where = &s.generics.where_clause; | ||
|
||
let impl_lifetime = match s.generics.lifetimes().count() { | ||
0 => { | ||
let il = from_request_lifetime(); | ||
generics_params_l.push(il.clone()); | ||
il | ||
} | ||
1 => s.generics.params.first().unwrap().clone(), | ||
_ => return Err(syn::Error::new(Span::call_site(), "#[derive(FromRequest)] doesn't support multiple lifetime params")), | ||
}; | ||
|
||
let build = if s.semi_token.is_none() {/* struct S { 〜 } */ | ||
let fields = s.fields.into_iter() | ||
.map(|Field { ident, ty, .. }| quote! { | ||
#ident: <#ty as ::ohkami::FromRequest>::from_request(req) | ||
.map_err(::ohkami::IntoResponse::into_response)? | ||
}); | ||
quote![ Self { #( #fields ),* } ] | ||
|
||
} else {/* struct T(); */ | ||
let fields = s.fields.into_iter() | ||
.map(|Field { ty, .. }| quote! { | ||
<#ty as ::ohkami::FromRequest>::from_request(req) | ||
.map_err(::ohkami::IntoResponse::into_response)? | ||
}); | ||
quote![ Self(#( #fields ),*) ] | ||
}; | ||
|
||
Ok(quote! { | ||
impl<#generics_params_l> ::ohkami::FromRequest<#impl_lifetime> for #name<#generics_params_r> | ||
#generics_where | ||
{ | ||
type Error = ::ohkami::Response; | ||
fn from_request(req: &#impl_lifetime ::ohkami::Request) -> ::std::result::Result<Self, Self::Error> { | ||
::std::result::Result::Ok(#build) | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters