-
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.
- Loading branch information
Showing
3 changed files
with
66 additions
and
64 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 |
---|---|---|
@@ -1,74 +1,74 @@ | ||
use ecdar_protobuf_transpiler::CompileVariables; | ||
use proc_macro::TokenStream as TS; | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
use convert_case::*; | ||
use ecdar_protobuf_transpiler::SERVICES as services; | ||
use proc_macro::TokenStream; | ||
|
||
trait ToBodyType { | ||
fn to_body_type(&self) -> String; | ||
} | ||
#[proc_macro] | ||
pub fn add_endpoints(app_name: TS) -> TS{ | ||
let app_name = TokenStream::from(app_name); | ||
|
||
let endpoints = ecdar_protobuf_transpiler::compile(|var| { | ||
let CompileVariables { | ||
fn_name, | ||
endpoint_name, | ||
service_name, | ||
.. | ||
} = var; | ||
|
||
impl ToBodyType for ecdar_protobuf_transpiler::ProtobuffTypes { | ||
fn to_body_type(&self) -> String { | ||
let rust_type = self.to_rust_type(); | ||
match rust_type.as_str() { | ||
"()" => "".into(), | ||
_ => format!("body : {rust_type}"), | ||
let route = format!("/{}/{}", | ||
service_name.to_string().to_case(Case::Snake), | ||
endpoint_name.to_string().to_case(Case::Snake) | ||
); | ||
|
||
quote!{ | ||
route(#route, post(#fn_name)) | ||
} | ||
} | ||
} | ||
}); | ||
|
||
#[proc_macro] | ||
pub fn add_endpoints(app_name: TokenStream) -> TokenStream { | ||
(app_name.to_string() | ||
+ ecdar_protobuf_transpiler::compile(|var| { | ||
format!( | ||
r#".route("/{}/{}", get({}))"#, | ||
var.service_name, var.endpoint_name, var.fn_name, | ||
) | ||
}) | ||
.as_str()) | ||
.print() | ||
.parse() | ||
.unwrap() | ||
quote!{ | ||
#app_name.#(#endpoints).* | ||
}.into() | ||
} | ||
|
||
#[proc_macro] | ||
pub fn add_endpoint_functions(_: TokenStream) -> TokenStream { | ||
ecdar_protobuf_transpiler::compile(|var| | ||
[ | ||
format!( | ||
"#[derive(serde::Serialize, serde::Deserialize)]\npub {in_struct}\n", | ||
in_struct = var.in_struct | ||
), | ||
format!( | ||
"pub async fn {fn_name}(Json(payload) : Json<{in_struct_name}>) -> Result<Json<{rtn_struct}>, StatusCode> {{", | ||
fn_name = var.fn_name, | ||
in_struct_name = var.in_struct_name, | ||
rtn_struct = var.rtn_struct, | ||
), | ||
format!( | ||
r#"let mut connect = {module}_client::{client}Client::connect(String::from("http://") + payload.ip.as_str()).await.map_err(|_| StatusCode::MISDIRECTED_REQUEST)?;"#, | ||
module = var.service_name.to_case(Case::Snake), | ||
client = var.service_name.to_case(Case::Pascal) | ||
), | ||
format!( | ||
r#"Ok(Json(connect.{endpoint_fn}({endpoint_fn_in}).await.map_err(|_| StatusCode::BAD_REQUEST)?.into_inner()))"#, | ||
endpoint_fn = var.endpoint_name.to_case(Case::Snake), | ||
endpoint_fn_in = if var.in_struct_has_body { "payload.body" } else { "()" }, | ||
), | ||
format!( | ||
"}}" | ||
) | ||
].join("\n") | ||
).print().parse().unwrap() | ||
} | ||
pub fn add_endpoint_functions(_: TS) -> TS{ | ||
let functions = ecdar_protobuf_transpiler::compile(|var|{ | ||
let CompileVariables { | ||
in_struct, | ||
in_struct_name, | ||
in_struct_has_body, | ||
rtn_struct, | ||
fn_name, | ||
client, | ||
endpoint_name, | ||
.. | ||
} = var; | ||
|
||
trait Print { | ||
fn print(self) -> Self; | ||
} | ||
let payload_body = if in_struct_has_body { | ||
quote!{ payload.body } | ||
} else { | ||
quote!{()} | ||
}; | ||
|
||
quote!{ | ||
#in_struct | ||
pub async fn #fn_name(Json(payload) : Json<#in_struct_name>) | ||
-> Result<Json<#rtn_struct>, StatusCode> { | ||
let mut connect = #client::connect(format!("http://{}", payload.ip.as_str())) | ||
.await | ||
.map_err(|_| StatusCode::MISDIRECTED_REQUEST)?; | ||
let res = connect.#endpoint_name(#payload_body) | ||
.await | ||
.map_err(|_| StatusCode::BAD_REQUEST)? | ||
.into_inner(); | ||
Ok(Json(res)) | ||
} | ||
} | ||
}); | ||
|
||
impl Print for String { | ||
fn print(self) -> Self { | ||
println!("{self}"); | ||
self | ||
} | ||
quote!{ | ||
#(#functions)* | ||
}.into() | ||
} | ||
|
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