mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-02-22 20:52:27 +00:00
Merge branch 'master' into gotham-master
Conflicts: example/Cargo.toml gotham_restful/Cargo.toml
This commit is contained in:
commit
694b45ea60
5 changed files with 36 additions and 24 deletions
|
@ -17,7 +17,7 @@ gitlab = { repository = "msrd0/gotham-restful", branch = "master" }
|
|||
fake = "2.2"
|
||||
gotham = { git = "https://github.com/gotham-rs/gotham", version = "0.5.0-dev", default-features = false }
|
||||
gotham_derive = { git = "https://github.com/gotham-rs/gotham", version = "0.5.0-dev", default-features = false }
|
||||
gotham_restful = { version = "0.0.3", features = ["auth", "openapi"] }
|
||||
gotham_restful = { version = "0.0.4", features = ["auth", "openapi"] }
|
||||
log = "0.4"
|
||||
log4rs = { version = "0.8", features = ["console_appender"], default-features = false }
|
||||
serde = "1"
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
[package]
|
||||
name = "gotham_restful"
|
||||
version = "0.0.3"
|
||||
version = "0.0.4"
|
||||
authors = ["Dominic Meiser <git@msrd0.de>"]
|
||||
edition = "2018"
|
||||
description = "RESTful additions for Gotham"
|
||||
|
@ -24,7 +24,7 @@ futures-util = "0.3.4"
|
|||
gotham = { git = "https://github.com/gotham-rs/gotham", version = "0.5.0-dev", default-features = false }
|
||||
gotham_derive = { git = "https://github.com/gotham-rs/gotham", version = "0.5.0-dev" }
|
||||
gotham_middleware_diesel = { git = "https://github.com/gotham-rs/gotham", version = "0.1.0", optional = true }
|
||||
gotham_restful_derive = { version = "0.0.2" }
|
||||
gotham_restful_derive = { version = "0.0.3" }
|
||||
hyper = "0.13.4"
|
||||
indexmap = { version = "1.3.0", optional = true }
|
||||
jsonwebtoken = { version = "7.1.0", optional = true }
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
[package]
|
||||
name = "gotham_restful_derive"
|
||||
version = "0.0.2"
|
||||
version = "0.0.3"
|
||||
authors = ["Dominic Meiser <git@msrd0.de>"]
|
||||
edition = "2018"
|
||||
description = "RESTful additions for Gotham - Derive"
|
||||
|
|
|
@ -47,7 +47,7 @@ impl FromStr for Method
|
|||
"Update" | "update" => Ok(Self::Update),
|
||||
"DeleteAll" | "delete_all" => Ok(Self::DeleteAll),
|
||||
"Delete" | "delete" => Ok(Self::Delete),
|
||||
_ => Err("unknown method".to_string())
|
||||
_ => Err(format!("Unknown method: `{}'", str))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,23 +1,27 @@
|
|||
use crate::method::Method;
|
||||
use crate::{
|
||||
method::Method,
|
||||
util::CollectToResult
|
||||
};
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::TokenStream as TokenStream2;
|
||||
use quote::quote;
|
||||
use syn::{
|
||||
parse::{Parse, ParseStream, Result as SynResult},
|
||||
parse::{Parse, ParseStream},
|
||||
punctuated::Punctuated,
|
||||
token::Comma,
|
||||
Error,
|
||||
Ident,
|
||||
ItemStruct,
|
||||
parenthesized,
|
||||
parse_macro_input
|
||||
};
|
||||
use std::str::FromStr;
|
||||
use std::{iter, str::FromStr};
|
||||
|
||||
struct MethodList(Punctuated<Ident, Comma>);
|
||||
|
||||
impl Parse for MethodList
|
||||
{
|
||||
fn parse(input: ParseStream) -> SynResult<Self>
|
||||
fn parse(input: ParseStream) -> Result<Self, Error>
|
||||
{
|
||||
let content;
|
||||
let _paren = parenthesized!(content in input);
|
||||
|
@ -26,26 +30,28 @@ impl Parse for MethodList
|
|||
}
|
||||
}
|
||||
|
||||
pub fn expand_resource(tokens : TokenStream) -> TokenStream
|
||||
fn expand(tokens : TokenStream) -> Result<TokenStream2, Error>
|
||||
{
|
||||
let krate = super::krate();
|
||||
let input = parse_macro_input!(tokens as ItemStruct);
|
||||
let input = parse_macro_input::parse::<ItemStruct>(tokens)?;
|
||||
let ident = input.ident;
|
||||
let name = ident.to_string();
|
||||
|
||||
let methods : Vec<TokenStream2> = input.attrs.into_iter().filter(|attr|
|
||||
let methods = input.attrs.into_iter().filter(|attr|
|
||||
attr.path.segments.iter().last().map(|segment| segment.ident.to_string()) == Some("rest_resource".to_string()) // TODO wtf
|
||||
).flat_map(|attr| {
|
||||
let m : MethodList = syn::parse2(attr.tokens).expect("unable to parse attributes");
|
||||
m.0.into_iter()
|
||||
}).map(|method| {
|
||||
let method = Method::from_str(&method.to_string()).expect("unknown method");
|
||||
let mod_ident = method.mod_ident(&name);
|
||||
let ident = method.setup_ident(&name);
|
||||
quote!(#mod_ident::#ident(&mut route);)
|
||||
}).collect();
|
||||
).map(|attr| {
|
||||
syn::parse2(attr.tokens).map(|m : MethodList| m.0.into_iter())
|
||||
}).flat_map(|list| match list {
|
||||
Ok(iter) => Box::new(iter.map(|method| {
|
||||
let method = Method::from_str(&method.to_string()).map_err(|err| Error::new(method.span(), err))?;
|
||||
let mod_ident = method.mod_ident(&name);
|
||||
let ident = method.setup_ident(&name);
|
||||
Ok(quote!(#mod_ident::#ident(&mut route);))
|
||||
})) as Box<dyn Iterator<Item = Result<TokenStream2, Error>>>,
|
||||
Err(err) => Box::new(iter::once(Err(err)))
|
||||
}).collect_to_result()?;
|
||||
|
||||
let output = quote! {
|
||||
Ok(quote! {
|
||||
impl #krate::Resource for #ident
|
||||
{
|
||||
fn name() -> String
|
||||
|
@ -58,6 +64,12 @@ pub fn expand_resource(tokens : TokenStream) -> TokenStream
|
|||
#(#methods)*
|
||||
}
|
||||
}
|
||||
};
|
||||
output.into()
|
||||
})
|
||||
}
|
||||
|
||||
pub fn expand_resource(tokens : TokenStream) -> TokenStream
|
||||
{
|
||||
expand(tokens)
|
||||
.unwrap_or_else(|err| err.to_compile_error())
|
||||
.into()
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue