Skip to main content

mdbook_plotly/
code_handler.rs

1pub mod parse_context;
2pub mod plot_obj_parser;
3pub mod until;
4
5use crate::preprocessor::config::{MapEvalConfig, PlotlyInputType};
6use anyhow::Result;
7use plotly::Plot;
8use serde_json::Value;
9
10pub fn handle(
11    raw_code: String,
12    input_type: &PlotlyInputType,
13    map_eval: &MapEvalConfig,
14) -> Result<Plot> {
15    let result = match input_type {
16        PlotlyInputType::JSONInput => handle_json_input(raw_code, map_eval)?,
17    };
18    Ok(result)
19}
20
21/// `Plot` does not implement `Deserialize`, so this routine is only an
22/// unofficial best-effort translation.
23///
24/// Do not be surprised if the output of `Plot::serialize` cannot be
25/// round-tripped through this function.
26///
27/// In addition, fields that cannot be translated are silently dropped.
28pub fn handle_json_input(raw_code: String, map_eval: &MapEvalConfig) -> Result<Plot> {
29    // Use Json5 to provide more flexible JSON.
30    let mut value: Value = json5::from_str(&raw_code)?;
31    plot_obj_parser::parse(&mut value, map_eval)
32}