Skip to main content

entrenar/monitor/wasm/
options.rs

1//! Dashboard rendering options.
2
3#[cfg(target_arch = "wasm32")]
4use wasm_bindgen::prelude::*;
5
6/// Default canvas width in pixels for the WASM dashboard
7const DEFAULT_DASHBOARD_WIDTH: u32 = 800;
8/// Default canvas height in pixels for the WASM dashboard
9const DEFAULT_DASHBOARD_HEIGHT: u32 = 400;
10
11/// Dashboard rendering options.
12#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
13#[derive(Debug, Clone)]
14pub struct WasmDashboardOptions {
15    pub(crate) width: u32,
16    pub(crate) height: u32,
17    pub(crate) background_color: String,
18    pub(crate) loss_color: String,
19    pub(crate) accuracy_color: String,
20    pub(crate) show_sparklines: bool,
21}
22
23#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
24impl WasmDashboardOptions {
25    /// Create default dashboard options.
26    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(constructor))]
27    pub fn new() -> Self {
28        Self {
29            width: DEFAULT_DASHBOARD_WIDTH,
30            height: DEFAULT_DASHBOARD_HEIGHT,
31            background_color: "#1a1a2e".to_string(),
32            loss_color: "#ff6b6b".to_string(),
33            accuracy_color: "#4ecdc4".to_string(),
34            show_sparklines: true,
35        }
36    }
37
38    /// Set width in pixels.
39    #[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
40    pub fn width(mut self, width: u32) -> Self {
41        self.width = width;
42        self
43    }
44
45    /// Set height in pixels.
46    #[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
47    pub fn height(mut self, height: u32) -> Self {
48        self.height = height;
49        self
50    }
51
52    /// Set background color (hex format).
53    #[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
54    pub fn background_color(mut self, color: &str) -> Self {
55        self.background_color = color.to_string();
56        self
57    }
58
59    /// Set loss color (hex format).
60    #[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
61    pub fn loss_color(mut self, color: &str) -> Self {
62        self.loss_color = color.to_string();
63        self
64    }
65
66    /// Set accuracy color (hex format).
67    #[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
68    pub fn accuracy_color(mut self, color: &str) -> Self {
69        self.accuracy_color = color.to_string();
70        self
71    }
72
73    /// Enable/disable sparklines.
74    #[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
75    pub fn show_sparklines(mut self, show: bool) -> Self {
76        self.show_sparklines = show;
77        self
78    }
79
80    /// Get width.
81    pub fn get_width(&self) -> u32 {
82        self.width
83    }
84
85    /// Get height.
86    pub fn get_height(&self) -> u32 {
87        self.height
88    }
89}
90
91impl Default for WasmDashboardOptions {
92    fn default() -> Self {
93        Self::new()
94    }
95}
96
97#[cfg(test)]
98mod tests {
99    use super::*;
100
101    #[test]
102    fn test_wasm_dashboard_options_default() {
103        let opts = WasmDashboardOptions::new();
104        assert_eq!(opts.width, 800);
105        assert_eq!(opts.height, 400);
106        assert_eq!(opts.background_color, "#1a1a2e");
107    }
108
109    #[test]
110    fn test_wasm_dashboard_options_builder() {
111        let opts = WasmDashboardOptions::new()
112            .width(1024)
113            .height(768)
114            .background_color("#ffffff")
115            .loss_color("#ff0000")
116            .accuracy_color("#00ff00")
117            .show_sparklines(false);
118
119        assert_eq!(opts.width, 1024);
120        assert_eq!(opts.height, 768);
121        assert_eq!(opts.background_color, "#ffffff");
122        assert_eq!(opts.loss_color, "#ff0000");
123        assert_eq!(opts.accuracy_color, "#00ff00");
124        assert!(!opts.show_sparklines);
125    }
126
127    #[test]
128    fn test_wasm_dashboard_options_default_trait() {
129        let opts = WasmDashboardOptions::default();
130        assert_eq!(opts.get_width(), 800);
131        assert_eq!(opts.get_height(), 400);
132    }
133}