dir parsing test example

This commit is contained in:
Robin Appelman 2023-12-21 18:54:11 +01:00
commit 4629d01d0f
3 changed files with 76 additions and 4 deletions

42
examples/dir.rs Normal file
View file

@ -0,0 +1,42 @@
use miette::{Context, IntoDiagnostic, Result};
use std::env::args;
use std::fs::read_to_string;
use std::path::Path;
use vmt_parser::from_str;
use vmt_parser::material::Material;
use walkdir::WalkDir;
fn main() -> Result<()> {
let mut success = 0;
let mut err = Vec::new();
let dir = args().nth(1).expect("no path provided");
for entry in WalkDir::new(dir)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.file_name().to_str().unwrap_or_default().ends_with(".vmt"))
{
if let Err(e) = try_parse(entry.path()) {
err.push(e);
let e = try_parse(entry.path()).unwrap_err();
println!("{:?}", e);
} else {
success += 1;
println!("{}", entry.path().display());
}
}
println!("successfully parsed {success} files");
println!("found errors in {} files", err.len());
for e in err {
println!("{:?}", e);
}
Ok(())
}
fn try_parse(path: &Path) -> Result<Material> {
let raw = read_to_string(path)
.into_diagnostic()
.wrap_err_with(|| format!("failed to read {}", path.display()))?;
from_str(&raw).into_diagnostic()
}