Skip to main content

mdbook_plotly/code_handler/plot_obj_parser/
ohlc_parser.rs

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