Skip to content

Commit

Permalink
Tested finding streamname
Browse files Browse the repository at this point in the history
  • Loading branch information
amigin committed Sep 23, 2023
1 parent 0ee1087 commit 6163f4e
Showing 1 changed file with 72 additions and 17 deletions.
89 changes: 72 additions & 17 deletions my-grpc-server-macros/src/with_result_stream/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,91 @@ pub fn generate(
attr: proc_macro::TokenStream,
input: proc_macro::TokenStream,
) -> Result<proc_macro::TokenStream, syn::Error> {
let params_list = types_reader::ParamsList::new(attr.into(), || None)?;

let item_name = params_list.get_from_single_or_named("item_name")?;

let content = input.to_string();

println!("content: {}", content);
let stream_name = find_stream_name(content.as_str());

let index = content.find("tonic::Response<Self::");
Ok(crate::generate_stream::generate_stream(
stream_name,
item_name.unwrap_as_string_value()?.as_str(),
))
}

if index.is_none() {
panic!("tonic::Response<Self::StreamName> is not found");
}
fn find_stream_name(content: &str) -> &str {
let mut self_index = false;
let mut double_dots_index = false;

let index = index.unwrap();
let mut stream_name = None;

let content = &content[index..];
for item in content.split(' ') {
if !self_index {
if item == "Self" {
self_index = true;
continue;
}
}

let end = content.find(">");
if !double_dots_index {
if item == "::" {
double_dots_index = true;
continue;
}
}

if end.is_none() {
if !double_dots_index {
if item.ends_with("Stream") {
stream_name = Some(item);
break;
}
}

self_index = false;
double_dots_index = false;
}

if stream_name.is_none() {
panic!("tonic::Response<Self::StreamName> is not found");
}

let end = end.unwrap();
stream_name.unwrap()
}

let stream_name = &content[0..end];
#[cfg(test)]
mod tests {

let params_list = types_reader::ParamsList::new(attr.into(), || None)?;
#[test]
fn test_finding_stream() {
let content = r#"#[allow(clippy :: async_yields_async, clippy :: diverging_sub_expression,
clippy :: let_unit_value, clippy :: no_effect_underscore_binding, clippy ::
shadow_same, clippy :: type_complexity, clippy :: type_repetition_in_bounds,
clippy :: used_underscore_binding)] fn get_active_withdrawals < 'life0,
'async_trait >
(& 'life0 self, request : tonic :: Request < GetActiveWithdrawalsRequest >,)
-> :: core :: pin :: Pin < Box < dyn :: core :: future :: Future < Output =
Result < tonic :: Response < Self :: GetActiveWithdrawalsStream >, tonic ::
Status > > + :: core :: marker :: Send + 'async_trait > > where 'life0 :
'async_trait, Self : 'async_trait
{
Box ::
pin(async move
{
if let :: core :: option :: Option :: Some(__ret) = :: core :: option
:: Option :: None :: < Result < tonic :: Response < Self ::
GetActiveWithdrawalsStream >, tonic :: Status > > { return __ret ; }
let __self = self ; let request = request ; let __ret : Result < tonic
:: Response < Self :: GetActiveWithdrawalsStream >, tonic :: Status >
= { let request = request.into_inner() ; todo! ("Implement me") } ;
#[allow(unreachable_code)] __ret
})
}
"#;

let item_name = params_list.get_from_single_or_named("item_name")?;
let stream_name = super::find_stream_name(content);

Ok(crate::generate_stream::generate_stream(
stream_name,
item_name.unwrap_as_string_value()?.as_str(),
))
assert_eq!(stream_name, "GetActiveWithdrawalsStream");
}
}

0 comments on commit 6163f4e

Please sign in to comment.