-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathargs.rs
More file actions
296 lines (282 loc) · 11.5 KB
/
args.rs
File metadata and controls
296 lines (282 loc) · 11.5 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
use crate::http::is_http_method;
pub struct RouteArgs {
pub method: Option<syn::Ident>,
pub path: Option<syn::LitStr>,
pub error_status: Option<syn::ExprArray>,
pub tags: Option<syn::ExprArray>,
pub description: Option<syn::LitStr>,
}
impl syn::parse::Parse for RouteArgs {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let mut method: Option<syn::Ident> = None;
let mut path: Option<syn::LitStr> = None;
let mut error_status: Option<syn::ExprArray> = None;
let mut tags: Option<syn::ExprArray> = None;
let mut description: Option<syn::LitStr> = None;
// Parse comma-separated list of arguments
while !input.is_empty() {
let lookahead = input.lookahead1();
if lookahead.peek(syn::Ident) {
// Try to parse as method identifier (get, post, etc.)
let ident: syn::Ident = input.parse()?;
let ident_str = ident.to_string().to_lowercase();
if is_http_method(&ident_str) {
method = Some(ident);
} else if ident_str == "path" {
input.parse::<syn::Token![=]>()?;
let lit: syn::LitStr = input.parse()?;
path = Some(lit);
} else if ident_str == "error_status" {
input.parse::<syn::Token![=]>()?;
let array: syn::ExprArray = input.parse()?;
error_status = Some(array);
} else if ident_str == "tags" {
input.parse::<syn::Token![=]>()?;
let array: syn::ExprArray = input.parse()?;
tags = Some(array);
} else if ident_str == "description" {
input.parse::<syn::Token![=]>()?;
let lit: syn::LitStr = input.parse()?;
description = Some(lit);
} else {
return Err(lookahead.error());
}
// Check if there's a comma
if input.peek(syn::Token![,]) {
input.parse::<syn::Token![,]>()?;
} else {
break;
}
} else {
return Err(lookahead.error());
}
}
Ok(RouteArgs {
method,
path,
error_status,
tags,
description,
})
}
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use super::*;
#[rstest]
// Method only
#[case("get", true, Some("get"), None, None)]
#[case("post", true, Some("post"), None, None)]
#[case("put", true, Some("put"), None, None)]
#[case("patch", true, Some("patch"), None, None)]
#[case("delete", true, Some("delete"), None, None)]
#[case("head", true, Some("head"), None, None)]
#[case("options", true, Some("options"), None, None)]
// Path only
#[case("path = \"/api\"", true, None, Some("/api"), None)]
#[case("path = \"/users\"", true, None, Some("/users"), None)]
#[case("path = \"/api/v1\"", true, None, Some("/api/v1"), None)]
// Method and path
#[case("get, path = \"/api\"", true, Some("get"), Some("/api"), None)]
#[case("post, path = \"/users\"", true, Some("post"), Some("/users"), None)]
#[case("path = \"/api\", get", true, Some("get"), Some("/api"), None)]
// Error status only
#[case("error_status = [400]", true, None, None, Some(vec![400]))]
#[case("error_status = [400, 404]", true, None, None, Some(vec![400, 404]))]
#[case("error_status = [400, 404, 500]", true, None, None, Some(vec![400, 404, 500]))]
// Method and error_status
#[case("get, error_status = [400]", true, Some("get"), None, Some(vec![400]))]
#[case("post, error_status = [400, 404]", true, Some("post"), None, Some(vec![400, 404]))]
// Path and error_status
#[case("path = \"/api\", error_status = [400]", true, None, Some("/api"), Some(vec![400]))]
// All three
#[case("get, path = \"/api\", error_status = [400]", true, Some("get"), Some("/api"), Some(vec![400]))]
#[case("post, path = \"/users\", error_status = [400, 404]", true, Some("post"), Some("/users"), Some(vec![400, 404]))]
#[case("path = \"/api\", get, error_status = [400]", true, Some("get"), Some("/api"), Some(vec![400]))]
// Empty input
#[case("", true, None, None, None)]
// Invalid cases
#[case("invalid", false, None, None, None)]
#[case("path", false, None, None, None)]
#[case("error_status", false, None, None, None)]
#[case("get, invalid", false, None, None, None)]
#[case("path =", false, None, None, None)]
#[case("error_status =", false, None, None, None)]
// Non-Ident tokens (should trigger line 40)
#[case("123", false, None, None, None)]
#[case("\"string\"", false, None, None, None)]
#[case("=", false, None, None, None)]
#[case("[", false, None, None, None)]
#[case("]", false, None, None, None)]
#[case(",", false, None, None, None)]
#[case("get, 123", false, None, None, None)]
#[case("get, =", false, None, None, None)]
fn test_route_args_parse(
#[case] input: &str,
#[case] should_parse: bool,
#[case] expected_method: Option<&str>,
#[case] expected_path: Option<&str>,
#[case] expected_error_status: Option<Vec<u16>>,
) {
let result = syn::parse_str::<RouteArgs>(input);
match (should_parse, result) {
(true, Ok(route_args)) => {
// Check method
if let Some(exp_method) = expected_method {
assert!(
route_args.method.is_some(),
"Expected method {} but got None for input: {}",
exp_method,
input
);
assert_eq!(
route_args.method.as_ref().unwrap().to_string(),
exp_method,
"Method mismatch for input: {}",
input
);
} else {
assert!(
route_args.method.is_none(),
"Expected no method but got {:?} for input: {}",
route_args.method,
input
);
}
// Check path
if let Some(exp_path) = expected_path {
assert!(
route_args.path.is_some(),
"Expected path {} but got None for input: {}",
exp_path,
input
);
assert_eq!(
route_args.path.as_ref().unwrap().value(),
exp_path,
"Path mismatch for input: {}",
input
);
} else {
assert!(
route_args.path.is_none(),
"Expected no path but got {:?} for input: {}",
route_args.path,
input
);
}
// Check error_status
if let Some(exp_status) = expected_error_status {
assert!(
route_args.error_status.is_some(),
"Expected error_status {:?} but got None for input: {}",
exp_status,
input
);
let array = route_args.error_status.as_ref().unwrap();
let mut status_codes = Vec::new();
for elem in &array.elems {
if let syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Int(lit_int),
..
}) = elem
&& let Ok(code) = lit_int.base10_parse::<u16>()
{
status_codes.push(code);
}
}
assert_eq!(
status_codes, exp_status,
"Error status mismatch for input: {}",
input
);
} else {
assert!(
route_args.error_status.is_none(),
"Expected no error_status but got {:?} for input: {}",
route_args.error_status,
input
);
}
}
(false, Err(_)) => {
// Expected error, test passes
}
(true, Err(e)) => {
panic!(
"Expected successful parse but got error: {} for input: {}",
e, input
);
}
(false, Ok(_)) => {
panic!("Expected parse error but got success for input: {}", input);
}
}
}
#[rstest]
// Tags only
#[case("tags = [\"users\"]", true, vec!["users"])]
#[case("tags = [\"users\", \"admin\"]", true, vec!["users", "admin"])]
#[case("tags = [\"api\", \"v1\", \"users\"]", true, vec!["api", "v1", "users"])]
// Tags with method
#[case("get, tags = [\"users\"]", true, vec!["users"])]
#[case("post, tags = [\"users\", \"create\"]", true, vec!["users", "create"])]
// Tags with path
#[case("path = \"/api\", tags = [\"api\"]", true, vec!["api"])]
// Tags with method and path
#[case("get, path = \"/users\", tags = [\"users\"]", true, vec!["users"])]
// Empty tags array
#[case("tags = []", true, vec![])]
fn test_route_args_parse_tags(
#[case] input: &str,
#[case] should_parse: bool,
#[case] expected_tags: Vec<&str>,
) {
let result = syn::parse_str::<RouteArgs>(input);
match (should_parse, result) {
(true, Ok(route_args)) => {
if expected_tags.is_empty() {
// Empty array should result in Some with empty vec
if let Some(tags_array) = &route_args.tags {
assert!(tags_array.elems.is_empty());
}
} else {
assert!(
route_args.tags.is_some(),
"Expected tags but got None for input: {}",
input
);
let tags_array = route_args.tags.as_ref().unwrap();
let mut parsed_tags = Vec::new();
for elem in &tags_array.elems {
if let syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(lit_str),
..
}) = elem
{
parsed_tags.push(lit_str.value());
}
}
assert_eq!(
parsed_tags, expected_tags,
"Tags mismatch for input: {}",
input
);
}
}
(false, Err(_)) => {
// Expected error, test passes
}
(true, Err(e)) => {
panic!(
"Expected successful parse but got error: {} for input: {}",
e, input
);
}
(false, Ok(_)) => {
panic!("Expected parse error but got success for input: {}", input);
}
}
}
}