1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use super::{Method, Service};
use crate::{generate_doc_comments, naive_snake_case};
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
pub fn generate<T: Service>(service: &T, proto_path: &str) -> TokenStream {
let service_ident = quote::format_ident!("{}Client", service.name());
let client_mod = quote::format_ident!("{}_client", naive_snake_case(service.name()));
let methods = generate_methods(service, proto_path);
let service_doc = generate_doc_comments(service.comment());
quote! {
#[allow(dead_code, unused_imports)]
pub mod #client_mod {
use hrpc::client::prelude::*;
#service_doc
#[derive(Debug, Clone)]
pub struct #service_ident {
inner: Client,
}
impl #service_ident {
pub fn new<U>(host: U) -> ClientResult<Self>
where
U: TryInto<Uri>,
U::Error: Debug,
{
Ok(Self {
inner: Client::new(host.try_into().expect("invalid URL"))?,
})
}
pub fn new_inner(inner: Client) -> Self {
Self { inner }
}
pub fn new_http(http: HttpClient, host_url: Uri) -> ClientResult<Self> {
Ok(Self {
inner: Client::new_inner(http, host_url)?,
})
}
#methods
}
}
}
}
fn generate_methods<T: Service>(service: &T, proto_path: &str) -> TokenStream {
let mut stream = TokenStream::new();
for method in service.methods() {
let path = format!(
"/{}{}{}/{}",
service.package(),
if service.package().is_empty() {
""
} else {
"."
},
service.identifier(),
method.identifier()
);
let make_method = match (method.client_streaming(), method.server_streaming()) {
(false, false) => generate_unary,
(true, true) => generate_streaming,
(false, true) => generate_server_streaming,
(true, false) => panic!("{}: Client streaming server unary method is invalid.", path),
};
stream.extend(generate_doc_comments(method.comment()));
stream.extend(make_method(method, proto_path, path));
}
stream
}
fn generate_unary<T: Method>(method: &T, proto_path: &str, path: String) -> TokenStream {
let ident = format_ident!("{}", method.name());
let (request, response) = method.request_response_name(proto_path);
quote! {
pub async fn #ident<Req>(&mut self, req: Req) -> ClientResult<#response>
where
Req: IntoRequest<#request>,
{
self.inner.execute_request(#path, req.into_request()).await
}
}
}
fn generate_streaming<T: Method>(method: &T, proto_path: &str, path: String) -> TokenStream {
let ident = format_ident!("{}", method.name());
let (request, response) = method.request_response_name(proto_path);
quote! {
pub async fn #ident<Req>(&mut self, req: Req) -> ClientResult<Socket<#request, #response>>
where
Req: IntoRequest<()>,
{
self.inner.connect_socket(#path, req.into_request()).await
}
}
}
fn generate_server_streaming<T: Method>(method: &T, proto_path: &str, path: String) -> TokenStream {
let ident = format_ident!("{}", method.name());
let (request, response) = method.request_response_name(proto_path);
quote! {
pub async fn #ident<Req>(&mut self, req: Req) -> ClientResult<Socket<#request, #response>>
where
Req: IntoRequest<#request>,
{
self.inner.connect_socket_req(#path, req.into_request()).await
}
}
}