Skip to content

Commit fe052d7

Browse files
feat(aot): add adapters for managed head components (#97)
* Head body render * Bad path * Linting * Coverage * Linting * Review comments --------- Co-authored-by: Interchouette <484423+Interchouette@users.noreply.github.com>
1 parent 7821a80 commit fe052d7

13 files changed

Lines changed: 2932 additions & 46 deletions

File tree

‎crates/rangular-aot/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ rangular-expr = { workspace = true }
1818
rangular-host = { workspace = true }
1919
rangular-parser = { workspace = true }
2020
send_wrapper = "0.6"
21+
syn = { version = "3", features = ["parsing"] }
2122

2223
[dev-dependencies]
2324
syn = { version = "3", features = ["full"] }

‎crates/rangular-aot/src/emit.rs‎

Lines changed: 147 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,42 @@
11
use rangular_parser::{Diagnostic, builtin_tag_io, classify_bindings, parse, parse_head_template};
22

33
use crate::error::{AotIssue, EmitResult, EmitTokens};
4-
use crate::lower::{emit_rust_tokens, emit_rust_tokens_head};
4+
use crate::lower::{
5+
HeadComponentPaths, emit_rust_tokens, emit_rust_tokens_head, emit_rust_tokens_head_with_adapter,
6+
};
7+
8+
/// Leptos component paths used to opt a generated head view into a document
9+
/// integration's title, metadata, and link lifecycle.
10+
///
11+
/// The components receive `text` for titles, `head_key` for keyed entries, and
12+
/// standard head attributes as props. The compiler does not depend on any
13+
/// particular component library; callers provide paths from their integration.
14+
#[derive(Clone, Debug, Eq, PartialEq)]
15+
pub struct HeadComponentAdapter {
16+
title: String,
17+
meta: String,
18+
link: String,
19+
}
20+
21+
impl HeadComponentAdapter {
22+
/// Creates an adapter from three Rust component paths.
23+
#[must_use]
24+
pub fn new(title: impl Into<String>, meta: impl Into<String>, link: impl Into<String>) -> Self {
25+
Self {
26+
title: title.into(),
27+
meta: meta.into(),
28+
link: link.into(),
29+
}
30+
}
31+
32+
fn paths(&self) -> Result<HeadComponentPaths, AotIssue> {
33+
Ok(HeadComponentPaths {
34+
title: parse_component_path("title", &self.title)?,
35+
meta: parse_component_path("meta", &self.meta)?,
36+
link: parse_component_path("link", &self.link)?,
37+
})
38+
}
39+
}
540

641
#[must_use]
742
pub fn compile(source: &str, fn_name: &str) -> EmitResult {
@@ -25,7 +60,81 @@ pub fn compile_head_named(source: &str, file: &str, fn_name: &str) -> EmitResult
2560
compile_named_with_mode(source, file, fn_name, true)
2661
}
2762

63+
/// Compiles a named head template to caller-supplied Leptos lifecycle components.
64+
///
65+
/// In this mode, `head-key="..."` is consumed as a stable registry key on
66+
/// `<title>`, `<meta>`, and `<link>` elements. Without an adapter,
67+
/// [`compile_head_named`] continues to emit native Leptos HTML elements.
68+
#[must_use]
69+
pub fn compile_head_named_with_adapter(
70+
source: &str,
71+
file: &str,
72+
fn_name: &str,
73+
adapter: &HeadComponentAdapter,
74+
) -> EmitResult {
75+
let adapter = match adapter.paths() {
76+
Ok(adapter) => adapter,
77+
Err(issue) => {
78+
return EmitResult {
79+
code: String::new(),
80+
issues: vec![issue],
81+
};
82+
}
83+
};
84+
compile_named_with_options(source, file, fn_name, true, Some(adapter))
85+
}
86+
87+
/// Compiles a head template to caller-supplied Leptos lifecycle components.
88+
#[must_use]
89+
pub fn compile_head_with_adapter(
90+
source: &str,
91+
fn_name: &str,
92+
adapter: &HeadComponentAdapter,
93+
) -> EmitResult {
94+
compile_head_named_with_adapter(source, "<head-template>", fn_name, adapter)
95+
}
96+
97+
/// Compiles a head template to caller-supplied Leptos lifecycle components as Rust tokens.
98+
#[must_use]
99+
pub fn compile_head_tokens_with_adapter(
100+
source: &str,
101+
fn_name: &str,
102+
adapter: &HeadComponentAdapter,
103+
) -> EmitTokens {
104+
compile_head_tokens_named_with_adapter(source, "<head-template>", fn_name, adapter)
105+
}
106+
107+
/// Compiles a named head template to caller-supplied Leptos lifecycle components as Rust tokens.
108+
#[must_use]
109+
pub fn compile_head_tokens_named_with_adapter(
110+
source: &str,
111+
file: &str,
112+
fn_name: &str,
113+
adapter: &HeadComponentAdapter,
114+
) -> EmitTokens {
115+
let adapter = match adapter.paths() {
116+
Ok(adapter) => adapter,
117+
Err(issue) => {
118+
return EmitTokens {
119+
tokens: proc_macro2::TokenStream::new(),
120+
issues: vec![issue],
121+
};
122+
}
123+
};
124+
compile_tokens_named_with_options(source, file, fn_name, true, Some(adapter))
125+
}
126+
28127
fn compile_named_with_mode(source: &str, file: &str, fn_name: &str, head_mode: bool) -> EmitResult {
128+
compile_named_with_options(source, file, fn_name, head_mode, None)
129+
}
130+
131+
fn compile_named_with_options(
132+
source: &str,
133+
file: &str,
134+
fn_name: &str,
135+
head_mode: bool,
136+
head_adapter: Option<HeadComponentPaths>,
137+
) -> EmitResult {
29138
let mut parsed = if head_mode {
30139
parse_head_template(source, file)
31140
} else {
@@ -45,7 +154,11 @@ fn compile_named_with_mode(source: &str, file: &str, fn_name: &str, head_mode: b
45154
}
46155
classify_bindings(&mut parsed.template, &builtin_tag_io());
47156
let mut out = if head_mode {
48-
emit_rust_tokens_head(&parsed.template, fn_name)
157+
if let Some(adapter) = head_adapter {
158+
emit_rust_tokens_head_with_adapter(&parsed.template, fn_name, adapter)
159+
} else {
160+
emit_rust_tokens_head(&parsed.template, fn_name)
161+
}
49162
} else {
50163
emit_rust_tokens(&parsed.template, fn_name)
51164
};
@@ -80,6 +193,16 @@ fn compile_tokens_named_with_mode(
80193
file: &str,
81194
fn_name: &str,
82195
head_mode: bool,
196+
) -> EmitTokens {
197+
compile_tokens_named_with_options(source, file, fn_name, head_mode, None)
198+
}
199+
200+
fn compile_tokens_named_with_options(
201+
source: &str,
202+
file: &str,
203+
fn_name: &str,
204+
head_mode: bool,
205+
head_adapter: Option<HeadComponentPaths>,
83206
) -> EmitTokens {
84207
let mut parsed = if head_mode {
85208
parse_head_template(source, file)
@@ -100,12 +223,33 @@ fn compile_tokens_named_with_mode(
100223
}
101224
classify_bindings(&mut parsed.template, &builtin_tag_io());
102225
if head_mode {
103-
emit_rust_tokens_head(&parsed.template, fn_name)
226+
if let Some(adapter) = head_adapter {
227+
emit_rust_tokens_head_with_adapter(&parsed.template, fn_name, adapter)
228+
} else {
229+
emit_rust_tokens_head(&parsed.template, fn_name)
230+
}
104231
} else {
105232
emit_rust_tokens(&parsed.template, fn_name)
106233
}
107234
}
108235

236+
fn parse_component_path(kind: &str, path: &str) -> Result<proc_macro2::TokenStream, AotIssue> {
237+
if path.trim().is_empty() {
238+
return Err(AotIssue::error(
239+
"RANG404",
240+
format!("the {kind} head component path cannot be empty"),
241+
));
242+
}
243+
244+
let path = syn::parse_str::<syn::Path>(path).map_err(|error| {
245+
AotIssue::error(
246+
"RANG404",
247+
format!("invalid {kind} head component path `{path}`: {error}"),
248+
)
249+
})?;
250+
Ok(quote::quote! { #path })
251+
}
252+
109253
fn diag_issue(d: &Diagnostic) -> AotIssue {
110254
AotIssue::error(d.code, d.message.clone())
111255
}

‎crates/rangular-aot/src/lib.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ mod lower;
1010
mod print;
1111

1212
pub use emit::{
13-
compile, compile_head, compile_head_named, compile_head_tokens, compile_head_tokens_named,
14-
compile_named, compile_tokens, compile_tokens_named,
13+
HeadComponentAdapter, compile, compile_head, compile_head_named,
14+
compile_head_named_with_adapter, compile_head_tokens, compile_head_tokens_named,
15+
compile_head_tokens_named_with_adapter, compile_head_tokens_with_adapter,
16+
compile_head_with_adapter, compile_named, compile_tokens, compile_tokens_named,
1517
};
1618
pub use error::{AotIssue, EmitResult, EmitTokens};
1719
pub use glue::HostCell;

0 commit comments

Comments
 (0)