Skip to main content

mdbook_plotly/preprocessor/handlers/
code_handler.rs

1pub mod plot_obj_parser;
2pub mod until;
3
4use crate::preprocessor::config::PlotlyInputType;
5use anyhow::Result;
6use log::{debug, warn};
7use plotly::Plot;
8use serde_json::Value;
9
10pub fn handle(raw_code: String, input_type: &PlotlyInputType) -> Result<Plot> {
11    let result = match input_type {
12        PlotlyInputType::SandBoxScript => {
13            warn!("The entry has been discarded. This config shouldn't be used.");
14            debug!("This function returns an empty string.");
15            // This treatment may not be good, but it is sufficient.
16            Plot::new()
17        }
18        PlotlyInputType::JSONInput => handle_json_input(raw_code)?,
19    };
20    Ok(result)
21}
22
23/// `Plot` does not implement `Deserialize`, so this routine is only an
24/// unofficial best-effort translation.
25///
26/// Do not be surprised if the output of `Plot::serialize` cannot be
27/// round-tripped through this function.
28///
29/// In addition, fields that cannot be translated are silently dropped.
30pub fn handle_json_input(raw_code: String) -> Result<Plot> {
31    // Use Json5 to provide more flexible JSON.
32    let mut value: Value = json5::from_str(&raw_code)?;
33    plot_obj_parser::parse(&mut value)
34}