Skip to content

Commit c05d553

Browse files
committed
refactor
Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent cd015b0 commit c05d553

File tree

6 files changed

+56
-50
lines changed

6 files changed

+56
-50
lines changed

plotly/src/layout/layout.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use plotly_derive::generate_layout_structs;
1+
use std::borrow::Cow;
2+
3+
use plotly_derive::layout_structs;
24
use plotly_derive::FieldSetter;
35
use serde::Serialize;
4-
use std::borrow::Cow;
56

67
use crate::color::Color;
78
use crate::common::{Calendar, Font, Label, Title};
@@ -37,7 +38,7 @@ impl Into<Cow<'static, Template>> for &'static Template {
3738
}
3839
}
3940

40-
#[generate_layout_structs]
41+
#[layout_structs]
4142
pub struct LayoutFields {
4243
title: Option<Title>,
4344
#[serde(rename = "showlegend")]

plotly/src/layout/mapbox.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,10 @@ impl Mapbox {
7373

7474
#[cfg(test)]
7575
mod tests {
76-
use super::*;
7776
use serde_json::{json, to_value};
7877

78+
use super::*;
79+
7980
#[test]
8081
#[rustfmt::skip]
8182
fn serialize_mapbox_style() {

plotly/src/layout/mod.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
use plotly_derive::FieldSetter;
12
use serde::Serialize;
23
use update_menu::UpdateMenu;
34

45
use crate::{
56
color::Color,
67
common::{ColorScale, Orientation},
78
};
8-
use plotly_derive::FieldSetter;
99

1010
pub mod themes;
1111
pub mod update_menu;
@@ -30,15 +30,18 @@ pub use self::axis::{
3030
pub use self::grid::{GridDomain, GridPattern, GridXSide, GridYSide, LayoutGrid, RowOrder};
3131
/// LayoutTemplate matches Layout except it lacks a field for template
3232
/// See layout/layout.rs for the full field list and doc comments.
33-
/// Layout is identical to LayoutTemplate except it has a `template` field and #[field_setter(kind = "layout")]
34-
/// See layout/layout.rs for the full field list and doc comments.
33+
/// Layout is identical to LayoutTemplate except it has a `template` field and
34+
/// #[field_setter(kind = "layout")] See layout/layout.rs for the full field
35+
/// list and doc comments.
3536
pub use self::layout::{Layout, LayoutTemplate, Template};
3637
pub use self::legend::{Legend, TraceOrder};
37-
pub use self::mapbox::{Mapbox, MapboxStyle};
38+
pub use self::mapbox::{Center, Mapbox, MapboxStyle};
3839
pub use self::modes::{
39-
BarMode, BarNorm, BoxMode, ClickMode, UniformTextMode, ViolinMode, WaterfallMode,
40+
AspectMode, BarMode, BarNorm, BoxMode, ClickMode, UniformTextMode, ViolinMode, WaterfallMode,
41+
};
42+
pub use self::scene::{
43+
Camera, CameraCenter, DragMode, DragMode3D, HoverMode, LayoutScene, Projection, ProjectionType,
4044
};
41-
pub use self::scene::{DragMode, DragMode3D, HoverMode, LayoutScene, Projection, ProjectionType};
4245
pub use self::shape::{
4346
ActiveShape, DrawDirection, FillRule, NewShape, Shape, ShapeLayer, ShapeLine, ShapeSizeMode,
4447
ShapeType,

plotly/src/layout/modes.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
use serde::{Serialize, Serializer};
22

3+
#[derive(Serialize, Debug, Clone)]
4+
/// If "cube", this scene's axes are drawn as a cube, regardless of the axes'
5+
/// ranges. If "data", this scene's axes are drawn in proportion with the axes'
6+
/// ranges. If "manual", this scene's axes are drawn in proportion with the
7+
/// input of "aspectratio" (the default behavior if "aspectratio" is provided).
8+
/// If "auto", this scene's axes are drawn using the results of "data" except
9+
/// when one axis is more than four times the size of the two others, where in
10+
/// that case the results of "cube" are used.
11+
/// Default: "auto"
12+
#[derive(Default)]
13+
pub enum AspectMode {
14+
#[serde(rename = "auto")]
15+
#[default]
16+
Auto,
17+
#[serde(rename = "cube")]
18+
Cube,
19+
#[serde(rename = "data")]
20+
Data,
21+
#[serde(rename = "manual")]
22+
Manual,
23+
}
24+
325
#[derive(Serialize, Debug, Clone)]
426
#[serde(rename_all = "lowercase")]
527
pub enum BoxMode {
@@ -123,4 +145,19 @@ mod tests {
123145
assert_eq!(to_value(WaterfallMode::Group).unwrap(), json!("group"));
124146
assert_eq!(to_value(WaterfallMode::Overlay).unwrap(), json!("overlay"));
125147
}
148+
149+
#[test]
150+
fn serialize_aspect_mode() {
151+
let aspect_mode = AspectMode::default();
152+
153+
assert_eq!(to_value(aspect_mode).unwrap(), json!("auto"));
154+
155+
let aspect_mode = AspectMode::Data;
156+
157+
assert_eq!(to_value(aspect_mode).unwrap(), json!("data"));
158+
159+
let aspect_mode = AspectMode::Cube;
160+
161+
assert_eq!(to_value(aspect_mode).unwrap(), json!("cube"));
162+
}
126163
}

plotly/src/layout/scene.rs

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,7 @@ use plotly_derive::FieldSetter;
22
use serde::Serialize;
33

44
use crate::color::Color;
5-
use crate::layout::{Annotation, Axis};
6-
7-
#[derive(Serialize, Debug, Clone)]
8-
/// If "cube", this scene's axes are drawn as a cube, regardless of the axes'
9-
/// ranges. If "data", this scene's axes are drawn in proportion with the axes'
10-
/// ranges. If "manual", this scene's axes are drawn in proportion with the
11-
/// input of "aspectratio" (the default behavior if "aspectratio" is provided).
12-
/// If "auto", this scene's axes are drawn using the results of "data" except
13-
/// when one axis is more than four times the size of the two others, where in
14-
/// that case the results of "cube" are used.
15-
/// Default: "auto"
16-
#[derive(Default)]
17-
pub enum AspectMode {
18-
#[serde(rename = "auto")]
19-
#[default]
20-
Auto,
21-
#[serde(rename = "cube")]
22-
Cube,
23-
#[serde(rename = "data")]
24-
Data,
25-
#[serde(rename = "manual")]
26-
Manual,
27-
}
5+
use crate::layout::{Annotation, AspectMode, Axis};
286

297
#[serde_with::skip_serializing_none]
308
#[derive(Serialize, Debug, Clone, FieldSetter)]
@@ -494,21 +472,6 @@ mod tests {
494472
assert_eq!(to_value(camera_center).unwrap(), expected);
495473
}
496474

497-
#[test]
498-
fn serialize_aspect_mode() {
499-
let aspect_mode = AspectMode::default();
500-
501-
assert_eq!(to_value(aspect_mode).unwrap(), json!("auto"));
502-
503-
let aspect_mode = AspectMode::Data;
504-
505-
assert_eq!(to_value(aspect_mode).unwrap(), json!("data"));
506-
507-
let aspect_mode = AspectMode::Cube;
508-
509-
assert_eq!(to_value(aspect_mode).unwrap(), json!("cube"));
510-
}
511-
512475
#[test]
513476
fn serialize_up() {
514477
let up = Up::new();

plotly_derive/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ pub fn field_setter(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
1414
}
1515

1616
#[proc_macro_attribute]
17-
pub fn generate_layout_structs(_attr: TokenStream, item: TokenStream) -> TokenStream {
17+
pub fn layout_structs(_attr: TokenStream, item: TokenStream) -> TokenStream {
1818
let input = parse_macro_input!(item as ItemStruct);
1919
let vis = &input.vis;
2020
let fields = &input.fields;
2121
let attrs = &input.attrs;
2222

23-
// Generate LayoutTemplate (no template field, no #[field_setter(kind = "layout")])
23+
// Generate LayoutTemplate (no template field, no #[field_setter(kind =
24+
// "layout")])
2425
let template_struct_name = format_ident!("LayoutTemplate");
2526
let template_struct = quote! {
2627
#(#attrs)*

0 commit comments

Comments
 (0)