use std::sync::Arc;
use include_dir::{Dir, include_dir};
use crate::loader::{self, SchemaLoadError};
use crate::schema::Schema;
static BUILTIN_SCHEMAS: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/builtins/schemas");
pub(crate) fn builtin_schemas_dir() -> &'static Dir<'static> {
&BUILTIN_SCHEMAS
}
pub fn builtin_mem_template(name: &str) -> Option<serde_json::Value> {
let file = BUILTIN_SCHEMAS.get_file(format!("{name}/mem-template.json").as_str())?;
serde_json::from_slice(file.contents()).ok()
}
pub fn load_builtin_schemas() -> Result<Vec<Arc<Schema>>, SchemaLoadError> {
let mut out = Vec::new();
for dir in BUILTIN_SCHEMAS.dirs() {
let schema = load_builtin_dir(dir)?;
out.push(Arc::new(schema));
}
Ok(out)
}
fn load_builtin_dir(dir: &Dir<'_>) -> Result<Schema, SchemaLoadError> {
let manifest_file = dir.get_file(format!("{}/schema.yaml", dir.path().display()).as_str());
let manifest_text = manifest_file
.and_then(|f| f.contents_utf8())
.ok_or_else(|| SchemaLoadError::Io {
path: dir.path().join("schema.yaml"),
source: std::io::Error::new(
std::io::ErrorKind::NotFound,
"embedded schema.yaml missing or not utf-8",
),
})?;
let mut types: Vec<(String, String)> = Vec::new();
let types_path = format!("{}/types", dir.path().display());
if let Some(types_dir) = dir.get_dir(types_path.as_str()) {
for file in types_dir.files() {
if file.path().extension().and_then(|s| s.to_str()) != Some("yaml") {
continue;
}
let Some(stem) = file
.path()
.file_stem()
.and_then(|s| s.to_str())
.map(str::to_owned)
else {
continue;
};
let Some(contents) = file.contents_utf8() else {
return Err(SchemaLoadError::Io {
path: file.path().to_path_buf(),
source: std::io::Error::new(
std::io::ErrorKind::InvalidData,
"embedded type yaml is not utf-8",
),
});
};
types.push((stem, contents.to_string()));
}
}
loader::load_schema_from_memory(manifest_text, &types)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn builtin_mem_templates_carry_instance_keys_only() {
let cases = [
("planning", "phase_context"),
("project", "scope"),
("software", "stack"),
];
for (name, instance_key) in cases {
let tpl = builtin_mem_template(name)
.unwrap_or_else(|| panic!("{name} must ship a mem-template.json"));
assert!(
tpl["language"].is_string(),
"{name}: template carries language"
);
let wg = &tpl["writeGuidance"];
assert!(
wg.get(instance_key).is_some(),
"{name}: template carries instance key {instance_key}",
);
assert!(
wg.get("goal").is_none() && wg.get("avoid").is_none(),
"{name}: template must not carry the deprecated literal goal/avoid (schema owns those)",
);
}
}
#[test]
fn builtin_mem_template_absent_is_none() {
assert!(builtin_mem_template("default").is_none());
assert!(builtin_mem_template("ingest").is_none());
assert!(builtin_mem_template("not-a-builtin").is_none());
}
#[test]
fn all_builtins_still_load_with_templates_present() {
let schemas = load_builtin_schemas().expect("built-ins load");
assert!(
schemas.iter().any(|s| s.manifest.name == "planning"),
"planning still loads alongside its mem-template.json",
);
}
}