read_all_schemas/
read_all_schemas.rs

1use std::path::PathBuf;
2
3use brdb::{BrFsReader, Brdb, IntoReader, schema::BrdbSchema};
4
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6    // world file from argv
7    let filename = std::env::args()
8        .nth(1)
9        .unwrap_or_else(|| "world.brdb".to_string());
10    let path = PathBuf::from(filename);
11    if !path.exists() {
12        eprintln!("File does not exist: {}", path.display());
13        std::process::exit(1);
14    }
15
16    let db = Brdb::open(&path)?.into_reader();
17
18    let schemas_dir = path.with_extension("schemas");
19    if schemas_dir.exists() {
20        std::fs::remove_dir_all(&schemas_dir)?;
21    }
22    std::fs::create_dir_all(&schemas_dir)?;
23
24    db.get_fs()?.for_each(&mut |f| {
25        if !f.is_file() || !f.name().ends_with(".schema") {
26            return;
27        }
28        println!("Reading schema file: {}... ", f.name());
29
30        let Ok(buf) = f.read(&*db) else {
31            eprintln!("Failed to read schema file: {}", f.name());
32            return;
33        };
34
35        let Ok(schema) = BrdbSchema::read(buf.as_slice()) else {
36            eprintln!("Failed to parse schema file: {}", f.name());
37            return;
38        };
39        let soa = schema
40            .structs
41            .last()
42            .unwrap()
43            .0
44            .get(&schema)
45            .unwrap()
46            .to_string();
47
48        if let Err(e) = std::fs::write(
49            schemas_dir.join(soa).with_extension("schema"),
50            schema.to_string(),
51        ) {
52            eprintln!("Failed to write schema file: {}: {}", f.name(), e);
53        }
54    });
55
56    Ok(())
57}