MCPcopy Create free account
hub / github.com/PRQL/prql / write_json_ast_node

Function write_json_ast_node

prqlc/prqlc/src/debug/render_html.rs:362–411  ·  view source on GitHub ↗

A hacky way to reconstruct AST nodes from their JSON serialization. Finds structures that look like `{ "BinOp": { ... }, "span": ... }` and converts them to ` ... `.

(
    w: &mut W,
    node: serde_json::Value,
    is_node_contents: bool,
)

Source from the content-addressed store, hash-verified

360/// Finds structures that look like `{ "BinOp": { ... }, "span": ... }`
361/// and converts them to `<div class=ast-node>...</div>`.
362fn write_json_ast_node<W: Write>(
363 w: &mut W,
364 node: serde_json::Value,
365 is_node_contents: bool,
366) -> Result {
367 match node {
368 serde_json::Value::Null => write!(w, "None"),
369 serde_json::Value::Bool(b) => write!(w, "{b}"),
370 serde_json::Value::Number(n) => write!(w, "{n}"),
371 serde_json::Value::String(s) => write!(w, "{s}"),
372 serde_json::Value::Array(items) => {
373 writeln!(w, r#"<ul class="json-array">"#)?;
374 for item in items {
375 write!(w, "<li>")?;
376 write_json_ast_node(w, item, false)?;
377 write!(w, "</li>")?;
378 }
379 writeln!(w, "</ul>")?;
380 Ok(())
381 }
382 serde_json::Value::Object(properties) => {
383 let is_ast_node = properties.contains_key("span")
384 || properties.contains_key("id")
385 || (properties.len() == 1
386 && !is_node_contents
387 && properties.values().next().unwrap().is_object());
388 if is_ast_node {
389 return write_ast_node_from_object(w, properties);
390 }
391
392 writeln!(w, r#"<div class="json-object">"#)?;
393 for (key, value) in properties {
394 if key == "ty" || key == "return_ty" {
395 // special case for better type printing
396 let ty_json = value.to_string();
397 if let Ok(ty) = serde_json::from_str::<pr::Ty>(&ty_json) {
398 let ty_prql = escape_html(&codegen::write_ty(&ty));
399 write!(w, r#"<span>{key}: {ty_prql}</span>"#)?;
400 }
401 continue;
402 }
403
404 write!(w, r#"<span>{key}: </span><div class="json-value">"#)?;
405 write_json_ast_node(w, value, false)?;
406 writeln!(w, "</div>")?;
407 }
408 writeln!(w, "</div>")
409 }
410 }
411}
412
413fn write_ast_node_from_object<W: Write>(
414 w: &mut W,

Callers 8

write_repr_prFunction · 0.85
write_repr_plFunction · 0.85
write_repr_rqFunction · 0.85
write_repr_pq_earlyFunction · 0.85
write_repr_pqFunction · 0.85
write_repr_sql_parserFunction · 0.85
write_declFunction · 0.85

Calls 5

escape_htmlFunction · 0.85
write_tyFunction · 0.85
lenMethod · 0.80
nextMethod · 0.80

Tested by

no test coverage detected