openpine_vm/visuals/
graph.rs

1use serde::{Deserialize, Serialize};
2
3use crate::visuals::{Hline, Label, Line, LineFill, Polyline, r#box::Box, table::Table};
4
5/// Stable identifier for a graph stored in a [`Chart`](crate::visuals::Chart).
6#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
7pub struct GraphId(pub(crate) i64);
8
9impl GraphId {
10    /// Returns the underlying id value.
11    #[inline]
12    pub fn as_i64(&self) -> i64 {
13        self.0
14    }
15}
16
17/// A non-series visual primitive (e.g. label/line/box).
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub enum Graph {
20    /// Horizontal line.
21    Hline(Hline),
22    /// Label.
23    Label(Label),
24    /// Line.
25    Line(Line),
26    /// Line fill.
27    LineFill(LineFill),
28    /// Box.
29    Box(Box),
30    /// Polyline.
31    Polyline(Polyline),
32    /// Table.
33    Table(Table),
34}
35
36impl Graph {
37    /// Returns the underlying [`Hline`] if this graph is a horizontal line.
38    #[inline]
39    pub fn as_hline(&self) -> Option<&Hline> {
40        match self {
41            Graph::Hline(hline) => Some(hline),
42            _ => None,
43        }
44    }
45
46    /// Returns the underlying [`Label`] if this graph is a label.
47    #[inline]
48    pub fn as_label(&self) -> Option<&Label> {
49        match self {
50            Graph::Label(label) => Some(label),
51            _ => None,
52        }
53    }
54
55    #[inline]
56    pub(crate) fn as_label_mut(&mut self) -> Option<&mut Label> {
57        match self {
58            Graph::Label(label) => Some(label),
59            _ => None,
60        }
61    }
62
63    /// Returns the underlying [`Line`] if this graph is a line.
64    #[inline]
65    pub fn as_line(&self) -> Option<&Line> {
66        match self {
67            Graph::Line(line) => Some(line),
68            _ => None,
69        }
70    }
71
72    #[inline]
73    pub(crate) fn as_line_mut(&mut self) -> Option<&mut Line> {
74        match self {
75            Graph::Line(line) => Some(line),
76            _ => None,
77        }
78    }
79
80    /// Returns the underlying [`LineFill`] if this graph is a linefill.
81    #[inline]
82    pub fn as_linefill(&self) -> Option<&LineFill> {
83        match self {
84            Graph::LineFill(linefill) => Some(linefill),
85            _ => None,
86        }
87    }
88
89    #[inline]
90    pub(crate) fn as_linefill_mut(&mut self) -> Option<&mut LineFill> {
91        match self {
92            Graph::LineFill(linefill) => Some(linefill),
93            _ => None,
94        }
95    }
96
97    /// Returns the underlying [`Box`] if this graph is a box.
98    #[inline]
99    pub fn as_box(&self) -> Option<&Box> {
100        match self {
101            Graph::Box(box_) => Some(box_),
102            _ => None,
103        }
104    }
105
106    #[inline]
107    pub(crate) fn as_box_mut(&mut self) -> Option<&mut Box> {
108        match self {
109            Graph::Box(box_) => Some(box_),
110            _ => None,
111        }
112    }
113
114    /// Returns the underlying [`Polyline`] if this graph is a polyline.
115    #[inline]
116    pub fn as_polyline(&self) -> Option<&Polyline> {
117        match self {
118            Graph::Polyline(polyline) => Some(polyline),
119            _ => None,
120        }
121    }
122
123    /// Returns the underlying [`Table`] if this graph is a table.
124    #[inline]
125    pub fn as_table(&self) -> Option<&Table> {
126        match self {
127            Graph::Table(table) => Some(table),
128            _ => None,
129        }
130    }
131
132    #[inline]
133    pub(crate) fn as_table_mut(&mut self) -> Option<&mut Table> {
134        match self {
135            Graph::Table(table) => Some(table),
136            _ => None,
137        }
138    }
139}