-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.rs
204 lines (182 loc) · 4.99 KB
/
service.rs
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#![allow(dead_code)]
use indexmap::IndexMap;
use nom::combinator::opt;
use nom::multi::many0;
use nom::sequence::delimited;
use nom::{branch::alt, combinator::map, sequence::tuple};
use crate::common::{match_text_case_insensitive, match_token, IResult, Input};
use crate::token::APITokenKind::*;
#[derive(Debug, Default)]
pub struct Service {
name: String,
anotation: Option<IndexMap<String, String>>,
pub handlers: Vec<Handler>,
}
#[derive(Debug)]
pub struct Handler {
pub name: String,
pub method: HttpMethod,
pub path: String,
req_type: Option<String>,
pub resp_type: Option<String>,
}
#[derive(Debug)]
pub enum HttpMethod {
GET,
POST,
}
impl std::fmt::Display for HttpMethod {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
HttpMethod::GET => write!(f, "get"),
HttpMethod::POST => write!(f, "post"),
}
}
}
pub fn parse_service(i: Input) -> IResult<Service> {
tuple((
opt(parse_service_anotation),
delimited(
match_token(Service),
match_token(Identifier),
match_token(OpenBrace),
),
many0(parse_handler),
match_token(CloseBrace),
))(i)
.map(|(i, (anotation, name, handlers, _))| {
(
i,
Service {
name: name.at.to_string(),
anotation,
handlers,
},
)
})
}
fn parse_service_anotation(i: Input) -> IResult<IndexMap<String, String>> {
tuple((
match_token(Server),
delimited(
match_token(OpenParen),
parse_kv_pairs,
match_token(CloseParen),
),
))(i)
.map(|(i, (_, pairs))| (i, pairs.into_iter().collect::<IndexMap<_, _>>()))
}
fn parse_kv_pairs(i: Input) -> IResult<Vec<(String, String)>> {
many0(tuple((
match_token(Identifier),
match_token(Colon),
match_token(Identifier),
)))(i)
.map(|(i, pairs)| {
(
i,
pairs
.into_iter()
.map(|(key, _, value)| (key.at.to_string(), value.at.to_string()))
.collect::<Vec<_>>(),
)
})
}
fn parse_handler(i: Input) -> IResult<Handler> {
tuple((
match_token(Handler),
match_token(Identifier),
parse_http_method,
match_token(RoutePath),
delimited(
opt(match_token(OpenParen)),
opt(match_token(Identifier)),
opt(match_token(CloseParen)),
),
delimited(
tuple((opt(match_token(RespReturns)), opt(match_token(OpenParen)))),
opt(match_token(Identifier)),
opt(match_token(CloseParen)),
),
))(i)
.map(|(i, (_, name, method, path, req_type, resp_type))| {
(
i,
Handler {
name: name.at.to_string(),
method,
path: path.at.to_string(),
req_type: req_type.map(|t| t.at.to_string()),
resp_type: resp_type.map(|t| t.at.to_string()),
},
)
})
}
fn parse_http_method(i: Input) -> IResult<HttpMethod> {
alt((
map(match_text_case_insensitive("GET"), |_| HttpMethod::GET),
map(match_text_case_insensitive("POST"), |_| HttpMethod::POST),
))(i)
}
#[cfg(test)]
mod tests {
use crate::token::tokenize;
use super::*;
#[test]
fn it_parse_service() {
let source = r#"
@server (
group: json
jwt: Auth
timeout: 3ms
)
service example {
@handler getForm
get /example/form (GetFormReq) returns (GetFormResp)
@handler postJson
post /example/json (PostJsonReq) returns (PostJsonResp)
}
"#;
let tokens = tokenize(source);
let res = parse_service(&tokens);
let service_res = res.unwrap().1;
println!("{:#?}", service_res);
}
#[test]
fn it_parse_service_anotation() {
let source = r#"
@server (
group: json
jwt: Auth
timeout: 3m
)
"#;
let tokens = tokenize(source);
let res = parse_service_anotation(&tokens);
let anotation_res = res.unwrap().1;
println!("{:#?}", anotation_res);
}
#[test]
fn it_parse_kv_pairs() {
let source = r#"
group: json
jwt: Auth
timeout: 3m
"#;
let tokens = tokenize(source);
let res = parse_kv_pairs(&tokens);
let kv_pairs_res = res.unwrap().1;
println!("{:#?}", kv_pairs_res);
}
#[test]
fn it_parse_handler() {
let source = r#"
@handler GetFormReq
get /form/req returns (GetFormReq)
"#;
let tokens = tokenize(source);
let res = parse_handler(&tokens);
let handler_res = res.unwrap().1;
println!("{:#?}", handler_res);
}
}