Skip to content

Commit

Permalink
Improve ergonomics of LegendGroupTitle method/struct
Browse files Browse the repository at this point in the history
  • Loading branch information
fsktom committed Aug 9, 2023
1 parent 3228ef1 commit 3feaa0c
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 22 deletions.
30 changes: 26 additions & 4 deletions plotly/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,36 @@ pub enum HoverInfo {
#[serde_with::skip_serializing_none]
#[derive(Serialize, Clone, Debug, Default)]
pub struct LegendGroupTitle {
text: String,
text: Option<String>,
font: Option<Font>,
}

impl From<&str> for LegendGroupTitle {
fn from(title: &str) -> Self {
LegendGroupTitle::with_text(title)
}
}

impl From<String> 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<S: Into<String>>(text: S) -> Self {
LegendGroupTitle {
text: Some(text.into()),
..Default::default()
}
}
Expand Down
2 changes: 1 addition & 1 deletion plotly/src/traces/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion plotly/src/traces/box_plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.])
Expand Down
2 changes: 1 addition & 1 deletion plotly/src/traces/candlestick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
9 changes: 6 additions & 3 deletions plotly/src/traces/contour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,11 @@ where
Box::new(self)
}

pub fn legend_group_title(mut self, legend_group_title: LegendGroupTitle) -> Box<Self> {
self.legend_group_title = Some(legend_group_title);
pub fn legend_group_title(
mut self,
legend_group_title: impl Into<LegendGroupTitle>,
) -> Box<Self> {
self.legend_group_title = Some(legend_group_title.into());
Box::new(self)
}

Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion plotly/src/traces/heat_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion plotly/src/traces/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion plotly/src/traces/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion plotly/src/traces/mesh3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
4 changes: 2 additions & 2 deletions plotly/src/traces/ohlc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion plotly/src/traces/sankey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion plotly/src/traces/scatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion plotly/src/traces/scatter3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion plotly/src/traces/scatter_mapbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
2 changes: 1 addition & 1 deletion plotly/src/traces/scatter_polar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion plotly/src/traces/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
8 changes: 8 additions & 0 deletions plotly_derive/src/field_setter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ enum FieldType {
OptionNumOrString,
OptionNumOrStringCollection,
OptionTitle,
OptionLegendGroupTitle,
OptionOther(syn::Type),
}

Expand Down Expand Up @@ -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],
}
}

Expand All @@ -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()),
}
}
Expand Down Expand Up @@ -348,6 +351,11 @@ impl FieldReceiver {
quote![],
),
FieldType::OptionTitle => (quote![impl Into<Title>], quote![value.into()], quote![]),
FieldType::OptionLegendGroupTitle => (
quote![impl Into<LegendGroupTitle>],
quote![value.into()],
quote![],
),
};

struct ModifyEnum {
Expand Down

0 comments on commit 3feaa0c

Please sign in to comment.