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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#![deny(missing_docs)]
use proc_macro2::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream};
use quote::TokenStreamExt;
#[cfg(feature = "rustfmt")]
#[cfg_attr(docsrs, doc(cfg(feature = "rustfmt")))]
use std::io::{self, Write};
#[cfg(feature = "rustfmt")]
#[cfg_attr(docsrs, doc(cfg(feature = "rustfmt")))]
use std::process::{exit, Command};
mod prost;
pub use self::prost::{compile_protos, configure, Builder};
#[cfg(feature = "client")]
pub mod client;
#[cfg(feature = "server")]
pub mod server;
pub trait Service {
type Comment: AsRef<str>;
type Method: Method;
fn name(&self) -> &str;
fn package(&self) -> &str;
fn identifier(&self) -> &str;
fn methods(&self) -> &[Self::Method];
fn comment(&self) -> &[Self::Comment];
}
pub trait Method {
type Comment: AsRef<str>;
fn name(&self) -> &str;
fn identifier(&self) -> &str;
fn client_streaming(&self) -> bool;
fn server_streaming(&self) -> bool;
fn comment(&self) -> &[Self::Comment];
fn options(&self) -> Vec<(String, String)>;
fn request_response_name(&self, proto_path: &str) -> (TokenStream, TokenStream);
}
#[cfg(feature = "rustfmt")]
#[cfg_attr(docsrs, doc(cfg(feature = "rustfmt")))]
pub fn fmt(out_dir: &str) {
let dir = std::fs::read_dir(out_dir).unwrap();
for entry in dir {
let file = entry.unwrap().file_name().into_string().unwrap();
if !file.ends_with(".rs") {
continue;
}
let result = Command::new("rustfmt")
.arg("--emit")
.arg("files")
.arg("--edition")
.arg("2018")
.arg(format!("{}/{}", out_dir, file))
.output();
match result {
Err(e) => {
eprintln!("error running rustfmt: {:?}", e);
exit(1)
}
Ok(output) => {
if !output.status.success() {
io::stderr().write_all(&output.stderr).unwrap();
exit(output.status.code().unwrap_or(1))
}
}
}
}
}
fn generate_doc_comment<S: AsRef<str>>(comment: S) -> TokenStream {
let mut doc_stream = TokenStream::new();
doc_stream.append(Ident::new("doc", Span::call_site()));
doc_stream.append(Punct::new('=', Spacing::Alone));
let comment = comment.as_ref();
let comment_escaped = comment.replace("```", "\\`\\`\\`");
doc_stream.append(Literal::string(&comment_escaped));
let group = Group::new(Delimiter::Bracket, doc_stream);
let mut stream = TokenStream::new();
stream.append(Punct::new('#', Spacing::Alone));
stream.append(group);
stream
}
fn generate_doc_comments<T: AsRef<str>>(comments: &[T]) -> TokenStream {
let mut stream = TokenStream::new();
for comment in comments {
stream.extend(generate_doc_comment(comment));
}
stream
}
fn naive_snake_case(name: &str) -> String {
let mut s = String::new();
let mut it = name.chars().peekable();
while let Some(x) = it.next() {
s.push(x.to_ascii_lowercase());
if let Some(y) = it.peek() {
if y.is_uppercase() {
s.push('_');
}
}
}
s
}
#[test]
fn test_snake_case() {
for case in &[
("Service", "service"),
("ThatHasALongName", "that_has_a_long_name"),
("greeter", "greeter"),
("ABCServiceX", "a_b_c_service_x"),
] {
assert_eq!(naive_snake_case(case.0), case.1)
}
}