Skip to main content

mdbook_plotly/code_handler/plot_obj_parser/
pie_parser.rs

1use super::common::parse_marker;
2use super::until::must_translate_from_context;
3use crate::code_handler::parse_context::ParseContext;
4use crate::{translate_enum_with_config, translate_with_config};
5use anyhow::Result;
6use plotly::{Pie, Trace};
7
8pub fn parse_pie_data(
9    pie_obj: &mut serde_json::Value,
10    context: &ParseContext<'_>,
11) -> Result<Box<Pie<f64>>> {
12    let pie: Vec<f64> = must_translate_from_context(pie_obj, context, "values")?;
13    let pie: Box<Pie<f64>> = Pie::new(pie);
14    let pie = translate_with_config! {
15        pie,
16        pie_obj,
17        context.map(),
18        context.map_eval(),
19        (automargin, bool),
20        (dlabel, f64),
21        (hole, f64),
22        (hover_template, String),
23        (hover_template_array, Vec<String>),
24        (hover_text, String),
25        (hover_text_array, Vec<String>),
26        (ids, Vec<String>),
27        (label0, f64),
28        (labels, Vec<String>),
29        (legend_group, String),
30        (legend_rank, usize),
31        (name, String),
32        (opacity, f64),
33        (meta, String),
34        (sort, bool),
35        (text_position_src, String),
36        (text_position_src_array, Vec<String>),
37        (text, String),
38        (text_array, Vec<String>),
39        (text_info, String),
40        (show_legend, bool),
41        (rotation, f64),
42        (pull, f64),
43    }?;
44
45    use plotly::traces::pie::PieDirection;
46    let pie = translate_enum_with_config! {
47        pie,
48        pie_obj,
49        context.map(),
50        context.map_eval(),
51        (direction, {
52            "clockwise" =>          PieDirection::Clockwise,
53            "counterclockwise" =>   PieDirection::CounterClockwise,
54        }),
55    }?;
56
57    let pie = if let Some(marker_obj) = pie_obj.get_mut("marker")
58        && marker_obj.is_object()
59    {
60        let marker = parse_marker(marker_obj, context)?;
61        pie.marker(marker)
62    } else {
63        pie
64    };
65
66    Ok(pie)
67}
68
69pub fn parse_pie_trace(
70    pie_obj: &mut serde_json::Value,
71    context: &ParseContext<'_>,
72) -> Result<Box<dyn Trace>> {
73    Ok(parse_pie_data(pie_obj, context)?)
74}