Skip to main content

alembic_engine/
loader.rs

1//! inventory file loading with include/import support.
2
3use crate::{report_to_result_with_sources, validate};
4use alembic_core::{Inventory, Schema, SourceLocation};
5use anyhow::{anyhow, Context, Result};
6use serde::Deserialize;
7use std::collections::BTreeSet;
8use std::fs;
9use std::path::{Path, PathBuf};
10
11/// raw on-disk representation for a inventory file.
12#[derive(Debug, Deserialize)]
13struct InventoryFile {
14    #[serde(default)]
15    include: Vec<String>,
16    #[serde(default)]
17    imports: Vec<String>,
18    #[serde(default)]
19    schema: Option<Schema>,
20    #[serde(default)]
21    objects: Vec<alembic_core::Object>,
22}
23
24/// load a inventory file (yaml or json) and merge any includes.
25pub fn load_inventory(path: impl AsRef<Path>) -> Result<Inventory> {
26    let mut visited = BTreeSet::new();
27    let mut objects = Vec::new();
28    let mut schema: Option<Schema> = None;
29    let path = path.as_ref();
30    load_recursive(path, &mut visited, &mut objects, &mut schema)?;
31    let schema = schema.ok_or_else(|| anyhow!("inventory is missing a schema block"))?;
32    let inventory = Inventory { schema, objects };
33    report_to_result_with_sources(validate(&inventory), &inventory.objects)?;
34    Ok(inventory)
35}
36
37/// recursive loader with cycle-safe include handling.
38fn load_recursive(
39    path: &Path,
40    visited: &mut BTreeSet<PathBuf>,
41    objects: &mut Vec<alembic_core::Object>,
42    schema: &mut Option<Schema>,
43) -> Result<()> {
44    let canonical =
45        fs::canonicalize(path).with_context(|| format!("load inventory: {}", path.display()))?;
46    if !visited.insert(canonical.clone()) {
47        return Ok(());
48    }
49
50    let content = fs::read_to_string(&canonical)
51        .with_context(|| format!("read inventory: {}", canonical.display()))?;
52    let inventory: InventoryFile = if canonical.extension().and_then(|s| s.to_str()) == Some("json")
53    {
54        serde_json::from_str(&content)
55            .with_context(|| format!("parse json: {}", canonical.display()))?
56    } else {
57        serde_yaml::from_str(&content)
58            .with_context(|| format!("parse yaml: {}", canonical.display()))?
59    };
60
61    let base = canonical
62        .parent()
63        .ok_or_else(|| anyhow!("missing parent dir for {}", canonical.display()))?;
64
65    let mut includes = inventory.include;
66    includes.extend(inventory.imports);
67
68    for entry in includes {
69        let include_path = base.join(entry);
70        load_recursive(&include_path, visited, objects, schema)?;
71    }
72
73    merge_schema(schema, inventory.schema)?;
74
75    // set source location on each object from this file, with line numbers
76    for object in inventory.objects {
77        let line = find_uid_line(&content, &object.uid.to_string());
78        let source = match line {
79            Some(n) => SourceLocation::file_line(&canonical, n),
80            None => SourceLocation::file(&canonical),
81        };
82        objects.push(object.with_source(source));
83    }
84
85    Ok(())
86}
87
88/// find the line number (1-indexed) where a UID appears in the content.
89fn find_uid_line(content: &str, uid: &str) -> Option<usize> {
90    for (idx, line) in content.lines().enumerate() {
91        if line.contains(uid) {
92            return Some(idx + 1); // 1-indexed line numbers
93        }
94    }
95    None
96}
97
98fn merge_schema(current: &mut Option<Schema>, incoming: Option<Schema>) -> Result<()> {
99    let Some(incoming) = incoming else {
100        return Ok(());
101    };
102    match current {
103        Some(existing) => {
104            for (name, schema) in incoming.types {
105                if existing.types.contains_key(&name) {
106                    return Err(anyhow!("duplicate schema type {name}"));
107                }
108                existing.types.insert(name, schema);
109            }
110        }
111        None => {
112            *current = Some(incoming);
113        }
114    }
115    Ok(())
116}