-
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.
Fix:
derive_from_request
& increment version
- Loading branch information
Showing
5 changed files
with
108 additions
and
11 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" | ||
|
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,19 +1,53 @@ | ||
use proc_macro2::{Span, TokenStream}; | ||
use syn::{Error, ItemStruct, Result}; | ||
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)?; | ||
|
||
if s.generics.lifetimes().count() >= 2 { | ||
return Err(Error::new(Span::call_site(), "`#[derive(FromRequest)]` doesn't support multiple lifetimes!")) | ||
} | ||
let name = &s.ident; | ||
|
||
if s.semi_token.is_none() {/* struct S { 〜 } */ | ||
|
||
let generics_params_r = &s.generics.params; | ||
let generics_params_l = &mut generics_params_r.clone(); | ||
let generics_where = &s.generics.where_clause; | ||
|
||
} else {/* struct T(); */ | ||
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 ),*) ] | ||
}; | ||
|
||
todo!() | ||
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) | ||
} | ||
} | ||
}) | ||
} |