This repository was archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsnapshot.rs
54 lines (43 loc) · 1.48 KB
/
snapshot.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
use std::env;
use std::fs::read_dir;
use std::io;
use std::path::PathBuf;
use php_parser_rs::parse;
fn main() -> io::Result<()> {
let manifest = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let mut entries = read_dir(manifest.join("tests").join("fixtures"))?
.flatten()
.map(|entry| entry.path())
.filter(|entry| entry.is_dir())
.collect::<Vec<PathBuf>>();
entries.sort();
for entry in entries {
let code_filename = entry.join("code.php");
let ast_filename = entry.join("ast.txt");
let error_filename = entry.join("error.txt");
if !code_filename.exists() {
continue;
}
if ast_filename.exists() {
std::fs::remove_file(&ast_filename)?;
}
if error_filename.exists() {
std::fs::remove_file(&error_filename)?;
}
let code = std::fs::read_to_string(&code_filename)?;
match parse(&code) {
Ok(ast) => {
std::fs::write(ast_filename, format!("{:#?}\n", ast))?;
println!("✅ generated `ast.txt` for `{}`", entry.to_string_lossy());
}
Err(error) => {
std::fs::write(
error_filename,
format!("{}\n", error.report(&code, Some("code.php"), false, true)?),
)?;
println!("✅ generated `error.txt` for `{}`", entry.to_string_lossy());
}
}
}
Ok(())
}