From 3feaa0c4ea60a1351ff42ff858e91b96457d4a00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Toma=C5=9Bko?= Date: Wed, 9 Aug 2023 22:11:14 +0200 Subject: [PATCH] Improve ergonomics of LegendGroupTitle method/struct --- plotly/src/common/mod.rs | 30 +++++++++++++++++++++++++---- plotly/src/traces/bar.rs | 2 +- plotly/src/traces/box_plot.rs | 2 +- plotly/src/traces/candlestick.rs | 2 +- plotly/src/traces/contour.rs | 9 ++++++--- plotly/src/traces/heat_map.rs | 2 +- plotly/src/traces/histogram.rs | 2 +- plotly/src/traces/image.rs | 2 +- plotly/src/traces/mesh3d.rs | 2 +- plotly/src/traces/ohlc.rs | 4 ++-- plotly/src/traces/sankey.rs | 2 +- plotly/src/traces/scatter.rs | 2 +- plotly/src/traces/scatter3d.rs | 2 +- plotly/src/traces/scatter_mapbox.rs | 2 +- plotly/src/traces/scatter_polar.rs | 2 +- plotly/src/traces/surface.rs | 2 +- plotly_derive/src/field_setter.rs | 8 ++++++++ 17 files changed, 55 insertions(+), 22 deletions(-) diff --git a/plotly/src/common/mod.rs b/plotly/src/common/mod.rs index 4bd3dfd9..911ece0f 100644 --- a/plotly/src/common/mod.rs +++ b/plotly/src/common/mod.rs @@ -58,14 +58,36 @@ pub enum HoverInfo { #[serde_with::skip_serializing_none] #[derive(Serialize, Clone, Debug, Default)] pub struct LegendGroupTitle { - text: String, + text: Option, font: Option, } +impl From<&str> for LegendGroupTitle { + fn from(title: &str) -> Self { + LegendGroupTitle::with_text(title) + } +} + +impl From for LegendGroupTitle { + fn from(value: String) -> Self { + LegendGroupTitle::with_text(value) + } +} + +impl From<&String> for LegendGroupTitle { + fn from(value: &String) -> Self { + LegendGroupTitle::with_text(value) + } +} + impl LegendGroupTitle { - pub fn new(text: &str) -> Self { - Self { - text: text.to_string(), + pub fn new() -> Self { + Default::default() + } + + pub fn with_text>(text: S) -> Self { + LegendGroupTitle { + text: Some(text.into()), ..Default::default() } } diff --git a/plotly/src/traces/bar.rs b/plotly/src/traces/bar.rs index 16300ae6..0acaed0d 100644 --- a/plotly/src/traces/bar.rs +++ b/plotly/src/traces/bar.rs @@ -159,7 +159,7 @@ mod tests { .inside_text_anchor(TextAnchor::End) .inside_text_font(Font::new()) .legend_group("legend-group") - .legend_group_title(LegendGroupTitle::new("legend-group-title")) + .legend_group_title("legend-group-title") .marker(Marker::new()) .name("Bar") .offset(5) diff --git a/plotly/src/traces/box_plot.rs b/plotly/src/traces/box_plot.rs index 5b8e9c91..8903c480 100644 --- a/plotly/src/traces/box_plot.rs +++ b/plotly/src/traces/box_plot.rs @@ -283,7 +283,7 @@ mod tests { .jitter(0.5) .line(Line::new()) .legend_group("one") - .legend_group_title(LegendGroupTitle::new("Legend Group Title")) + .legend_group_title("Legend Group Title") .lower_fence(vec![0., 1.]) .marker(Marker::new()) .mean(vec![12., 13.]) diff --git a/plotly/src/traces/candlestick.rs b/plotly/src/traces/candlestick.rs index 2fe6a197..64b25a5b 100644 --- a/plotly/src/traces/candlestick.rs +++ b/plotly/src/traces/candlestick.rs @@ -144,7 +144,7 @@ mod tests { .visible(Visible::True) .show_legend(false) .legend_group("group_1") - .legend_group_title(LegendGroupTitle::new("Legend Group Title")) + .legend_group_title("Legend Group Title") .opacity(0.3) .text_array(vec!["text", "here"]) .text("text here") diff --git a/plotly/src/traces/contour.rs b/plotly/src/traces/contour.rs index 844f55c2..576e58f1 100644 --- a/plotly/src/traces/contour.rs +++ b/plotly/src/traces/contour.rs @@ -339,8 +339,11 @@ where Box::new(self) } - pub fn legend_group_title(mut self, legend_group_title: LegendGroupTitle) -> Box { - self.legend_group_title = Some(legend_group_title); + pub fn legend_group_title( + mut self, + legend_group_title: impl Into, + ) -> Box { + self.legend_group_title = Some(legend_group_title.into()); Box::new(self) } @@ -583,7 +586,7 @@ mod tests { .hover_template_array(vec!["ok {1}", "ok {2}"]) .hover_text(vec!["p3", "p4"]) .legend_group("group_1") - .legend_group_title(LegendGroupTitle::new("Legend Group Title")) + .legend_group_title("Legend Group Title") .line(Line::new()) .n_contours(5) .name("contour trace") diff --git a/plotly/src/traces/heat_map.rs b/plotly/src/traces/heat_map.rs index 151b6941..374ceec9 100644 --- a/plotly/src/traces/heat_map.rs +++ b/plotly/src/traces/heat_map.rs @@ -202,7 +202,7 @@ mod tests { .hover_template_array(vec!["tmpl1", "tmpl2"]) .hover_text(vec!["hov", "er"]) .legend_group("1") - .legend_group_title(LegendGroupTitle::new("Legend Group Title")) + .legend_group_title("Legend Group Title") .name("name") .opacity(0.99) .reverse_scale(false) diff --git a/plotly/src/traces/histogram.rs b/plotly/src/traces/histogram.rs index 7ae6dd59..b4b9b97f 100644 --- a/plotly/src/traces/histogram.rs +++ b/plotly/src/traces/histogram.rs @@ -410,7 +410,7 @@ mod tests { .hover_text("hover_text") .hover_text_array(vec!["hover_text_1", "hover_text_2"]) .legend_group("legendgroup") - .legend_group_title(LegendGroupTitle::new("Legend Group Title")) + .legend_group_title("Legend Group Title") .marker(Marker::new()) .n_bins_x(5) .n_bins_y(10) diff --git a/plotly/src/traces/image.rs b/plotly/src/traces/image.rs index 4efbf0c9..d081f9b4 100644 --- a/plotly/src/traces/image.rs +++ b/plotly/src/traces/image.rs @@ -408,7 +408,7 @@ mod tests { .name("image name") .visible(Visible::True) .legend_rank(1000) - .legend_group_title(LegendGroupTitle::new("Legend Group Title")) + .legend_group_title("Legend Group Title") .opacity(0.5) .ids(vec!["one"]) .x0(0.0) diff --git a/plotly/src/traces/mesh3d.rs b/plotly/src/traces/mesh3d.rs index fae98c9a..fc45d781 100644 --- a/plotly/src/traces/mesh3d.rs +++ b/plotly/src/traces/mesh3d.rs @@ -493,7 +493,7 @@ mod tests { .show_legend(true) .legend_rank(1000) .legend_group("legend_group") - .legend_group_title(LegendGroupTitle::new("Legend Group Title")) + .legend_group_title("Legend Group Title") .opacity(0.5) .ids(vec!["one"]) .face_color(vec!["#ff00ff"]) diff --git a/plotly/src/traces/ohlc.rs b/plotly/src/traces/ohlc.rs index cf1b4e50..7067514f 100644 --- a/plotly/src/traces/ohlc.rs +++ b/plotly/src/traces/ohlc.rs @@ -28,7 +28,7 @@ use crate::{ /// let expected = serde_json::json!({ /// "type": "ohlc", /// "x": ["2022-08-22", "2022-08-23"], -/// "open": [5, 6], +/// "open": [5, 6], /// "high": [8, 10], /// "low": [2, 4], /// "close": [6, 7] @@ -133,7 +133,7 @@ mod test { .hover_text("1") .increasing(Direction::Increasing { line: Line::new() }) .legend_group("legendgroup") - .legend_group_title(LegendGroupTitle::new("Legend Group Title")) + .legend_group_title("Legend Group Title") .line(Line::new()) .name("ohlc_trace") .opacity(0.4) diff --git a/plotly/src/traces/sankey.rs b/plotly/src/traces/sankey.rs index 1a53d2c8..be96afa2 100644 --- a/plotly/src/traces/sankey.rs +++ b/plotly/src/traces/sankey.rs @@ -436,7 +436,7 @@ mod tests { .name("sankey") .visible(true) .legend_rank(1000) - .legend_group_title(LegendGroupTitle::new("Legend Group Title")) + .legend_group_title("Legend Group Title") .ids(vec!["one"]) .hover_info(HoverInfo::All) .hover_label(Label::new()) diff --git a/plotly/src/traces/scatter.rs b/plotly/src/traces/scatter.rs index caa2a076..fe9f5b9c 100644 --- a/plotly/src/traces/scatter.rs +++ b/plotly/src/traces/scatter.rs @@ -456,7 +456,7 @@ mod tests { .hover_template_array(vec!["hover_template"]) .ids(vec!["1"]) .legend_group("legend_group") - .legend_group_title(LegendGroupTitle::new("Legend Group Title")) + .legend_group_title("Legend Group Title") .line(Line::new()) .marker(Marker::new()) .meta("meta") diff --git a/plotly/src/traces/scatter3d.rs b/plotly/src/traces/scatter3d.rs index 66c72c03..412ba1f8 100644 --- a/plotly/src/traces/scatter3d.rs +++ b/plotly/src/traces/scatter3d.rs @@ -367,7 +367,7 @@ mod tests { .ids(vec!["1"]) .legend_group("legend_group") .legend_rank(1000) - .legend_group_title(LegendGroupTitle::new("Legend Group Title")) + .legend_group_title("Legend Group Title") .line(Line::new()) .marker(Marker::new()) .meta("meta") diff --git a/plotly/src/traces/scatter_mapbox.rs b/plotly/src/traces/scatter_mapbox.rs index 1ee3c164..76348e6a 100644 --- a/plotly/src/traces/scatter_mapbox.rs +++ b/plotly/src/traces/scatter_mapbox.rs @@ -311,7 +311,7 @@ mod tests { .show_legend(true) .legend_rank(1000) .legend_group("legend group") - .legend_group_title(LegendGroupTitle::new("Legend Group Title")) + .legend_group_title("Legend Group Title") .opacity(0.5) .mode(Mode::LinesText) .ids(vec!["one"]) diff --git a/plotly/src/traces/scatter_polar.rs b/plotly/src/traces/scatter_polar.rs index ae3ed244..a9101607 100644 --- a/plotly/src/traces/scatter_polar.rs +++ b/plotly/src/traces/scatter_polar.rs @@ -368,7 +368,7 @@ mod tests { .hover_text_array(vec!["hover_text"]) .ids(vec!["1"]) .legend_group("legend_group") - .legend_group_title(LegendGroupTitle::new("Legend Group Title")) + .legend_group_title("Legend Group Title") .line(Line::new()) .marker(Marker::new()) .meta("meta") diff --git a/plotly/src/traces/surface.rs b/plotly/src/traces/surface.rs index b4110d3b..3f692543 100644 --- a/plotly/src/traces/surface.rs +++ b/plotly/src/traces/surface.rs @@ -328,7 +328,7 @@ mod tests { .hover_text("hover_text") .hover_text_array(vec!["hover_text_1"]) .legend_group("legend_group") - .legend_group_title(LegendGroupTitle::new("Legend Group Title")) + .legend_group_title("Legend Group Title") .lighting(Lighting::new()) .light_position(Position::new(0, 0, 0)) .name("surface_trace") diff --git a/plotly_derive/src/field_setter.rs b/plotly_derive/src/field_setter.rs index 0950f1af..a78fe73e 100644 --- a/plotly_derive/src/field_setter.rs +++ b/plotly_derive/src/field_setter.rs @@ -141,6 +141,7 @@ enum FieldType { OptionNumOrString, OptionNumOrStringCollection, OptionTitle, + OptionLegendGroupTitle, OptionOther(syn::Type), } @@ -204,6 +205,7 @@ impl FieldType { FieldType::OptionOther(inner) => quote![#inner], FieldType::OptionBoxOther(inner) => quote![Box<#inner>], FieldType::OptionTitle => quote![Title], + FieldType::OptionLegendGroupTitle => quote![LegendGroupTitle], } } @@ -228,6 +230,7 @@ impl FieldType { ["Box", ..] => FieldType::OptionBoxOther(types.get(2).cloned().unwrap()), ["Vec", "Box", "Color"] => FieldType::OptionVecBoxColor, ["Title"] => FieldType::OptionTitle, + ["LegendGroupTitle"] => FieldType::OptionLegendGroupTitle, _ => FieldType::OptionOther(types.get(1).cloned().unwrap()), } } @@ -348,6 +351,11 @@ impl FieldReceiver { quote![], ), FieldType::OptionTitle => (quote![impl Into], quote![value.into()], quote![]), + FieldType::OptionLegendGroupTitle => ( + quote![impl Into<LegendGroupTitle>], + quote![value.into()], + quote![], + ), }; struct ModifyEnum {