1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use serde::Serialize;
use std::borrow::Cow;

use crate::Visualization;

// This file should provide types to build JSON documents matching
// the schema at https://hediet.github.io/visualization/docs/visualization-data-schema.json.

pub enum True {
    True,
}

impl Default for True {
    fn default() -> Self {
        True::True
    }
}

impl Serialize for True {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_bool(true)
    }
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Text<'t> {
    kind: TextKind,
    text: Cow<'t, str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    file_name: Option<String>,
}

#[derive(Serialize, Default)]
pub struct TextKind {
    text: True,
}

impl<'t> Text<'t> {
    pub fn new(text: Cow<'t, str>) -> Self {
        Self {
            text,
            file_name: None,
            kind: TextKind::default(),
        }
    }

    pub fn with_file_name(&mut self, file_name: String) -> &mut Text<'t> {
        self.file_name = Some(file_name);
        self
    }
}

impl<'t> Visualization for Text<'t> {
    fn to_json(&self) -> String {
        serde_json::to_string(self).unwrap()
    }
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PngImage<'t> {
    kind: PngImageKind,
    base64_data: Cow<'t, str>,
}

#[derive(Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct PngImageKind {
    image_png: True,
}

impl<'t> PngImage<'t> {
    pub fn new(png_data: &[u8]) -> Self {
        Self {
            kind: PngImageKind::default(),
            base64_data: base64::encode(png_data).into(),
        }
    }
}

impl<'t> Visualization for PngImage<'t> {
    fn to_json(&self) -> String {
        serde_json::to_string(self).unwrap()
    }
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Plotly {
    kind: PlotlyKind,
    data: Vec<PlotlySeries>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PlotlySeries {
    #[serde(skip_serializing_if = "Option::is_none")]
    x: Option<Vec<f64>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    y: Option<Vec<f64>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    z: Option<Vec<f64>>,
}

impl Default for PlotlySeries {
    fn default() -> PlotlySeries {
        PlotlySeries {
            x: None,
            y: None,
            z: None,
        }
    }
}

impl PlotlySeries {
    pub fn set_y(&mut self, ys: Vec<f64>) -> &mut Self {
        self.y = Some(ys);
        self
    }

    pub fn with_y(mut self, ys: Vec<f64>) -> Self {
        self.y = Some(ys);
        self
    }
}

#[derive(Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct PlotlyKind {
    plotly: True,
}

impl Plotly {
    pub fn of_y(ys: &[f64]) -> Self {
        Plotly {
            kind: PlotlyKind::default(),
            data: vec![PlotlySeries::default().with_y(ys.into())],
        }
    }
}

impl<'t> Visualization for Plotly {
    fn to_json(&self) -> String {
        serde_json::to_string(self).unwrap()
    }
}