Line data Source code
1 : //! The Vega-Lite JSON of a `@vis vegalite` tag: the spec of the tag with the query as data.
2 :
3 : use dashql_core::analyzer::{AnalyzedScript, VisualizationSpec};
4 : use serde_json::{Map, Value};
5 :
6 : /// Generate the pretty-printed Vega-Lite spec of a visualization
7 13 : pub fn generate_vegalite_spec(spec: &VisualizationSpec, script: &AnalyzedScript) -> String {
8 13 : let mut root = Map::new();
9 13 : root.insert("$schema".to_string(), Value::from("https://vega.github.io/schema/vega-lite/v5.json"));
10 13 : let mut data = Map::new();
11 13 : data.insert("$sql".to_string(), Value::from(String::from_utf8_lossy(super::node_text(script, spec.ast_node_id))));
12 13 : root.insert("data".to_string(), Value::Object(data));
13 13 : if let Some(vegalite) = &spec.vegalite {
14 20 : root.extend(vegalite.iter().map(|(k, v)| (k.clone(), v.clone())));
15 2 : }
16 13 : serde_json::to_string_pretty(&root).expect("a JSON value always serializes")
17 13 : }
|