Skip to main content

mdbook_plotly/code_handler/plot_obj_parser/
image_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::{Image, Trace, color::Rgba};
6
7pub fn parse_image_data(
8    image_obj: &mut serde_json::Value,
9    context: &ParseContext<'_>,
10) -> Result<Box<Image>> {
11    let z: Vec<Vec<Rgba>> = must_translate_from_context(image_obj, context, "z")?;
12    let image = Image::new(z);
13    let image = translate_with_config! {
14        image,
15        image_obj,
16        context.map(),
17        context.map_eval(),
18        (opacity, f64),
19        (name, String),
20        (legend_rank, usize),
21        (text, String),
22        (text_array, Vec<String>),
23        (hover_text, String),
24        (hover_text_array, Vec<String>),
25        (hover_template, String),
26        (hover_template_array, Vec<String>),
27        (source, String),
28        (x0, f64),
29        (dx, f64),
30        (y0, f64),
31        (dy, f64),
32        (x_axis, String),
33        (y_axis, String),
34        (ids, Vec<String>),
35        (meta, String),
36    }?;
37
38    use plotly::image::ZSmooth;
39    let image = translate_enum_with_config! {
40        image,
41        image_obj,
42        context.map(),
43        context.map_eval(),
44        (z_smooth, {
45            "fast" =>   ZSmooth::Fast,
46            "false" =>  ZSmooth::False,
47        }),
48    }?;
49
50    Ok(image)
51}
52
53pub fn parse_image_trace(
54    image_obj: &mut serde_json::Value,
55    context: &ParseContext<'_>,
56) -> Result<Box<dyn Trace>> {
57    Ok(parse_image_data(image_obj, context)?)
58}