Skip to main content

forge_foundation/edition/
loader.rs

1#![cfg(not(target_arch = "wasm32"))]
2
3use std::fs;
4use std::io;
5use std::path::Path;
6
7use super::editions_registry::EditionsRegistry;
8
9#[derive(Debug, Clone, Default)]
10pub struct LoadReport {
11    pub loaded: usize,
12    pub errors: Vec<String>,
13}
14
15pub fn load_editions_dir(dir: &Path, registry: &mut EditionsRegistry) -> io::Result<LoadReport> {
16    let mut report = LoadReport::default();
17    for entry in fs::read_dir(dir)? {
18        let entry = match entry {
19            Ok(e) => e,
20            Err(e) => {
21                report.errors.push(format!("read_dir entry: {e}"));
22                continue;
23            }
24        };
25        let path = entry.path();
26        if path.extension().and_then(|s| s.to_str()) != Some("txt") {
27            continue;
28        }
29        match fs::read_to_string(&path) {
30            Ok(body) => {
31                let code = registry.ingest_file(&body);
32                if code.is_empty() {
33                    report
34                        .errors
35                        .push(format!("{}: missing Code= in [metadata]", path.display()));
36                } else {
37                    report.loaded += 1;
38                }
39            }
40            Err(e) => report.errors.push(format!("{}: {e}", path.display())),
41        }
42    }
43    Ok(report)
44}