-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.rs
79 lines (70 loc) · 1.88 KB
/
parser.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
#![allow(dead_code)]
use nom::sequence::tuple;
use crate::{
common::{IResult, Input},
service::{parse_service, Service},
struct_ref::{parse_struct_stmt1, StructDef},
};
#[derive(Debug)]
pub struct APIStmt {
pub type_struct: Vec<StructDef>,
pub service: Service,
}
pub fn parse_api(i: Input) -> IResult<APIStmt> {
tuple((parse_struct_stmt1, parse_service))(i).map(|(i, (type_struct, service))| {
(
i,
APIStmt {
type_struct,
service,
},
)
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::token::tokenize;
#[test]
// cargo test --package goctl-rs --lib -- parser::tests::test_parse_api --exact --nocapture
fn test_parse_api() {
let source = r#"
type (
PostFormReq struct {
Name string `form:"name"`
Age int `form:"age"`
}
PostFormResp struct {
Total int64 `json:"total"`
}
)
type GetFormReq struct {
Name string `form:"name,omitempty"`
Age int64 `form:"age" json:"age"`
}
type Status struct {
Code int `json:"code"`
Msg string `json:"msg"`
}
type GetFormResp struct {
Total int64 `json:"total"`
Status
}
@server (
group: json
jwt: Auth
timeout: 3m
)
service example {
@handler getForm
get /example/form (GetFormReq) returns (GetFormResp)
@handler postJson
post /example/json (PostJsonReq) returns (PostJsonResp)
}
"#;
let input = tokenize(source);
let result = parse_api(&input);
let api_var = result.unwrap().1;
println!("{:#?}", api_var);
}
}