(data: &impl Plot)
| 61 | } |
| 62 | |
| 63 | pub(crate) fn ir_to_json(data: &impl Plot) -> Result<String, serde_json::Error> { |
| 64 | let (layout, layout_overrides) = crate::converters::layout::convert_layout_ir(data.ir_layout()); |
| 65 | let traces: Vec<Box<dyn Trace + 'static>> = data |
| 66 | .ir_traces() |
| 67 | .iter() |
| 68 | .map(crate::converters::trace::convert) |
| 69 | .collect(); |
| 70 | |
| 71 | let mut layout_json = serde_json::to_value(&layout)?; |
| 72 | |
| 73 | // Merge layout overrides (scene/polar domain entries) into the layout JSON |
| 74 | if let Some(ref overrides) = layout_overrides { |
| 75 | if let (Some(layout_map), Some(overrides_obj)) = |
| 76 | (layout_json.as_object_mut(), overrides.as_object()) |
| 77 | { |
| 78 | for (key, value) in overrides_obj { |
| 79 | layout_map.insert(key.clone(), value.clone()); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | let traces_json: Vec<Value> = traces |
| 85 | .iter() |
| 86 | .map(|t| { |
| 87 | let serialized = t.to_json(); |
| 88 | serde_json::from_str(&serialized).unwrap_or(Value::Null) |
| 89 | }) |
| 90 | .collect(); |
| 91 | |
| 92 | let output = serde_json::json!({ |
| 93 | "traces": traces_json, |
| 94 | "layout": layout_json, |
| 95 | }); |
| 96 | serde_json::to_string(&output) |
| 97 | } |
| 98 | |
| 99 | /// Helper function to open an HTML file in the default browser |
| 100 | pub(crate) fn open_html_file(path: &std::path::Path) { |
no test coverage detected