Skip to main content

modkit/gts/
schemas.rs

1use std::io::Read;
2use tracing::info;
3use zip;
4
5/// Get core GTS schemas provided by the modkit framework.
6///
7/// These schemas are loaded from an embedded ZIP archive containing JSON schema files.
8/// The schemas are fundamental types that other modules build upon.
9/// Examples: `BaseModkitPluginV1` for plugin systems.
10///
11/// # Errors
12///
13/// Returns an error if the embedded ZIP archive cannot be read or if any schema
14/// file cannot be parsed as valid JSON.
15pub fn get_core_gts_schemas() -> anyhow::Result<Vec<serde_json::Value>> {
16    info!("Loading core GTS schemas from embedded archive");
17
18    let mut schemas = Vec::new();
19
20    // Load embedded ZIP archive created by build script
21    let zip_data = include_bytes!(concat!(env!("OUT_DIR"), "/core_gts_schemas.zip"));
22    let cursor = std::io::Cursor::new(zip_data);
23    let mut archive = zip::ZipArchive::new(cursor)?;
24
25    // Extract and parse each schema file
26    for i in 0..archive.len() {
27        let mut file = archive.by_index(i)?;
28        let file_name = file.name().to_owned();
29
30        // Skip manifest.json, only process .schema.json files
31        if file_name.ends_with(".schema.json") {
32            let mut contents = String::new();
33            file.read_to_string(&mut contents)?;
34
35            let schema_json: serde_json::Value = serde_json::from_str(&contents)
36                .map_err(|e| anyhow::anyhow!("Failed to parse schema {file_name}: {e}"))?;
37
38            schemas.push(schema_json);
39            info!("Loaded core GTS schema: {}", file_name);
40        }
41    }
42
43    if schemas.is_empty() {
44        return Err(anyhow::anyhow!(
45            "no core GTS schemas found in embedded archive"
46        ));
47    }
48
49    info!("Core GTS schemas loaded: {} schemas", schemas.len());
50    Ok(schemas)
51}