1use std::io::{self, Write};
4
5use super::fields::FIELD_DESCRIPTORS;
6use super::namespace::{term, VIBE_NS, VIBE_SCHEMA_VERSION};
7use super::node_kinds::all_node_kinds;
8
9#[derive(Debug, Clone, Copy)]
11pub struct ExportBudget {
12 pub max_bytes: usize,
13}
14
15impl Default for ExportBudget {
16 fn default() -> Self {
17 Self {
18 max_bytes: 512 * 1024,
19 }
20 }
21}
22
23const CORE_CLASSES: &[&str] = &[
24 "LanguageVersion",
25 "Program",
26 "Module",
27 "AstNode",
28 "Declaration",
29 "FunctionDeclaration",
30 "HookDeclaration",
31 "CellDeclaration",
32 "FieldDeclaration",
33 "MaterialDeclaration",
34 "LawDeclaration",
35 "Statement",
36 "Expression",
37 "Pattern",
38 "Type",
39 "EffectClass",
40 "Capability",
41 "InvokeId",
42 "CapabilityArgument",
43 "Budget",
44 "SourceSpan",
45 "Diagnostic",
46 "Receipt",
47 "Block",
48];
49
50pub fn export_rdfs_turtle(w: &mut dyn Write, budget: ExportBudget) -> io::Result<usize> {
52 let mut buf = String::new();
53 buf.push_str("# Generated by vibe::metamodel::export — do not hand-edit.\n");
54 buf.push_str(&format!("# schema_version: {VIBE_SCHEMA_VERSION}\n"));
55 buf.push_str("@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n");
56 buf.push_str("@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n");
57 buf.push_str("@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n");
58 buf.push_str(&format!("@prefix vibe: <{VIBE_NS}> .\n\n"));
59 buf.push_str(&format!(
60 "vibe:Metamodel a rdfs:Class ;\n rdfs:label \"VibeScript metamodel\" ;\n vibe:schemaVersion \"{VIBE_SCHEMA_VERSION}\" .\n\n"
61 ));
62 for c in CORE_CLASSES {
63 buf.push_str(&format!(
64 "vibe:{c} a rdfs:Class ;\n rdfs:label \"{c}\" ;\n rdfs:isDefinedBy <{VIBE_NS}> .\n\n"
65 ));
66 }
67 for d in all_node_kinds() {
68 buf.push_str(&format!(
69 "vibe:nodeKind_{} a vibe:AstNode ;\n vibe:nodeKind \"{}\" ;\n rdfs:comment \"{}\" .\n\n",
70 d.local_name.replace('.', "_"),
71 d.local_name,
72 d.rust_path
73 ));
74 let _ = term(d.rdfs_class_local);
75 }
76 for f in FIELD_DESCRIPTORS {
77 buf.push_str(&format!(
78 "vibe:{} a rdf:Property ;\n rdfs:domain vibe:{} ;\n rdfs:range {} ;\n rdfs:label \"{}\" .\n\n",
79 f.rdfs_property_local,
80 f.domain_class,
81 if f.range_hint.starts_with("xsd:") {
82 f.range_hint.to_string()
83 } else {
84 format!("vibe:{}", f.range_hint)
85 },
86 f.local_name
87 ));
88 }
89 if buf.len() > budget.max_bytes {
90 return Err(io::Error::new(
91 io::ErrorKind::Other,
92 "metamodel RDFS export exceeds budget",
93 ));
94 }
95 w.write_all(buf.as_bytes())?;
96 Ok(buf.len())
97}
98
99pub fn export_shacl_turtle(w: &mut dyn Write, budget: ExportBudget) -> io::Result<usize> {
101 let mut buf = String::new();
102 buf.push_str("# Generated by vibe::metamodel::export — do not hand-edit.\n");
103 buf.push_str(&format!("# schema_version: {VIBE_SCHEMA_VERSION}\n"));
104 buf.push_str("@prefix sh: <http://www.w3.org/ns/shacl#> .\n");
105 buf.push_str("@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n");
106 buf.push_str(&format!("@prefix vibe: <{VIBE_NS}> .\n\n"));
107 buf.push_str(
108 "vibe:ProgramShape a sh:NodeShape ;\n sh:targetClass vibe:Program ;\n sh:property [\n sh:path vibe:languageVersion ;\n sh:minCount 1 ;\n sh:maxCount 1 ;\n sh:datatype xsd:string ;\n ] ;\n sh:property [\n sh:path vibe:contains ;\n sh:class vibe:AstNode ;\n ] .\n\n",
109 );
110 buf.push_str(
111 "vibe:AstNodeShape a sh:NodeShape ;\n sh:targetClass vibe:AstNode ;\n sh:property [\n sh:path vibe:nodeKind ;\n sh:minCount 1 ;\n sh:maxCount 1 ;\n sh:datatype xsd:string ;\n ] .\n\n",
112 );
113 buf.push_str(
114 "vibe:FunctionDeclarationShape a sh:NodeShape ;\n sh:targetClass vibe:FunctionDeclaration ;\n sh:property [\n sh:path vibe:hasEffect ;\n sh:maxCount 1 ;\n sh:class vibe:EffectClass ;\n ] ;\n sh:property [\n sh:path vibe:hasBudget ;\n sh:class vibe:Budget ;\n ] .\n\n",
115 );
116 if buf.len() > budget.max_bytes {
117 return Err(io::Error::new(
118 io::ErrorKind::Other,
119 "metamodel SHACL export exceeds budget",
120 ));
121 }
122 w.write_all(buf.as_bytes())?;
123 Ok(buf.len())
124}
125
126#[cfg(test)]
127mod tests {
128 use super::*;
129
130 #[test]
131 fn rdfs_export_deterministic_and_budgeted() {
132 let mut a = Vec::new();
133 let mut b = Vec::new();
134 let n1 = export_rdfs_turtle(&mut a, ExportBudget::default()).unwrap();
135 let n2 = export_rdfs_turtle(&mut b, ExportBudget::default()).unwrap();
136 assert_eq!(n1, n2);
137 assert_eq!(a, b);
138 assert!(a.starts_with(b"# Generated"));
139 assert!(std::str::from_utf8(&a).unwrap().contains("vibe:Program"));
140 }
141
142 #[test]
143 fn shacl_export_has_program_shape() {
144 let mut buf = Vec::new();
145 export_shacl_turtle(&mut buf, ExportBudget::default()).unwrap();
146 let s = String::from_utf8(buf).unwrap();
147 assert!(s.contains("vibe:ProgramShape"));
148 assert!(s.contains("sh:minCount 1"));
149 }
150}