use crate::ast::*;
pub fn generate_rsx(nodes: &[RsxNode]) -> Result<String, Vec<String>> {
let mut errors = Vec::new();
let code = generate_rsx_inner(nodes, &mut errors);
if errors.is_empty() {
Ok(code)
} else {
Err(errors)
}
}
fn generate_rsx_inner(nodes: &[RsxNode], errors: &mut Vec<String>) -> String {
if nodes.is_empty() {
return "strata_dom::View::new(strata_dom::NodeBlueprint::Empty)".to_string();
}
if nodes.len() == 1 {
let node_code = lower_node(&nodes[0], errors);
format!("strata_dom::View::new({})", node_code)
} else {
let mut children_code = Vec::new();
for node in nodes {
children_code.push(lower_node(node, errors));
}
format!(
"strata_dom::View::new(strata_dom::NodeBlueprint::fragment(vec![{}]))",
children_code.join(", ")
)
}
}
fn lower_node(node: &RsxNode, errors: &mut Vec<String>) -> String {
match node {
RsxNode::Text(txt) => {
format!("strata_dom::NodeBlueprint::text({:?})", txt)
}
RsxNode::Expression(stream) => {
format!(
"strata_dom::IntoNodeBlueprint::into_node_blueprint({{ {} }})",
stream
)
}
RsxNode::Fragment(children) => {
let mut children_code = Vec::new();
for child in children {
children_code.push(lower_node(child, errors));
}
format!(
"strata_dom::NodeBlueprint::fragment(vec![{}])",
children_code.join(", ")
)
}
RsxNode::Element(el) => lower_element(el, errors),
RsxNode::Component(comp) => lower_component(comp, errors),
}
}
fn normalize_event_name(name: &str) -> String {
name.to_ascii_lowercase().replace('_', "")
}
fn lower_element(el: &RsxElement, errors: &mut Vec<String>) -> String {
let tag_code = match el.tag.to_ascii_lowercase().as_str() {
"div" => "strata_dom::Tag::Div".to_string(),
"span" => "strata_dom::Tag::Span".to_string(),
"button" => "strata_dom::Tag::Button".to_string(),
"input" => "strata_dom::Tag::Input".to_string(),
"select" => "strata_dom::Tag::Select".to_string(),
"option" => "strata_dom::Tag::Option".to_string(),
"textarea" => "strata_dom::Tag::Textarea".to_string(),
"label" => "strata_dom::Tag::Label".to_string(),
"form" => "strata_dom::Tag::Form".to_string(),
"header" => "strata_dom::Tag::Header".to_string(),
"nav" => "strata_dom::Tag::Nav".to_string(),
"main" => "strata_dom::Tag::Main".to_string(),
"section" => "strata_dom::Tag::Section".to_string(),
"aside" => "strata_dom::Tag::Aside".to_string(),
"footer" => "strata_dom::Tag::Footer".to_string(),
"h1" => "strata_dom::Tag::H1".to_string(),
"h2" => "strata_dom::Tag::H2".to_string(),
"h3" => "strata_dom::Tag::H3".to_string(),
"h4" => "strata_dom::Tag::H4".to_string(),
"h5" => "strata_dom::Tag::H5".to_string(),
"h6" => "strata_dom::Tag::H6".to_string(),
"p" => "strata_dom::Tag::P".to_string(),
"ul" => "strata_dom::Tag::Ul".to_string(),
"ol" => "strata_dom::Tag::Ol".to_string(),
"li" => "strata_dom::Tag::Li".to_string(),
"a" => "strata_dom::Tag::A".to_string(),
"img" => "strata_dom::Tag::Img".to_string(),
"audio" => "strata_dom::Tag::Audio".to_string(),
"video" => "strata_dom::Tag::Video".to_string(),
"svg" => "strata_dom::Tag::Svg".to_string(),
"path" => "strata_dom::Tag::Path".to_string(),
"canvas" => "strata_dom::Tag::Canvas".to_string(),
"table" => "strata_dom::Tag::Table".to_string(),
"thead" => "strata_dom::Tag::Thead".to_string(),
"tbody" => "strata_dom::Tag::Tbody".to_string(),
"tr" => "strata_dom::Tag::Tr".to_string(),
"th" => "strata_dom::Tag::Th".to_string(),
"td" => "strata_dom::Tag::Td".to_string(),
"hr" => "strata_dom::Tag::Hr".to_string(),
"br" => "strata_dom::Tag::Br".to_string(),
_ => format!("strata_dom::Tag::Custom({:?})", el.tag),
};
let mut classes_code = "strata_dom::ClassList::Empty".to_string();
let mut other_attrs = Vec::new();
for attr in &el.attributes {
if attr.name == "class" || attr.name == "className" {
match &attr.value {
RsxAttrValue::Literal(s) => {
classes_code = crate::classes::emit_static_class_list(s);
}
RsxAttrValue::Expression(stream) => {
classes_code = format!(
"strata_dom::ClassList::Dynamic({{ let _s = ({}).to_string(); _s.split_whitespace().map(|c| c.to_string()).collect::<Vec<String>>() }})",
stream
);
}
RsxAttrValue::Flag | RsxAttrValue::Spread(_) => {}
}
} else {
other_attrs.push(attr);
}
}
let has_spread = other_attrs
.iter()
.any(|a| matches!(a.value, RsxAttrValue::Spread(_)));
let mut attrs_code = Vec::new();
let mut spread_stmts = Vec::new();
for attr in other_attrs {
if let RsxAttrValue::Spread(stream) = &attr.value {
spread_stmts.push(format!("__attrs.extend({});", stream));
continue;
}
let item_expr = if attr.name.starts_with("on") {
let event_name = normalize_event_name(&attr.name);
let event_type = match event_name.as_str() {
"onclick" => "strata_dom::EventType::Click".to_string(),
"ondblclick" => "strata_dom::EventType::DblClick".to_string(),
"oncontextmenu" => "strata_dom::EventType::ContextMenu".to_string(),
"onauxclick" => "strata_dom::EventType::AuxClick".to_string(),
"onmousedown" => "strata_dom::EventType::MouseDown".to_string(),
"onmouseup" => "strata_dom::EventType::MouseUp".to_string(),
"onmousemove" => "strata_dom::EventType::MouseMove".to_string(),
"onmouseenter" => "strata_dom::EventType::MouseEnter".to_string(),
"onmouseleave" => "strata_dom::EventType::MouseLeave".to_string(),
"onmouseover" => "strata_dom::EventType::MouseOver".to_string(),
"onmouseout" => "strata_dom::EventType::MouseOut".to_string(),
"oninput" => "strata_dom::EventType::Input".to_string(),
"onchange" => "strata_dom::EventType::Change".to_string(),
"onsubmit" => "strata_dom::EventType::Submit".to_string(),
"onkeydown" => "strata_dom::EventType::KeyDown".to_string(),
"onkeyup" => "strata_dom::EventType::KeyUp".to_string(),
"onpointerdown" => "strata_dom::EventType::PointerDown".to_string(),
"onpointerup" => "strata_dom::EventType::PointerUp".to_string(),
"onpointermove" => "strata_dom::EventType::PointerMove".to_string(),
"onfocus" => "strata_dom::EventType::Focus".to_string(),
"onblur" => "strata_dom::EventType::Blur".to_string(),
"onwheel" => "strata_dom::EventType::Wheel".to_string(),
"onscroll" => "strata_dom::EventType::Scroll".to_string(),
"ondragstart" => "strata_dom::EventType::DragStart".to_string(),
"ondrag" => "strata_dom::EventType::Drag".to_string(),
"ondragenter" => "strata_dom::EventType::DragEnter".to_string(),
"ondragover" => "strata_dom::EventType::DragOver".to_string(),
"ondragleave" => "strata_dom::EventType::DragLeave".to_string(),
"ondrop" => "strata_dom::EventType::Drop".to_string(),
"ondragend" => "strata_dom::EventType::DragEnd".to_string(),
"ontransitionrun" => "strata_dom::EventType::TransitionRun".to_string(),
"ontransitionstart" => "strata_dom::EventType::TransitionStart".to_string(),
"ontransitionend" => "strata_dom::EventType::TransitionEnd".to_string(),
"ontransitioncancel" => "strata_dom::EventType::TransitionCancel".to_string(),
"onanimationstart" => "strata_dom::EventType::AnimationStart".to_string(),
"onanimationiteration" => "strata_dom::EventType::AnimationIteration".to_string(),
"onanimationend" => "strata_dom::EventType::AnimationEnd".to_string(),
"onanimationcancel" => "strata_dom::EventType::AnimationCancel".to_string(),
"onresize" => "strata_dom::EventType::Resize".to_string(),
_ => {
errors.push(format!(
"unsupported event attribute `{}`; expected a known DOM event such as `onclick` or `on_click`",
attr.name
));
continue;
}
};
match &attr.value {
RsxAttrValue::Expression(stream) => {
let method = if event_name == "oninput"
|| event_name == "onchange"
|| event_name == "onsubmit"
{
"value_listener"
} else {
"event_listener"
};
format!("strata_dom::Attribute::{}({}, {})", method, event_type, stream)
}
RsxAttrValue::Literal(_) | RsxAttrValue::Flag | RsxAttrValue::Spread(_) => {
errors.push(format!(
"event attribute `{}` requires a callable expression, e.g. `{0}={{|_event| {{}}}}`; a literal string or flag is not a handler",
attr.name
));
continue;
}
}
} else {
match &attr.value {
RsxAttrValue::Literal(s) => {
format!("strata_dom::Attribute::property({:?}, {:?})", attr.name, s)
}
RsxAttrValue::Expression(stream) => {
format!(
"strata_dom::Attribute::property({:?}, ({}).to_string())",
attr.name, stream
)
}
RsxAttrValue::Flag => {
format!("strata_dom::Attribute::property({:?}, \"true\")", attr.name)
}
RsxAttrValue::Spread(_) => unreachable!(),
}
};
if has_spread {
spread_stmts.push(format!("__attrs.push({});", item_expr));
} else {
attrs_code.push(item_expr);
}
}
let attrs_expr = if has_spread {
format!(
"{{ let mut __attrs: Vec<strata_dom::Attribute> = Vec::new(); {} __attrs }}",
spread_stmts.join(" ")
)
} else {
format!("vec![{}]", attrs_code.join(", "))
};
let mut children_code = Vec::new();
for child in &el.children {
children_code.push(lower_node(child, errors));
}
format!(
"strata_dom::NodeBlueprint::element({}, {}, {}, vec![{}])",
tag_code,
classes_code,
attrs_expr,
children_code.join(", ")
)
}
fn lower_component(comp: &RsxComponent, errors: &mut Vec<String>) -> String {
let full_path = comp.full_path();
let last_is_upper = comp
.path
.last()
.is_some_and(|seg| segment_starts_uppercase(seg));
let penultimate_is_upper =
comp.path.len() >= 2 && segment_starts_uppercase(&comp.path[comp.path.len() - 2]);
if penultimate_is_upper {
if comp.props.is_empty() && comp.children.is_empty() {
return format!(
"strata_dom::IntoNodeBlueprint::into_node_blueprint({}(None))",
full_path
);
}
return lower_positional_call(&full_path, comp, errors);
}
if !last_is_upper {
return lower_positional_call(&full_path, comp, errors);
}
let mut builder_calls = Vec::new();
for prop in &comp.props {
if let RsxAttrValue::Spread(stream) = &prop.value {
builder_calls.push(format!(".__spread({})", stream));
continue;
}
let prop_name = prop.name.replace('-', "_");
match &prop.value {
RsxAttrValue::Literal(s) => {
builder_calls.push(format!(".{}({:?})", prop_name, s));
}
RsxAttrValue::Expression(stream) => {
let stream_str = stream.to_string();
let routed_closure = if prop_name != "onclick" && !prop_name.ends_with("_fn") {
extract_inline_closure_expr(&stream_str)
} else {
None
};
if let Some(closure_expr) = routed_closure {
builder_calls.push(format!(".{}_fn({})", prop_name, closure_expr));
} else {
builder_calls.push(format!(".{}({})", prop_name, stream_str));
}
}
RsxAttrValue::Flag => {
builder_calls.push(format!(".{}(true)", prop_name));
}
RsxAttrValue::Spread(_) => unreachable!(),
}
}
let is_error_boundary = comp.path.last().is_some_and(|s| s == "ErrorBoundary");
let has_explicit_render = comp.props.iter().any(|p| {
let norm = p.name.replace('-', "_");
norm == "render" || norm == "render_fn"
});
let lazy_error_boundary_children =
is_error_boundary && !comp.children.is_empty() && !has_explicit_render;
let finish_call = if lazy_error_boundary_children {
let mut children_code = Vec::new();
for child in &comp.children {
children_code.push(lower_node(child, errors));
}
let body = if children_code.len() == 1 {
format!("strata_dom::View::new({})", children_code[0])
} else {
format!(
"strata_dom::View::new(strata_dom::NodeBlueprint::fragment(vec![{}]))",
children_code.join(", ")
)
};
format!(".build_with_render(|| {{ {} }})", body)
} else {
if !comp.children.is_empty() {
let mut children_code = Vec::new();
for child in &comp.children {
children_code.push(lower_node(child, errors));
}
builder_calls.push(format!(".children(vec![{}])", children_code.join(", ")));
}
".build()".to_string()
};
let builder_prefix = match &comp.generics {
Some(g) => format!("{}::{}", full_path, g),
None => full_path,
};
format!(
"strata_dom::IntoNodeBlueprint::into_node_blueprint({}::builder(){}{})",
builder_prefix,
builder_calls.join(""),
finish_call
)
}
fn is_top_level_closure_literal(expr: &str) -> bool {
let t = expr.trim();
if t.starts_with('|') {
return true;
}
if let Some(rest) = t.strip_prefix("move") {
if rest.starts_with(|c: char| c.is_whitespace() || c == '|')
&& rest.trim_start().starts_with('|')
{
return true;
}
}
false
}
fn strip_outer_delim(expr: &str, open: char, close: char) -> Option<&str> {
let t = expr.trim();
if !t.starts_with(open) || !t.ends_with(close) || t.len() < 2 {
return None;
}
let mut depth = 0i32;
let mut in_str = false;
let mut in_char = false;
let mut escape = false;
for (byte_idx, ch) in t.char_indices() {
if escape {
escape = false;
continue;
}
if in_str {
if ch == '\\' {
escape = true;
} else if ch == '"' {
in_str = false;
}
continue;
}
if in_char {
if ch == '\\' {
escape = true;
} else if ch == '\'' {
in_char = false;
}
continue;
}
match ch {
'"' => in_str = true,
ch if ch == open => depth += 1,
ch if ch == close => {
depth -= 1;
if depth == 0 && byte_idx + ch.len_utf8() < t.len() {
return None;
}
}
_ => {}
}
}
if depth == 0 {
Some(t[open.len_utf8()..t.len() - close.len_utf8()].trim())
} else {
None
}
}
fn rfind_top_level_semicolon(body: &str) -> Option<usize> {
let mut depth = 0i32;
let mut in_str = false;
let mut escape = false;
let mut last_semi = None;
for (byte_idx, ch) in body.char_indices() {
if escape {
escape = false;
continue;
}
if in_str {
if ch == '\\' {
escape = true;
} else if ch == '"' {
in_str = false;
}
continue;
}
match ch {
'"' => in_str = true,
'(' | '[' | '{' => depth += 1,
')' | ']' | '}' => depth -= 1,
';' if depth == 0 => last_semi = Some(byte_idx),
_ => {}
}
}
last_semi
}
fn extract_inline_closure_expr(expr: &str) -> Option<String> {
let t = expr.trim();
if t.is_empty() {
return None;
}
if is_top_level_closure_literal(t) {
return Some(t.to_string());
}
for prefix in [
"Some",
"Option::Some",
"Option :: Some",
"std::option::Option::Some",
"std :: option :: Option :: Some",
"core::option::Option::Some",
"core :: option :: Option :: Some",
"Rc::new",
"Rc :: new",
"std::rc::Rc::new",
"std :: rc :: Rc :: new",
"alloc::rc::Rc::new",
"alloc :: rc :: Rc :: new",
] {
if let Some(rest) = t.strip_prefix(prefix) {
let rest = rest.trim_start();
if rest.starts_with('(') {
if let Some(inner) = strip_outer_delim(rest, '(', ')') {
return extract_inline_closure_expr(inner);
}
}
}
}
if t.starts_with('(') {
if let Some(inner) = strip_outer_delim(t, '(', ')') {
return extract_inline_closure_expr(inner);
}
}
if t.starts_with('{') {
if let Some(body) = strip_outer_delim(t, '{', '}') {
if let Some(norm) = extract_inline_closure_expr(body) {
return Some(format!("{{ {} }}", norm));
}
if let Some(semi_idx) = rfind_top_level_semicolon(body) {
let stmts = body[..=semi_idx].trim();
let tail = body[semi_idx + 1..].trim();
if !tail.is_empty() {
if let Some(norm_tail) = extract_inline_closure_expr(tail) {
return Some(format!("{{ {} {} }}", stmts, norm_tail));
}
}
}
}
}
None
}
fn lower_positional_call(full_path: &str, comp: &RsxComponent, errors: &mut Vec<String>) -> String {
let mut arg_exprs = Vec::new();
for prop in &comp.props {
match &prop.value {
RsxAttrValue::Literal(s) => arg_exprs.push(format!("{:?}", s)),
RsxAttrValue::Expression(stream) => arg_exprs.push(format!("{}", stream)),
RsxAttrValue::Flag => arg_exprs.push("true".to_string()),
RsxAttrValue::Spread(_) => {
errors.push(format!(
"spread attributes `{{..expr}}` are only supported on elements and `#[component]` builders, not positional call `{}`",
full_path
));
}
}
}
if !comp.children.is_empty() {
let mut children_code = Vec::new();
for child in &comp.children {
children_code.push(lower_node(child, errors));
}
arg_exprs.push(format!("vec![{}]", children_code.join(", ")));
}
let call_prefix = match &comp.generics {
Some(g) => format!("{}::{}", full_path, g),
None => full_path.to_string(),
};
format!(
"strata_dom::IntoNodeBlueprint::into_node_blueprint({}({}))",
call_prefix,
arg_exprs.join(", ")
)
}
#[cfg(test)]
mod tests {
use super::*;
fn button_with(name: &str, value: RsxAttrValue) -> Vec<RsxNode> {
vec![RsxNode::Element(RsxElement {
tag: "button".to_string(),
attributes: vec![RsxAttribute {
name: name.to_string(),
value,
}],
children: vec![RsxNode::Text("ok".to_string())],
self_closing: false,
})]
}
#[test]
fn unknown_event_attribute_is_rejected() {
let nodes = button_with("on_clicky", RsxAttrValue::Literal("f".to_string()));
let errors = generate_rsx(&nodes).expect_err("unknown event must be rejected");
assert_eq!(errors.len(), 1);
assert!(errors[0].contains("unsupported event attribute `on_clicky`"));
}
#[test]
fn literal_event_handler_is_rejected() {
let nodes = button_with("on_click", RsxAttrValue::Literal("f".to_string()));
let errors = generate_rsx(&nodes).expect_err("literal handler must be rejected");
assert_eq!(errors.len(), 1);
assert!(errors[0].contains("requires a callable expression"));
}
#[test]
fn flag_event_handler_is_rejected() {
let nodes = button_with("onclick", RsxAttrValue::Flag);
let errors = generate_rsx(&nodes).expect_err("flag handler must be rejected");
assert_eq!(errors.len(), 1);
assert!(errors[0].contains("requires a callable expression"));
}
#[test]
fn path_qualified_pascal_case_component_uses_builder() {
let nodes = vec![RsxNode::Component(RsxComponent {
leading_colon: false,
path: vec!["strata_dom".to_string(), "Portal".to_string()],
generics: None,
props: vec![RsxAttribute {
name: "anchor".to_string(),
value: RsxAttrValue::Literal("trigger-btn".to_string()),
}],
children: vec![RsxNode::Text("Menu".to_string())],
self_closing: false,
})];
let code = generate_rsx(&nodes).expect("codegen must succeed");
assert!(
code.contains("strata_dom::Portal::builder().anchor(\"trigger-btn\").children(vec!["),
"unexpected codegen: {code}"
);
}
#[test]
fn path_qualified_pascal_case_component_with_only_children_uses_builder() {
let nodes = vec![RsxNode::Component(RsxComponent {
leading_colon: false,
path: vec!["strata_dom".to_string(), "Portal".to_string()],
generics: None,
props: vec![],
children: vec![RsxNode::Text("Top layer".to_string())],
self_closing: false,
})];
let code = generate_rsx(&nodes).expect("codegen must succeed");
assert!(
code.contains("strata_dom::Portal::builder().children(vec!["),
"unexpected codegen: {code}"
);
}
#[test]
fn leading_colon_and_empty_path_qualified_component_codegen() {
let empty_nodes = vec![RsxNode::Component(RsxComponent {
leading_colon: false,
path: vec!["strata_dom".to_string(), "Portal".to_string()],
generics: None,
props: vec![],
children: vec![],
self_closing: true,
})];
let empty_code = generate_rsx(&empty_nodes).expect("empty component codegen must succeed");
assert!(
empty_code.contains(
"strata_dom::IntoNodeBlueprint::into_node_blueprint(strata_dom::Portal::builder().build())"
),
"unexpected empty codegen: {empty_code}"
);
let root_nodes = vec![RsxNode::Component(RsxComponent {
leading_colon: true,
path: vec!["strata_dom".to_string(), "Portal".to_string()],
generics: None,
props: vec![],
children: vec![RsxNode::Text("Global".to_string())],
self_closing: false,
})];
let root_code = generate_rsx(&root_nodes).expect("leading-colon codegen must succeed");
assert!(
root_code.contains("::strata_dom::Portal::builder().children(vec!["),
"unexpected leading-colon codegen: {root_code}"
);
}
#[test]
fn type_associated_and_lowercase_function_paths_use_call_syntax() {
let icon_nodes = vec![RsxNode::Component(RsxComponent {
leading_colon: false,
path: vec![
"strata_icons".to_string(),
"Icons".to_string(),
"Plus".to_string(),
],
generics: None,
props: vec![],
children: vec![],
self_closing: true,
})];
let icon_code = generate_rsx(&icon_nodes).expect("icon codegen must succeed");
assert!(
icon_code.contains("strata_icons::Icons::Plus(None)"),
"unexpected icon codegen: {icon_code}"
);
let fn_nodes = vec![RsxNode::Component(RsxComponent {
leading_colon: false,
path: vec!["helpers".to_string(), "render_badge".to_string()],
generics: None,
props: vec![RsxAttribute {
name: "label".to_string(),
value: RsxAttrValue::Literal("v1".to_string()),
}],
children: vec![],
self_closing: true,
})];
let fn_code = generate_rsx(&fn_nodes).expect("fn codegen must succeed");
assert!(
fn_code.contains("helpers::render_badge(\"v1\")"),
"unexpected fn codegen: {fn_code}"
);
}
}