-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Endpoint macro that auto add functions
IT IS NOT DONE YET. The macro needs to add the generated code to the implementation.
- Loading branch information
1 parent
f84ebf0
commit 68bb3d9
Showing
5 changed files
with
139 additions
and
5 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,14 @@ | ||
[package] | ||
name = "ecdar_api_macros" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
syn = { version = "2.0", features = ["full"] } | ||
quote = "1.0" | ||
# proc-macro2 = "1.0.70" |
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,114 @@ | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{parse_macro_input, ImplItemFn, Item, ItemFn, ItemImpl, ItemMod}; | ||
Check warning on line 3 in ecdar_api_macros/src/lib.rs GitHub Actions / Clippy lint and checkunused imports: `ImplItemFn`, `ItemFn`
|
||
|
||
#[proc_macro_attribute] | ||
pub fn endpoints(_attr: TokenStream, item: TokenStream) -> TokenStream { | ||
let item_mod: ItemMod = parse_macro_input!(item as ItemMod); | ||
|
||
let impl_names: Vec<String> = item_mod | ||
.clone() | ||
.content | ||
.as_ref() | ||
.map(|(_, items)| { | ||
items.iter().filter_map(|item| { | ||
if let syn::Item::Impl(impl_item) = item { | ||
Some( | ||
impl_item | ||
.trait_ | ||
.as_ref() | ||
.map_or_else( | ||
|| None, | ||
|t| Some(t.1.segments.last().unwrap().ident.to_string()), | ||
) | ||
.unwrap_or_else(|| "".to_string()), | ||
) | ||
} else { | ||
None | ||
} | ||
}) | ||
}) | ||
.unwrap() | ||
.collect(); | ||
|
||
let names: Vec<String> = impl_names | ||
.clone() | ||
.into_iter() | ||
.filter(|item| !item.is_empty()) | ||
.collect(); | ||
|
||
let mut endpoints: Vec<String> = Vec::new(); | ||
|
||
let items_impl: Vec<Item> = item_mod.content.unwrap().1; | ||
|
||
let items_impl: Vec<ItemImpl> = items_impl | ||
.into_iter() | ||
.filter_map(|item| match item { | ||
Item::Impl(item_impl) => Some(item_impl), | ||
_ => None, | ||
}) | ||
.collect(); | ||
|
||
let items_impl: Vec<ItemImpl> = items_impl | ||
.clone() | ||
.into_iter() | ||
.filter(|item| item.trait_.is_some()) | ||
.collect(); | ||
|
||
for i in 0..items_impl.len() { | ||
let existing_functions: Vec<String> = items_impl[i] | ||
.items | ||
.iter() | ||
.filter_map(|item| match item { | ||
syn::ImplItem::Fn(function) => Some(function.sig.ident.to_string()), | ||
_ => None, | ||
}) | ||
.collect(); | ||
|
||
for function in existing_functions { | ||
endpoints.push(format!("{}/{}", names[i], function)); | ||
} | ||
} | ||
|
||
let new_function: TokenStream = quote! { | ||
async fn endpoints(&self, request: tonic::Request<()>) -> std::result::Result<tonic::Response<EndpointsResponse>, tonic::Status> { | ||
let names = vec![#(#endpoints.to_string()),*]; | ||
Ok(Response::new(EndpointsResponse { | ||
endpoints: names | ||
})) | ||
} | ||
} | ||
.into(); | ||
|
||
println!("{}", new_function.to_string()); | ||
|
||
// let mut updated_items = item_mod.content.as_ref() | ||
|
||
// let mut updated_items = input.items.clone(); | ||
|
||
// updated_items.push(syn::ImplItem::Fn(parse_macro_input!( | ||
// new_function as ImplItemFn | ||
// ))); | ||
|
||
// let updated_impl = ItemImpl { | ||
// attrs: input.attrs, | ||
// defaultness: input.defaultness, | ||
// unsafety: input.unsafety, | ||
// impl_token: input.impl_token, | ||
// generics: input.generics, | ||
// trait_: input.trait_, | ||
// self_ty: input.self_ty, | ||
// brace_token: input.brace_token, | ||
// items: updated_items, | ||
// }; | ||
|
||
// let output = quote! { | ||
// #updated_impl | ||
// }; | ||
|
||
// println!("{}", output.to_string()); | ||
|
||
// output.into() | ||
todo!() | ||
} | ||
|
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,15 +1,18 @@ | ||
#![ecdar_api_macros::endpoints] | ||
Check failure on line 1 in src/api/ecdar_api.rs GitHub Actions / Clippy lint and checkinner macro attributes are unstable
Check failure on line 1 in src/api/ecdar_api.rs GitHub Actions / Clippy lint and checkinner macro attributes are unstable
|
||
#![feature(custom_inner_attributes)] | ||
|
||
use super::server::server::{ | ||
ecdar_api_auth_server::EcdarApiAuth, | ||
ecdar_api_server::EcdarApi, | ||
ecdar_backend_server::EcdarBackend, | ||
get_auth_token_request::{user_credentials, UserCredentials}, | ||
CreateAccessRequest, CreateProjectRequest, CreateProjectResponse, CreateQueryRequest, | ||
CreateUserRequest, DeleteAccessRequest, DeleteProjectRequest, DeleteQueryRequest, | ||
GetAuthTokenRequest, GetAuthTokenResponse, GetProjectRequest, GetProjectResponse, | ||
ListProjectsInfoResponse, Query, QueryRequest, QueryResponse, SendQueryRequest, | ||
SendQueryResponse, SimulationStartRequest, SimulationStepRequest, SimulationStepResponse, | ||
UpdateAccessRequest, UpdateProjectRequest, UpdateQueryRequest, UpdateUserRequest, | ||
UserTokenResponse, | ||
EndpointsResponse, GetAuthTokenRequest, GetAuthTokenResponse, GetProjectRequest, | ||
GetProjectResponse, ListProjectsInfoResponse, Query, QueryRequest, QueryResponse, | ||
SendQueryRequest, SendQueryResponse, SimulationStartRequest, SimulationStepRequest, | ||
SimulationStepResponse, UpdateAccessRequest, UpdateProjectRequest, UpdateQueryRequest, | ||
UpdateUserRequest, UserTokenResponse, | ||
}; | ||
use crate::api::context_collection::ContextCollection; | ||
use crate::api::{ | ||
|
@@ -1050,3 +1053,4 @@ mod user_logic_tests; | |
#[cfg(test)] | ||
#[path = "../tests/api/session_logic.rs"] | ||
mod session_logic_tests; | ||
Check failure on line 1055 in src/api/ecdar_api.rs GitHub Actions / Clippy lint and checknon-inline modules in proc macro input are unstable
|
||
|
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 |
---|---|---|
|
@@ -4,3 +4,4 @@ pub mod ecdar_api; | |
pub mod hashing_context; | ||
pub mod reveaal_context; | ||
pub mod server; | ||
|