-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain.rs
More file actions
96 lines (83 loc) · 2.4 KB
/
main.rs
File metadata and controls
96 lines (83 loc) · 2.4 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
#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_json;
use std::error::Error;
use std::fs::File;
use std::path::Path;
#[derive(Serialize, Deserialize, Debug)]
struct PyAssign
{
ast_type: String,
col_offset: u8,
lineno: u8
}
#[derive(Serialize, Deserialize, Debug)]
struct PyFunctionDef
{
//args
ast_type: String,
//body
col_offset: u8,
//decorator_list
lineno: u8,
name: String
}
#[derive(Serialize, Deserialize, Debug)]
struct PyExpr
{
ast_type: String,
col_offset: u8,
lineno: u8,
//value
}
#[derive(Serialize, Deserialize, Debug)]
struct PyBody {
ast_type: String,
col_offset: u8,
lineno: u8,
//targets: Vec<PyTarget>
}
#[derive(Serialize, Deserialize, Debug)]
struct PyFunction {
ast_type: String,
body: Vec<serde_json::Value>
}
fn read_code_from_file<P: AsRef<Path>>(path: P) -> Result<PyFunction, Box<Error>> {
// Open the file in read-only mode.
let file = File::open(path)?;
// Read the JSON contents of the file as an instance of `User`.
let first_pass = serde_json::from_reader(file)?;
// Return the `User`.
Ok(first_pass)
}
fn main() {
let nodes = read_code_from_file("../client/file1.json").unwrap();
println!("Parsed {:?} nodes:\n", nodes.body.len());
//println!("{:#?}", nodes.body[0]);
//parse each node
for i in 0..nodes.body.len()
{
//parse each node
let n: PyBody = serde_json::from_value(nodes.body[i].clone()).unwrap();
match n.ast_type.as_ref() {
"Assign" => {
//println!("parsing Assign node:");
let assign: PyAssign = serde_json::from_value(nodes.body[i].clone()).unwrap();
println!("{:?}", assign.ast_type);
},
"FunctionDef" => {
//println!("parsing FunctionDef node:");
let function_def: PyFunctionDef = serde_json::from_value(nodes.body[i].clone()).unwrap();
println!("{:?}, {:?}", function_def.ast_type, function_def.name);
},
"Expr" => {
//println!("parsing Expr node:");
let expr: PyExpr = serde_json::from_value(nodes.body[i].clone()).unwrap();
println!("{:?}", expr.ast_type);
},
_ => println!("matched unknown node, no action taken: {:?}", n.ast_type)
}
//println!("{:?}", n);
}
}