|
| 1 | +use plotly_derive::FieldSetter; |
| 2 | +use serde::Serialize; |
| 3 | + |
| 4 | +#[derive(Serialize, Debug, Clone)] |
| 5 | +pub enum RowOrder { |
| 6 | + #[serde(rename = "top to bottom")] |
| 7 | + TopToBottom, |
| 8 | + #[serde(rename = "bottom to top")] |
| 9 | + BottomToTop, |
| 10 | +} |
| 11 | + |
| 12 | +#[derive(Serialize, Debug, Clone)] |
| 13 | +#[serde(rename_all = "lowercase")] |
| 14 | +pub enum GridPattern { |
| 15 | + Independent, |
| 16 | + Coupled, |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Serialize, Debug, Clone)] |
| 20 | +#[serde(rename_all = "lowercase")] |
| 21 | +pub enum GridXSide { |
| 22 | + Bottom, |
| 23 | + #[serde(rename = "bottom plot")] |
| 24 | + BottomPlot, |
| 25 | + #[serde(rename = "top plot")] |
| 26 | + TopPlot, |
| 27 | + Top, |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(Serialize, Debug, Clone)] |
| 31 | +#[serde(rename_all = "lowercase")] |
| 32 | +pub enum GridYSide { |
| 33 | + Left, |
| 34 | + #[serde(rename = "left plot")] |
| 35 | + LeftPlot, |
| 36 | + #[serde(rename = "right plot")] |
| 37 | + RightPlot, |
| 38 | + Right, |
| 39 | +} |
| 40 | + |
| 41 | +#[serde_with::skip_serializing_none] |
| 42 | +#[derive(Serialize, Debug, Clone, FieldSetter)] |
| 43 | +pub struct GridDomain { |
| 44 | + x: Option<Vec<f64>>, |
| 45 | + y: Option<Vec<f64>>, |
| 46 | +} |
| 47 | + |
| 48 | +impl GridDomain { |
| 49 | + pub fn new() -> Self { |
| 50 | + Default::default() |
| 51 | + } |
| 52 | +} |
| 53 | +#[serde_with::skip_serializing_none] |
| 54 | +#[derive(Serialize, Debug, Clone, FieldSetter)] |
| 55 | +pub struct LayoutGrid { |
| 56 | + rows: Option<usize>, |
| 57 | + #[serde(rename = "roworder")] |
| 58 | + row_order: Option<RowOrder>, |
| 59 | + columns: Option<usize>, |
| 60 | + #[serde(rename = "subplots")] |
| 61 | + sub_plots: Option<Vec<String>>, |
| 62 | + #[serde(rename = "xaxes")] |
| 63 | + x_axes: Option<Vec<String>>, |
| 64 | + #[serde(rename = "yaxes")] |
| 65 | + y_axes: Option<Vec<String>>, |
| 66 | + pattern: Option<GridPattern>, |
| 67 | + #[serde(rename = "xgap")] |
| 68 | + x_gap: Option<f64>, |
| 69 | + #[serde(rename = "ygap")] |
| 70 | + y_gap: Option<f64>, |
| 71 | + domain: Option<GridDomain>, |
| 72 | + #[serde(rename = "xside")] |
| 73 | + x_side: Option<GridXSide>, |
| 74 | + #[serde(rename = "yside")] |
| 75 | + y_side: Option<GridYSide>, |
| 76 | +} |
| 77 | + |
| 78 | +impl LayoutGrid { |
| 79 | + pub fn new() -> Self { |
| 80 | + Default::default() |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +#[cfg(test)] |
| 85 | +mod tests { |
| 86 | + use serde_json::{json, to_value}; |
| 87 | + |
| 88 | + use super::*; |
| 89 | + |
| 90 | + #[test] |
| 91 | + #[rustfmt::skip] |
| 92 | + fn serialize_row_order() { |
| 93 | + assert_eq!(to_value(RowOrder::TopToBottom).unwrap(), json!("top to bottom")); |
| 94 | + assert_eq!(to_value(RowOrder::BottomToTop).unwrap(), json!("bottom to top")); |
| 95 | + } |
| 96 | + |
| 97 | + #[test] |
| 98 | + #[rustfmt::skip] |
| 99 | + fn serialize_grid_pattern() { |
| 100 | + assert_eq!(to_value(GridPattern::Independent).unwrap(), json!("independent")); |
| 101 | + assert_eq!(to_value(GridPattern::Coupled).unwrap(), json!("coupled")); |
| 102 | + } |
| 103 | + |
| 104 | + #[test] |
| 105 | + #[rustfmt::skip] |
| 106 | + fn serialize_grid_x_side() { |
| 107 | + assert_eq!(to_value(GridXSide::Bottom).unwrap(), json!("bottom")); |
| 108 | + assert_eq!(to_value(GridXSide::BottomPlot).unwrap(), json!("bottom plot")); |
| 109 | + assert_eq!(to_value(GridXSide::Top).unwrap(), json!("top")); |
| 110 | + assert_eq!(to_value(GridXSide::TopPlot).unwrap(), json!("top plot")); |
| 111 | + } |
| 112 | + |
| 113 | + #[test] |
| 114 | + #[rustfmt::skip] |
| 115 | + fn serialize_grid_y_side() { |
| 116 | + assert_eq!(to_value(GridYSide::Left).unwrap(), json!("left")); |
| 117 | + assert_eq!(to_value(GridYSide::LeftPlot).unwrap(), json!("left plot")); |
| 118 | + assert_eq!(to_value(GridYSide::Right).unwrap(), json!("right")); |
| 119 | + assert_eq!(to_value(GridYSide::RightPlot).unwrap(), json!("right plot")); |
| 120 | + } |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn serialize_grid_domain() { |
| 124 | + let grid_domain = GridDomain::new().x(vec![0.0]).y(vec![1.0]); |
| 125 | + let expected = json!({ |
| 126 | + "x": [0.0], |
| 127 | + "y": [1.0] |
| 128 | + }); |
| 129 | + |
| 130 | + assert_eq!(to_value(grid_domain).unwrap(), expected); |
| 131 | + } |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn serialize_layout_grid() { |
| 135 | + let layout_grid = LayoutGrid::new() |
| 136 | + .rows(224) |
| 137 | + .row_order(RowOrder::BottomToTop) |
| 138 | + .columns(501) |
| 139 | + .sub_plots(vec!["subplots".to_string()]) |
| 140 | + .x_axes(vec!["xaxes".to_string()]) |
| 141 | + .y_axes(vec!["yaxes".to_string()]) |
| 142 | + .pattern(GridPattern::Coupled) |
| 143 | + .x_gap(2.2) |
| 144 | + .y_gap(4.4) |
| 145 | + .domain(GridDomain::new()) |
| 146 | + .x_side(GridXSide::Top) |
| 147 | + .y_side(GridYSide::Right); |
| 148 | + |
| 149 | + let expected = json!({ |
| 150 | + "rows": 224, |
| 151 | + "roworder": "bottom to top", |
| 152 | + "columns": 501, |
| 153 | + "subplots": ["subplots"], |
| 154 | + "xaxes": ["xaxes"], |
| 155 | + "yaxes": ["yaxes"], |
| 156 | + "pattern": "coupled", |
| 157 | + "xgap": 2.2, |
| 158 | + "ygap": 4.4, |
| 159 | + "domain": {}, |
| 160 | + "xside": "top", |
| 161 | + "yside": "right", |
| 162 | + }); |
| 163 | + |
| 164 | + assert_eq!(to_value(layout_grid).unwrap(), expected); |
| 165 | + } |
| 166 | +} |
0 commit comments