Skip to content

Commit b55efe6

Browse files
authored
Tidy up folder structure (#85)
* reconfigure folder structure * disambiguate import and fix feature gating
1 parent 8813e5a commit b55efe6

20 files changed

+58
-62
lines changed

plotly/src/common/color.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ mod tests {
479479
);
480480
}
481481

482+
#[test]
482483
fn color_from_f64() {
483484
assert_eq!(1.54.to_color(), ColorWrapper::F(1.54));
484485
assert_eq!(0.1.to_color(), ColorWrapper::F(0.1));

plotly/src/common/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use serde::{Serialize, Serializer};
2-
31
pub mod color;
42

3+
use serde::{Serialize, Serializer};
4+
55
use crate::common::color::ColorWrapper;
6-
use crate::private::NumOrString;
7-
use crate::private::{self, NumOrStringCollection};
6+
use crate::private;
87
use color::Color;
98

109
#[derive(Serialize, Clone, Debug)]
@@ -688,7 +687,7 @@ impl Gradient {
688687
pub struct TickFormatStop {
689688
enabled: bool,
690689
#[serde(skip_serializing_if = "Option::is_none", rename = "dtickrange")]
691-
dtick_range: Option<NumOrStringCollection>,
690+
dtick_range: Option<private::NumOrStringCollection>,
692691
#[serde(skip_serializing_if = "Option::is_none")]
693692
value: Option<String>,
694693
#[serde(skip_serializing_if = "Option::is_none")]
@@ -710,7 +709,7 @@ impl TickFormatStop {
710709
self
711710
}
712711

713-
pub fn dtick_range<V: Into<NumOrString> + Clone>(mut self, range: Vec<V>) -> Self {
712+
pub fn dtick_range<V: Into<private::NumOrString> + Clone>(mut self, range: Vec<V>) -> Self {
714713
self.dtick_range = Some(range.into());
715714
self
716715
}

plotly/src/layout.rs renamed to plotly/src/layout/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
pub mod themes;
2+
13
use serde::{Serialize, Serializer};
24
use std::borrow::Cow;
35

File renamed without changes.

plotly/src/lib.rs

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,38 @@
22
//!
33
//! A plotting library for Rust powered by [Plotly.js](https://plot.ly/javascript/).
44
#![recursion_limit = "256"] // lets us use a large serde_json::json! macro for testing crate::layout::Axis
5-
#![allow(dead_code)]
65
extern crate askama;
76
extern crate rand;
87
extern crate serde;
98

9+
#[cfg(feature = "plotly_ndarray")]
1010
pub mod ndarray;
1111

1212
#[cfg(feature = "wasm")]
1313
pub mod bindings;
1414

15+
pub mod common;
1516
pub mod configuration;
1617
pub mod layout;
1718
pub mod plot;
19+
mod traces;
1820

19-
pub mod themes;
20-
21-
pub mod bar;
22-
pub mod box_plot;
23-
pub mod candlestick;
24-
pub mod common;
25-
pub mod contour;
26-
pub mod heat_map;
27-
pub mod histogram;
28-
pub mod ohlc;
29-
pub mod sankey;
30-
pub mod scatter;
31-
pub mod scatter_polar;
32-
pub mod surface;
33-
34-
pub use crate::configuration::Configuration;
35-
pub use crate::layout::Layout;
36-
pub use crate::plot::ImageFormat;
37-
pub use crate::plot::Plot;
38-
39-
pub use crate::bar::Bar;
40-
pub use crate::box_plot::BoxPlot;
41-
pub use crate::candlestick::Candlestick;
42-
pub use crate::contour::Contour;
43-
pub use crate::heat_map::HeatMap;
44-
pub use crate::histogram::Histogram;
45-
pub use crate::ohlc::Ohlc;
46-
pub use crate::sankey::Sankey;
47-
pub use crate::scatter::Scatter;
48-
pub use crate::scatter_polar::ScatterPolar;
49-
pub use crate::surface::Surface;
50-
51-
pub use crate::common::color::NamedColor;
52-
pub use crate::common::color::Rgb;
53-
pub use crate::common::color::Rgba;
21+
pub use common::color::{NamedColor, Rgb, Rgba};
22+
pub use configuration::Configuration;
23+
pub use layout::Layout;
24+
pub use plot::{ImageFormat, Plot, Trace};
5425

55-
pub use crate::plot::Trace;
26+
// Bring the different trace types into the top-level scope
27+
pub use traces::{
28+
Bar, BoxPlot, Candlestick, Contour, HeatMap, Histogram, Ohlc, Sankey, Scatter, ScatterPolar,
29+
Surface,
30+
};
31+
// Also provide easy access to modules which contain additional trace-specific types
32+
pub use traces::{box_plot, contour, histogram, sankey, surface};
5633

5734
#[cfg(feature = "plotly_ndarray")]
5835
pub use crate::ndarray::ArrayTraces;
5936

6037
// Not public API.
6138
#[doc(hidden)]
62-
pub mod private;
39+
mod private;
File renamed without changes.

plotly/src/plot.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use dyn_clone::DynClone;
66
use erased_serde::Serialize as ErasedSerialize;
77
use rand::{thread_rng, Rng};
88
use serde::Serialize;
9-
use std::env;
109
use std::fs::File;
1110
use std::io::Write;
1211
use std::path::Path;
@@ -139,6 +138,7 @@ pub struct Plot {
139138
remote_plotly_js: bool,
140139
}
141140

141+
#[cfg(not(feature = "wasm"))]
142142
const DEFAULT_HTML_APP_NOT_FOUND: &str = r#"Could not find default application for HTML files.
143143
Consider using the `to_html` method to save the plot instead. If using the `kaleido` feature the
144144
`save` method can be used to produce a static image in one of the following formats:
@@ -220,6 +220,8 @@ impl Plot {
220220
/// in the /tmp directory.
221221
#[cfg(not(feature = "wasm"))]
222222
pub fn show(&self) {
223+
use std::env;
224+
223225
let rendered = self.render(false, "", 0, 0);
224226
let rendered = rendered.as_bytes();
225227
let mut temp = env::temp_dir();
@@ -251,6 +253,8 @@ impl Plot {
251253
/// To save the resulting png right-click on the resulting image and select `Save As...`.
252254
#[cfg(not(feature = "wasm"))]
253255
pub fn show_png(&self, width: usize, height: usize) {
256+
use std::env;
257+
254258
let rendered = self.render(true, "png", width, height);
255259
let rendered = rendered.as_bytes();
256260
let mut temp = env::temp_dir();
@@ -281,6 +285,8 @@ impl Plot {
281285
/// To save the resulting png right-click on the resulting image and select `Save As...`.
282286
#[cfg(not(feature = "wasm"))]
283287
pub fn show_jpeg(&self, width: usize, height: usize) {
288+
use std::env;
289+
284290
let rendered = self.render(true, "jpg", width, height);
285291
let rendered = rendered.as_bytes();
286292
let mut temp = env::temp_dir();
@@ -472,7 +478,7 @@ impl Plot {
472478
.expect("Invalid JSON structure - expected a top-level Object")
473479
}
474480

475-
#[cfg(target_os = "linux")]
481+
#[cfg(all(target_os = "linux", not(feature = "wasm")))]
476482
fn show_with_default_app(temp_path: &str) {
477483
use std::process::Command;
478484
Command::new("xdg-open")
@@ -481,7 +487,7 @@ impl Plot {
481487
.expect(DEFAULT_HTML_APP_NOT_FOUND);
482488
}
483489

484-
#[cfg(target_os = "macos")]
490+
#[cfg(all(target_os = "macos", not(feature = "wasm")))]
485491
fn show_with_default_app(temp_path: &str) {
486492
use std::process::Command;
487493
Command::new("open")
@@ -490,7 +496,7 @@ impl Plot {
490496
.expect(DEFAULT_HTML_APP_NOT_FOUND);
491497
}
492498

493-
#[cfg(target_os = "windows")]
499+
#[cfg(all(target_os = "windows", not(feature = "wasm")))]
494500
fn show_with_default_app(temp_path: &str) {
495501
use std::process::Command;
496502
Command::new("cmd")

plotly/src/private/mod.rs renamed to plotly/src/private.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use crate::common::color::{Color, ColorWrapper};
21
use serde::Serialize;
32

3+
use crate::common::color::{Color, ColorWrapper};
4+
45
#[cfg(feature = "plotly_ndarray")]
56
use crate::ndarray::ArrayTraces;
67
#[cfg(feature = "plotly_ndarray")]
@@ -20,19 +21,6 @@ pub fn to_color_array<C: Color>(v: Vec<C>) -> Vec<ColorWrapper> {
2021
sv
2122
}
2223

23-
pub fn is_valid_color_array(a: &[ColorWrapper]) -> bool {
24-
let mut sv: Vec<String> = Vec::new();
25-
let mut fv: Vec<f64> = Vec::new();
26-
for e in a.iter() {
27-
match e {
28-
ColorWrapper::F(n) => fv.push(*n),
29-
ColorWrapper::S(s) => sv.push(s.clone()),
30-
}
31-
}
32-
33-
!sv.is_empty() && !fv.is_empty()
34-
}
35-
3624
#[derive(Serialize, Clone, Debug, PartialEq)]
3725
#[serde(untagged)]
3826
pub enum NumOrString {
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)