Skip to main content

mdbook_plotly/code_handler/plot_obj_parser/
ohlc_parser.rs

1use super::until::{Map, must_translate};
2use crate::{translate, translate_enum};
3use anyhow::Result;
4use plotly::Ohlc;
5
6pub fn parse_ohlc_data(
7    ohlc_obj: &mut serde_json::Value,
8    map: &Map,
9) -> Result<Box<Ohlc<String, f64>>> {
10    let x: Vec<String> = must_translate(ohlc_obj, map, "x")?;
11    let open: Vec<f64> = must_translate(ohlc_obj, map, "open")?;
12    let high: Vec<f64> = must_translate(ohlc_obj, map, "high")?;
13    let low: Vec<f64> = must_translate(ohlc_obj, map, "low")?;
14    let close: Vec<f64> = must_translate(ohlc_obj, map, "close")?;
15    let ohlc = Ohlc::new(x, open, high, low, close);
16    let ohlc = translate! {
17        *ohlc,
18        // UNEXPECTED: The other methods of the `Ohlc` return only `self`, not boxed `self`.
19        ohlc_obj,
20        map,
21        (name, String),
22        (show_legend, bool),
23        (legend_group, String),
24        (opacity, f64),
25        (hover_text, String),
26        (hover_text_array, Vec<String>),
27        (tick_width, f64),
28    }?;
29
30    use plotly::common::Visible;
31    let ohlc = translate_enum! {
32        ohlc,
33        ohlc_obj,
34        map,
35        (visible, {
36            "true" =>       Visible::True,
37            "false" =>      Visible::False,
38            "legendonly" => Visible::LegendOnly,
39        }),
40    }?;
41
42    // UNEXPECTED: The other methods of the `Ohlc` return only `self`, not boxed `self`.
43    Ok(Box::new(ohlc))
44}