feat(runtime): lower generated runtime ids

req: wire/001

req: ts/001
This commit is contained in:
slhx agent
2026-05-10 23:04:45 +02:00
parent 9e0342951c
commit 79414fbb39
6 changed files with 208 additions and 22 deletions
+74 -2
View File
@@ -120,6 +120,7 @@ impl Resources {
self.insert_handle(canonical, name.clone(), component.clone())?;
if tag == "form" {
let form_name = static_attr(&node.attrs, "data-slhx-form").unwrap_or_else(|| name.clone());
let controls = surface
.forms
.iter()
@@ -135,7 +136,7 @@ impl Resources {
.collect()
})
.unwrap_or_default();
self.insert_form(canonical_symbol(root, path, &name), name, component.clone(), controls)?;
self.insert_form(canonical_symbol(root, path, &form_name), form_name, component.clone(), controls)?;
}
}
}
@@ -192,14 +193,17 @@ impl Resources {
// req: build/001
self.push_resource_modules(&mut out, None, 0);
self.push_lowering_api(&mut out, None, 0);
for component in self.component_names() {
out.push_str("\n#[allow(non_upper_case_globals)]\n");
out.push_str(&format!("pub mod {component} {{\n"));
self.push_resource_modules(&mut out, Some(&component), 1);
self.push_lowering_api(&mut out, Some(&component), 1);
out.push_str("}\n");
}
self.push_lowering_helpers(&mut out);
out
}
@@ -279,6 +283,68 @@ impl Resources {
out.push_str(&format!("{pad}}}\n"));
}
fn push_lowering_api(&self, out: &mut String, component: Option<&str>, indent: usize) {
let pad = " ".repeat(indent);
let table_name = match component {
Some(component) => format!("__SLHX_LOWERING_TABLE_{}", component.to_ascii_uppercase()),
None => "__SLHX_LOWERING_TABLE".to_string(),
};
out.push_str(&format!("\n{pad}pub fn lower_html(html: impl ::std::convert::AsRef<str>) -> ::std::string::String {{\n"));
out.push_str(&format!("{pad} "));
if component.is_some() {
out.push_str("super::");
}
out.push_str(&format!("__slhx_lower_html(html.as_ref(), &{table_name})\n"));
out.push_str(&format!("{pad}}}\n\n"));
self.push_lowering_table(out, component, indent, &table_name);
}
fn push_lowering_table(&self, out: &mut String, component: Option<&str>, indent: usize, name: &str) {
let pad = " ".repeat(indent);
out.push_str(&format!("{pad}const {name}: &[(&str, &str, u32)] = &[\n"));
for res in self.slots.values().filter(|res| component_matches(res, component)) {
out.push_str(&format!("{pad} (\"data-slhx-slot\", {}, {}),\n", rust_str(&res.ident), res.id));
}
for res in self.handles.values().filter(|res| component_matches(res, component)) {
out.push_str(&format!("{pad} (\"data-slhx-handle\", {}, {}),\n", rust_str(&res.ident), res.id));
}
for form in self.forms.values().filter(|form| component_matches(&form.resource, component)) {
let res = &form.resource;
out.push_str(&format!("{pad} (\"data-slhx-form\", {}, {}),\n", rust_str(&res.ident), res.id));
}
for res in self.atoms.values().filter(|res| component_matches(res, component)) {
out.push_str(&format!("{pad} (\"data-slhx-atom\", {}, {}),\n", rust_str(&res.ident), res.id));
}
out.push_str(&format!("{pad}];\n"));
}
fn push_lowering_helpers(&self, out: &mut String) {
out.push_str(r#"
fn __slhx_lower_html(html: &str, table: &[(&str, &str, u32)]) -> ::std::string::String {
let mut out = html.to_owned();
for (attr, name, id) in table {
let runtime_attr = match *attr {
"data-slhx-slot" => "data-sid",
"data-slhx-handle" => "data-hid",
"data-slhx-form" => "data-fid",
"data-slhx-atom" => "data-aid",
_ => continue,
};
out = __slhx_replace_attr(out, attr, name, runtime_attr, *id);
}
out
}
fn __slhx_replace_attr(mut html: ::std::string::String, attr: &str, name: &str, runtime_attr: &str, id: u32) -> ::std::string::String {
let replacement = ::std::format!("{runtime_attr}=\"{id}\"");
let double = ::std::format!("{attr}=\"{name}\"");
html = html.replace(&double, &replacement);
let single = ::std::format!("{attr}='{name}'");
html.replace(&single, &replacement)
}
"#);
}
fn component_names(&self) -> Vec<String> {
let mut components = Vec::new();
for component in self
@@ -572,7 +638,7 @@ mod tests {
std::fs::create_dir_all(&templates).unwrap();
std::fs::write(
templates.join("todo.heml"),
r#"<form data-slhx-handle="create"><input name="title"></form><ul data-slhx-slot="todos"></ul><section data-slhx-atom="filter"></section>"#,
r#"<form data-slhx-handle="create" data-slhx-form="new_todo"><input name="title"></form><ul data-slhx-slot="todos"></ul><section data-slhx-atom="filter"></section>"#,
)
.unwrap();
@@ -586,8 +652,14 @@ mod tests {
assert!(generated.contains("pub mod forms"));
assert!(generated.contains("pub mod atoms"));
assert!(generated.contains("pub const filter"));
assert!(generated.contains("pub const new_todo"));
assert!(generated.contains("pub mod todo"));
assert!(generated.contains("pub const ALL_IDS"));
assert!(generated.contains("pub fn lower_html"));
assert!(generated.contains("data-slhx-slot"));
assert!(generated.contains("data-slhx-form"));
assert!(generated.contains("data-sid"));
assert!(generated.contains("data-fid"));
let syms = std::fs::read_to_string(out.join("slhx.syms")).unwrap();
assert!(syms.contains("atom\t"));