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
#![forbid(unsafe_code)]
#![doc = include_str!("../README.md")]
extern crate core;
pub mod access;
pub use self::access::*;
pub mod r#struct;
pub use self::r#struct::*;
pub mod common;
pub use self::common::*;
pub mod expressions;
pub use self::expressions::*;
pub mod functions;
pub use self::functions::*;
pub mod groups;
pub use self::groups::*;
pub mod input;
pub use self::input::*;
pub mod mapping;
pub use self::mapping::*;
pub mod passes;
pub use self::passes::*;
pub mod program;
pub use self::program::*;
pub mod statement;
pub use self::statement::*;
pub mod types;
pub use self::types::*;
pub mod value;
pub use self::value::*;
pub use common::node::*;
use leo_errors::{AstError, Result};
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Ast {
pub ast: Program,
}
impl Ast {
pub fn new(program: Program) -> Self {
Self { ast: program }
}
pub fn as_repr(&self) -> &Program {
&self.ast
}
pub fn into_repr(self) -> Program {
self.ast
}
pub fn to_json_string(&self) -> Result<String> {
Ok(serde_json::to_string_pretty(&self.ast).map_err(|e| AstError::failed_to_convert_ast_to_json_string(&e))?)
}
pub fn to_json_value(&self) -> Result<serde_json::Value> {
Ok(serde_json::to_value(&self.ast).map_err(|e| AstError::failed_to_convert_ast_to_json_value(&e))?)
}
pub fn to_json_file(&self, mut path: std::path::PathBuf, file_name: &str) -> Result<()> {
path.push(file_name);
let file = std::fs::File::create(&path).map_err(|e| AstError::failed_to_create_ast_json_file(&path, &e))?;
let writer = std::io::BufWriter::new(file);
Ok(serde_json::to_writer_pretty(writer, &self.ast)
.map_err(|e| AstError::failed_to_write_ast_to_json_file(&path, &e))?)
}
pub fn to_json_file_without_keys(
&self,
mut path: std::path::PathBuf,
file_name: &str,
excluded_keys: &[&str],
) -> Result<()> {
path.push(file_name);
let file = std::fs::File::create(&path).map_err(|e| AstError::failed_to_create_ast_json_file(&path, &e))?;
let writer = std::io::BufWriter::new(file);
let mut value = self.to_json_value().unwrap();
for key in excluded_keys {
value = remove_key_from_json(value, key);
}
value = normalize_json_value(value);
Ok(serde_json::to_writer_pretty(writer, &value)
.map_err(|e| AstError::failed_to_write_ast_to_json_file(&path, &e))?)
}
pub fn from_json_string(json: &str) -> Result<Self> {
let ast: Program = serde_json::from_str(json).map_err(|e| AstError::failed_to_read_json_string_to_ast(&e))?;
Ok(Self { ast })
}
pub fn from_json_file(path: std::path::PathBuf) -> Result<Self> {
let data = std::fs::read_to_string(&path).map_err(|e| AstError::failed_to_read_json_file(&path, &e))?;
Self::from_json_string(&data)
}
}
impl AsRef<Program> for Ast {
fn as_ref(&self) -> &Program {
&self.ast
}
}
pub(crate) fn remove_key_from_json(value: serde_json::Value, key: &str) -> serde_json::Value {
match value {
serde_json::Value::Object(map) => serde_json::Value::Object(
map.into_iter()
.filter(|(k, _)| k != key)
.map(|(k, v)| (k, remove_key_from_json(v, key)))
.collect(),
),
serde_json::Value::Array(values) => {
serde_json::Value::Array(values.into_iter().map(|v| remove_key_from_json(v, key)).collect())
}
_ => value,
}
}
pub(crate) fn normalize_json_value(value: serde_json::Value) -> serde_json::Value {
match value {
serde_json::Value::Array(vec) => {
let orig_length = vec.len();
let mut new_vec: Vec<serde_json::Value> = vec
.into_iter()
.filter(|v| !matches!(v, serde_json::Value::Object(map) if map.is_empty()))
.map(normalize_json_value)
.collect();
if orig_length == 2 && new_vec.len() == 1 {
new_vec.pop().unwrap()
} else {
serde_json::Value::Array(new_vec)
}
}
serde_json::Value::Object(map) => {
serde_json::Value::Object(map.into_iter().map(|(k, v)| (k, normalize_json_value(v))).collect())
}
_ => value,
}
}