Skip to content

Commit 43b3d41

Browse files
authored
Merge pull request #146 from Tcode-Motion/code-health-refactor-web-render-children-16402512525937100324
🧹 refactor: avoid cloning DslBlockValue in web module render_children
2 parents dbcb80a + d4bdc44 commit 43b3d41

1 file changed

Lines changed: 119 additions & 119 deletions

File tree

‎stdlib/src/web.rs‎

Lines changed: 119 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,7 @@ static PAGE_CONTENT: Mutex<String> = Mutex::new(String::new());
9898

9999
fn render_children(html: &mut String, dsl: &techscript_runtime::value::DslBlockValue) {
100100
for child in &dsl.children {
101-
html.push_str(&dsl_to_html(
102-
&techscript_runtime::value::RuntimeValue::DslBlock(std::rc::Rc::new(child.clone())),
103-
));
101+
html.push_str(&dsl_block_to_html(child));
104102
}
105103
}
106104

@@ -234,129 +232,131 @@ fn render_card(html: &mut String, dsl: &techscript_runtime::value::DslBlockValue
234232
/// Convert a DslBlockValue tree to HTML string.
235233
fn dsl_to_html(val: &RuntimeValue) -> String {
236234
match val {
237-
RuntimeValue::DslBlock(dsl) => {
238-
let mut html = String::new();
239-
match dsl.kind.as_str() {
240-
"website" => {
241-
render_website(&mut html, dsl);
242-
}
243-
"page" => {
244-
render_page(&mut html, dsl);
245-
}
246-
"hero" => {
247-
render_hero(&mut html, dsl);
248-
}
249-
"section" => {
250-
render_section(&mut html, dsl);
251-
}
252-
"card" => {
253-
render_card(&mut html, dsl);
254-
}
255-
"button" => {
256-
let label = dsl
257-
.properties
258-
.iter()
259-
.find(|p| p.name == "label")
260-
.and_then(|p| p.value.as_ref())
261-
.map(|v| v.to_string())
262-
.unwrap_or_else(|| "Button".to_string());
263-
let _ = write!(html, "<button>{}</button>", label);
264-
}
265-
"link" => {
266-
let label = dsl
267-
.properties
268-
.iter()
269-
.find(|p| p.name == "label")
270-
.and_then(|p| p.value.as_ref())
271-
.map(|v| v.to_string())
272-
.unwrap_or_else(|| "Link".to_string());
273-
let url = dsl
274-
.properties
275-
.iter()
276-
.find(|p| p.name == "url")
277-
.and_then(|p| p.value.as_ref())
278-
.map(|v| v.to_string());
279-
if let Some(u) = url {
280-
let _ = write!(html, "<a href=\"{}\">{}</a>", u, label);
281-
} else {
282-
let _ = write!(html, "<a href=\"#\">{}</a>", label);
283-
}
284-
}
285-
"nav" => {
286-
html.push_str("<nav>");
287-
render_children(&mut html, dsl);
288-
html.push_str("</nav>");
289-
}
290-
"header" => {
291-
html.push_str("<header>");
292-
for prop in &dsl.properties {
293-
if prop.name == "title" {
294-
if let Some(RuntimeValue::Str(t)) = &prop.value {
295-
let _ = write!(html, "<h1>{}</h1>", t);
296-
}
297-
}
298-
}
299-
render_children(&mut html, dsl);
300-
html.push_str("</header>");
301-
}
302-
"footer" => {
303-
html.push_str("<footer>");
304-
for prop in &dsl.properties {
305-
if prop.name == "text" {
306-
if let Some(RuntimeValue::Str(t)) = &prop.value {
307-
let _ = write!(html, "<p>{}</p>", t);
308-
}
309-
}
235+
RuntimeValue::DslBlock(dsl) => dsl_block_to_html(dsl),
236+
_ => String::new(),
237+
}
238+
}
239+
240+
fn dsl_block_to_html(dsl: &techscript_runtime::value::DslBlockValue) -> String {
241+
let mut html = String::new();
242+
match dsl.kind.as_str() {
243+
"website" => {
244+
render_website(&mut html, dsl);
245+
}
246+
"page" => {
247+
render_page(&mut html, dsl);
248+
}
249+
"hero" => {
250+
render_hero(&mut html, dsl);
251+
}
252+
"section" => {
253+
render_section(&mut html, dsl);
254+
}
255+
"card" => {
256+
render_card(&mut html, dsl);
257+
}
258+
"button" => {
259+
let label = dsl
260+
.properties
261+
.iter()
262+
.find(|p| p.name == "label")
263+
.and_then(|p| p.value.as_ref())
264+
.map(|v| v.to_string())
265+
.unwrap_or_else(|| "Button".to_string());
266+
let _ = write!(html, "<button>{}</button>", label);
267+
}
268+
"link" => {
269+
let label = dsl
270+
.properties
271+
.iter()
272+
.find(|p| p.name == "label")
273+
.and_then(|p| p.value.as_ref())
274+
.map(|v| v.to_string())
275+
.unwrap_or_else(|| "Link".to_string());
276+
let url = dsl
277+
.properties
278+
.iter()
279+
.find(|p| p.name == "url")
280+
.and_then(|p| p.value.as_ref())
281+
.map(|v| v.to_string());
282+
if let Some(u) = url {
283+
let _ = write!(html, "<a href=\"{}\">{}</a>", u, label);
284+
} else {
285+
let _ = write!(html, "<a href=\"#\">{}</a>", label);
286+
}
287+
}
288+
"nav" => {
289+
html.push_str("<nav>");
290+
render_children(&mut html, dsl);
291+
html.push_str("</nav>");
292+
}
293+
"header" => {
294+
html.push_str("<header>");
295+
for prop in &dsl.properties {
296+
if prop.name == "title" {
297+
if let Some(RuntimeValue::Str(t)) = &prop.value {
298+
let _ = write!(html, "<h1>{}</h1>", t);
310299
}
311-
render_children(&mut html, dsl);
312-
html.push_str("</footer>");
313300
}
314-
"input" => {
315-
let placeholder = dsl
316-
.properties
317-
.iter()
318-
.find(|p| p.name == "placeholder")
319-
.and_then(|p| p.value.as_ref())
320-
.map(|v| v.to_string());
321-
if let Some(p) = placeholder {
322-
let _ = write!(html, "<input placeholder=\"{}\">", p);
323-
} else {
324-
html.push_str("<input>");
301+
}
302+
render_children(&mut html, dsl);
303+
html.push_str("</header>");
304+
}
305+
"footer" => {
306+
html.push_str("<footer>");
307+
for prop in &dsl.properties {
308+
if prop.name == "text" {
309+
if let Some(RuntimeValue::Str(t)) = &prop.value {
310+
let _ = write!(html, "<p>{}</p>", t);
325311
}
326312
}
327-
"form" => {
328-
html.push_str("<form>");
329-
render_children(&mut html, dsl);
330-
html.push_str("</form>");
331-
}
332-
"main" => {
333-
html.push_str("<main>");
334-
render_children(&mut html, dsl);
335-
html.push_str("</main>");
336-
}
337-
"aside" => {
338-
html.push_str("<aside>");
339-
render_children(&mut html, dsl);
340-
html.push_str("</aside>");
341-
}
342-
"start" => {
343-
let label = dsl
344-
.properties
345-
.iter()
346-
.find(|p| p.name == "label")
347-
.and_then(|p| p.value.as_ref())
348-
.map(|v| v.to_string())
349-
.unwrap_or_else(|| "Get Started".to_string());
350-
let _ = write!(html, "<a class=\"start-button\" href=\"#\">{}</a>", label);
351-
}
352-
_ => {
353-
let _ = write!(html, "<!-- unknown DSL block: {} -->", dsl.kind);
354-
}
355313
}
356-
html
314+
render_children(&mut html, dsl);
315+
html.push_str("</footer>");
316+
}
317+
"input" => {
318+
let placeholder = dsl
319+
.properties
320+
.iter()
321+
.find(|p| p.name == "placeholder")
322+
.and_then(|p| p.value.as_ref())
323+
.map(|v| v.to_string());
324+
if let Some(p) = placeholder {
325+
let _ = write!(html, "<input placeholder=\"{}\">", p);
326+
} else {
327+
html.push_str("<input>");
328+
}
329+
}
330+
"form" => {
331+
html.push_str("<form>");
332+
render_children(&mut html, dsl);
333+
html.push_str("</form>");
334+
}
335+
"main" => {
336+
html.push_str("<main>");
337+
render_children(&mut html, dsl);
338+
html.push_str("</main>");
339+
}
340+
"aside" => {
341+
html.push_str("<aside>");
342+
render_children(&mut html, dsl);
343+
html.push_str("</aside>");
344+
}
345+
"start" => {
346+
let label = dsl
347+
.properties
348+
.iter()
349+
.find(|p| p.name == "label")
350+
.and_then(|p| p.value.as_ref())
351+
.map(|v| v.to_string())
352+
.unwrap_or_else(|| "Get Started".to_string());
353+
let _ = write!(html, "<a class=\"start-button\" href=\"#\">{}</a>", label);
354+
}
355+
_ => {
356+
let _ = write!(html, "<!-- unknown DSL block: {} -->", dsl.kind);
357357
}
358-
_ => String::new(),
359358
}
359+
html
360360
}
361361

362362
impl StdlibRegistry {

0 commit comments

Comments
 (0)