-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathutils.rs
More file actions
419 lines (364 loc) · 12.9 KB
/
utils.rs
File metadata and controls
419 lines (364 loc) · 12.9 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use log::error;
use regex::Regex;
use std::{
fs::{self, File, OpenOptions},
io::{self, BufRead, BufReader, ErrorKind, Read, Seek, Write},
path::{Path, PathBuf},
};
// Host
pub const IO_WRITE: &str = "zk_rust_io::write";
pub const IO_OUT: &str = "zk_rust_io::out();";
pub const HOST_INPUT: &str = "// INPUT //";
pub const HOST_OUTPUT: &str = "// OUTPUT //";
// I/O Markers
pub const IO_READ: &str = "zk_rust_io::read();";
pub const IO_COMMIT: &str = "zk_rust_io::commit";
pub const OUTPUT_FUNC: &str = r"pub fn output() {";
pub const INPUT_FUNC: &str = r"pub fn input() {";
pub fn prepend(file_path: &str, text_to_prepend: &str) -> io::Result<()> {
// Open the file in read mode to read its existing content
let mut file = OpenOptions::new().read(true).write(true).open(file_path)?;
// Read the existing content of the file
let mut content = String::new();
file.read_to_string(&mut content)?;
// Move the file cursor to the beginning of the file
file.seek(io::SeekFrom::Start(0))?;
// Write the text to prepend followed by the existing content back to the file
file.write_all(text_to_prepend.as_bytes())?;
file.write_all(content.as_bytes())?;
file.flush()?;
Ok(())
}
pub fn replace(file_path: &PathBuf, search_string: &str, replace_string: &str) -> io::Result<()> {
// Read the contents of the file
let mut contents = String::new();
fs::File::open(file_path)?.read_to_string(&mut contents)?;
// Replace all occurrences of the search string with the replace string
let new_contents = contents.replace(search_string, replace_string);
// Write the new contents back to the file
let mut file = fs::File::create(file_path)?;
file.write_all(new_contents.as_bytes())?;
Ok(())
}
fn copy_dir_all(src: &impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
fs::create_dir_all(&dst)?;
for entry in fs::read_dir(src)? {
let entry = entry?;
let ty = entry.file_type()?;
if ty.is_dir() {
copy_dir_all(&entry.path(), dst.as_ref().join(entry.file_name()))?;
} else {
fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
}
}
Ok(())
}
pub fn insert(target_file: &str, text: &str, search_string: &str) -> io::Result<()> {
// Read the contents of the target file
let mut target_contents = String::new();
fs::File::open(target_file)?.read_to_string(&mut target_contents)?;
// Find the position of the search string in the target file
if let Some(pos) = target_contents.find(search_string) {
// Split the target contents into two parts
let (before, after) = target_contents.split_at(pos + search_string.len());
// Combine the parts with the insert contents
let new_contents = format!("{}\n{}\n{}", before, text, after);
// Write the new contents back to the target file
let mut file = fs::File::create(target_file)?;
file.write_all(new_contents.as_bytes())?;
} else {
println!("Search string not found in target file.");
}
Ok(())
}
//Note: Works with a one off '{' not with '}'
pub fn extract_function_bodies(
file_path: &PathBuf,
functions: Vec<String>,
) -> io::Result<Vec<String>> {
// Read the contents of the target file
let mut code = String::new();
fs::File::open(file_path)?.read_to_string(&mut code)?;
let mut start_indices = vec![];
let mut index = 0;
// Find all start indices of the function signature
for keyword in functions {
if let Some(start_index) = code[index..].find(&keyword) {
let absolute_index = index + start_index;
start_indices.push(absolute_index);
index = absolute_index + keyword.len();
}
}
// Extract the code for each function
let mut extracted_codes = vec![];
for &start_index in &start_indices {
if let Some(start_brace_index) = code[start_index..].find('{') {
let start_brace_index = start_index + start_brace_index;
let mut stack = vec!["{"];
let mut end_index = start_brace_index;
for (i, ch) in code[start_brace_index + 1..].chars().enumerate() {
if handle_stack(ch, &mut stack) {
end_index = start_brace_index + 1 + i;
break;
}
}
let extracted_code = &code[start_brace_index + 1..end_index].trim();
extracted_codes.push(extracted_code.to_string());
}
}
Ok(extracted_codes)
}
// Function that handles the stack and status when parsing the file to extract_function_bodies
fn handle_stack(ch: char, stack: &mut Vec<&str>) -> bool {
match stack.last() {
Some(&"{") => return handle_char(ch, stack),
Some(&"/") => match ch {
'/' => {
stack.pop();
stack.push("//comment");
}
'*' => {
stack.pop();
stack.push("/*comment*\\");
}
_ => {
stack.pop();
handle_char(ch, stack);
}
},
Some(&"//comment") => match ch {
'\n' => {
stack.pop();
}
_ => {}
},
Some(&"/*comment*\\") => match ch {
'*' => {
stack.push("*");
}
_ => {}
},
Some(&"*") => {
match ch {
'/' => {
stack.pop(); //pop("*")
stack.pop(); //pop("/*comment*\\")
}
_ => {
stack.pop(); //pop("*"), back to "/*comment*\\"
}
}
}
Some(&"\"string\"") => match ch {
'\"' => {
stack.pop();
}
_ => {}
},
Some(&"\'c\'") => match ch {
'\'' => {
stack.pop();
}
_ => {}
},
_ => {}
}
false
}
// Function to handle characters when in normal status of the stack
fn handle_char(ch: char, stack: &mut Vec<&str>) -> bool {
match ch {
'/' => {
stack.push("/");
}
'{' => stack.push("{"),
'}' => {
stack.pop();
if stack.is_empty() {
return true;
}
}
'\"' => {
stack.push("\"string\"");
}
'\'' => {
stack.push("\'c\'");
}
_ => {}
}
false
}
fn copy_dependencies(toml_path: &Path, guest_toml_path: &Path) -> io::Result<()> {
let mut toml = std::fs::File::open(toml_path)?;
let mut content = String::new();
toml.read_to_string(&mut content)?;
match content.find("[dependencies]") {
Some(start_index) => {
// Get all text after the search string
let dependencies = &content[start_index + "[dependencies]".len()..];
// Open the output file in append mode
let mut guest_toml = OpenOptions::new()
.create(true)
.append(true)
.open(guest_toml_path)?;
// Write the text after the search string to the output file
guest_toml.write_all(dependencies.as_bytes())
}
None => Err(io::Error::other(
"Failed to find `[dependencies]` in project Cargo.toml",
)),
}
}
pub fn prepare_workspace(
guest_path: &Path,
workspace_guest_dir: &Path,
program_toml_dir: &Path,
workspace_host_dir: &Path,
host_toml_dir: &Path,
base_host_toml_dir: &Path,
base_guest_toml_dir: &Path,
) -> io::Result<()> {
let workspace_guest_src_dir = workspace_guest_dir.join("src");
let workspace_host_src_dir = workspace_host_dir.join("src");
if let Err(e) = fs::remove_dir_all(&workspace_guest_src_dir) {
if e.kind() != ErrorKind::NotFound {
return Err(e);
}
}
if let Err(e) = fs::remove_dir_all(&workspace_host_src_dir) {
if e.kind() != ErrorKind::NotFound {
return Err(e);
}
}
// Copy src/ directory
let src_dir_path = guest_path.join("src");
copy_dir_all(&src_dir_path, workspace_guest_src_dir)?;
copy_dir_all(&src_dir_path, workspace_host_src_dir)?;
// Copy lib/ if present
let lib_dir_path = guest_path.join("lib");
if Path::new(&lib_dir_path).exists() {
let workspace_guest_lib_dir = workspace_guest_dir.join("lib");
let workspace_host_lib_dir = workspace_host_dir.join("lib");
copy_dir_all(&lib_dir_path, workspace_guest_lib_dir)?;
copy_dir_all(&lib_dir_path, workspace_host_lib_dir)?;
}
// Copy Cargo.toml for zkVM
fs::copy(base_guest_toml_dir, program_toml_dir)?;
println!("{:?} {:?}", base_guest_toml_dir, program_toml_dir);
fs::copy(base_host_toml_dir, host_toml_dir)?;
// Select dependencies from the
let toml_path = guest_path.join("Cargo.toml");
copy_dependencies(&toml_path, program_toml_dir)?;
copy_dependencies(&toml_path, host_toml_dir)?;
Ok(())
}
//TODO: refactor this to eliminate the clone at each step.
pub fn get_imports(filename: &PathBuf) -> io::Result<String> {
// Open the file
let file = File::open(filename)?;
let mut lines = BufReader::new(file).lines();
let mut imports = String::new();
// Read the file line by line
while let Some(line) = lines.next() {
let mut line = line?;
// Check if the line starts with "use "
if line.trim_start().starts_with("use ")
|| line.trim_start().starts_with("pub mod ")
|| line.trim_start().starts_with("mod ")
{
line.push('\n');
imports.push_str(&line.clone());
// check if line does not contains a use declarator and a ';'
// if not continue reading till one is found this covers the case where import statements cover multiple lines
if !line.contains(';') {
// Iterate and continue adding lines to the import while line does not contain a ';' break if it does
for line in lines.by_ref() {
let mut line = line?;
line.push('\n');
imports.push_str(&line.clone());
if line.contains(';') {
break;
}
}
}
}
}
Ok(imports)
}
pub fn extract_regex(file_path: &PathBuf, regex: &str) -> io::Result<Vec<String>> {
let file = fs::File::open(file_path)?;
let reader = io::BufReader::new(file);
let mut values = Vec::new();
let regex = Regex::new(regex).map_err(io::Error::other)?;
for line in reader.lines() {
let line = line?;
for cap in regex.captures_iter(&line) {
if let Some(matched) = cap.get(1) {
values.push(matched.as_str().to_string());
}
}
}
Ok(values)
}
//Change to remove regex and remove the marker
pub fn remove_lines(file_path: &PathBuf, target: &str) -> io::Result<()> {
// Read the file line by line
let file = fs::File::open(file_path)?;
let reader = io::BufReader::new(file);
// Collect lines that do not contain the target string
let lines: Vec<String> = reader
.lines()
.map_while(Result::ok)
.filter(|line| !line.contains(target))
.collect();
// Write the filtered lines back to the file
let mut file = fs::File::create(file_path)?;
for line in lines {
writeln!(file, "{}", line)?;
}
Ok(())
}
pub fn validate_directory_structure(root: &str) -> bool {
let root = Path::new(root);
// Check if Cargo.toml exists in the root directory
let cargo_toml = root.join("Cargo.toml");
if !cargo_toml.exists() {
error!("Cargo.toml not found.");
return false;
}
// Check if src/ and lib/ directories exist
let src_dir = root.join("src");
if !src_dir.exists() {
error!("src/ directory not found in root");
return false;
}
// Check if src/ contains main.rs file
let main_rs = src_dir.join("main.rs");
if !main_rs.exists() {
error!("main.rs not found in src/ directory in root");
return false;
}
true
}
pub fn prepare_guest(
imports: &str,
main_func_code: &str,
program_header: &str,
io_read_header: &str,
io_commit_header: &str,
guest_main_file_path: &PathBuf,
) -> io::Result<()> {
let mut guest_program = program_header.to_string();
guest_program.push_str(imports);
guest_program.push_str("pub fn main() {\n");
guest_program.push_str(main_func_code);
guest_program.push_str("\n}");
// Replace zkRust::read()
let guest_program = guest_program.replace(IO_READ, io_read_header);
// Replace zkRust::commit()
let guest_program = guest_program.replace(IO_COMMIT, io_commit_header);
// Write to guest
let mut file = fs::File::create(guest_main_file_path)?;
file.write_all(guest_program.as_bytes())?;
Ok(())
}