Skip to main content

llm_wiki/
default_schemas.rs

1use std::collections::HashMap;
2
3const BASE: &str = include_str!("../schemas/base.json");
4const CONCEPT: &str = include_str!("../schemas/concept.json");
5const PAPER: &str = include_str!("../schemas/paper.json");
6const SKILL: &str = include_str!("../schemas/skill.json");
7const DOC: &str = include_str!("../schemas/doc.json");
8const SECTION: &str = include_str!("../schemas/section.json");
9
10const TMPL_CONCEPT: &str = include_str!("../schemas/concept.md");
11const TMPL_PAPER: &str = include_str!("../schemas/paper.md");
12const TMPL_DOC: &str = include_str!("../schemas/doc.md");
13const TMPL_SECTION: &str = include_str!("../schemas/section.md");
14const TMPL_QUERY_RESULT: &str = include_str!("../schemas/query-result.md");
15
16/// Returns a map of schema filename → embedded JSON content.
17pub fn default_schemas() -> HashMap<&'static str, &'static str> {
18    HashMap::from([
19        ("base.json", BASE),
20        ("concept.json", CONCEPT),
21        ("paper.json", PAPER),
22        ("skill.json", SKILL),
23        ("doc.json", DOC),
24        ("section.json", SECTION),
25    ])
26}
27
28/// Returns a map of template filename → embedded Markdown content.
29pub fn default_templates() -> HashMap<&'static str, &'static str> {
30    HashMap::from([
31        ("concept.md", TMPL_CONCEPT),
32        ("paper.md", TMPL_PAPER),
33        ("doc.md", TMPL_DOC),
34        ("section.md", TMPL_SECTION),
35        ("query-result.md", TMPL_QUERY_RESULT),
36    ])
37}
38
39/// Resolve a body template for a type name. Checks embedded templates.
40pub fn embedded_body_template(type_name: &str) -> Option<&'static str> {
41    let filename = format!("{type_name}.md");
42    default_templates().get(filename.as_str()).copied()
43}
44
45/// A default type entry extracted from `x-wiki-types` in a schema.
46pub struct DefaultTypeEntry {
47    /// The type identifier (e.g. `"concept"`, `"paper"`).
48    pub type_name: String,
49    /// Relative path to the schema file that defines this type (e.g. `"schemas/concept.json"`).
50    pub schema_file: String,
51    /// Human-readable description of the type.
52    pub description: String,
53}
54
55/// Extract all default type entries from the embedded schemas.
56///
57/// Reads `x-wiki-types` from each schema file. Returns entries sorted
58/// by type name for deterministic output.
59pub fn default_type_entries() -> Vec<DefaultTypeEntry> {
60    let mut entries = Vec::new();
61    for (filename, content) in default_schemas() {
62        let schema: serde_json::Value = serde_json::from_str(content)
63            .unwrap_or_else(|e| panic!("{filename} is not valid JSON: {e}"));
64        if let Some(types) = schema.get("x-wiki-types").and_then(|v| v.as_object()) {
65            for (type_name, desc) in types {
66                entries.push(DefaultTypeEntry {
67                    type_name: type_name.clone(),
68                    schema_file: format!("schemas/{filename}"),
69                    description: desc.as_str().unwrap_or("").to_string(),
70                });
71            }
72        }
73    }
74    entries.sort_by(|a, b| a.type_name.cmp(&b.type_name));
75    entries
76}