11
11
12
12
use directories:: ProjectDirs ;
13
13
use serde:: { Deserialize , Serialize } ;
14
+ use serde_json:: Value ;
14
15
use std:: fs:: File ;
15
16
use std:: io:: prelude:: * ;
16
17
use std:: io:: BufReader ;
17
18
use std:: path:: { Path , PathBuf } ;
18
19
use std:: process:: { Command , Stdio } ;
19
20
20
- #[ derive( Serialize ) ]
21
- struct PlotData {
22
- format : String ,
23
- width : usize ,
24
- height : usize ,
25
- scale : f64 ,
26
- data : String ,
27
- }
28
-
29
21
#[ allow( dead_code) ]
30
22
#[ derive( Deserialize , Debug ) ]
31
23
struct KaleidoResult {
@@ -47,26 +39,32 @@ impl KaleidoResult {
47
39
}
48
40
}
49
41
50
- impl PlotData {
51
- fn new ( data : & str , format : & str , width : usize , height : usize , scale : f64 ) -> PlotData {
42
+ #[ derive( Serialize ) ]
43
+ struct PlotData < ' a > {
44
+ // TODO: as with `data`, it would be much better if this were a plotly::ImageFormat, but problems
45
+ // with cyclic dependencies.
46
+ format : String ,
47
+ width : usize ,
48
+ height : usize ,
49
+ scale : f64 ,
50
+ // TODO: it would be great if this could be a plotly::Plot, but with the current workspace set up,
51
+ // that would be a cyclic dependency.
52
+ data : & ' a Value ,
53
+ }
54
+
55
+ impl < ' a > PlotData < ' a > {
56
+ fn new ( data : & ' a Value , format : & str , width : usize , height : usize , scale : f64 ) -> PlotData < ' a > {
52
57
PlotData {
53
- format : String :: from ( format) ,
58
+ format : format. to_string ( ) ,
54
59
width,
55
60
height,
56
61
scale,
57
- data : String :: from ( data ) ,
62
+ data,
58
63
}
59
64
}
60
65
61
66
fn to_json ( & self ) -> String {
62
- let data = format ! (
63
- r##"{{"format":"{}","width":{},"height":{},"scale":{},"data":{}}}"## ,
64
- self . format, self . width, self . height, self . scale, self . data
65
- ) ;
66
- data. replace ( " " , "" )
67
- . replace ( "\n " , "" )
68
- . replace ( "\t " , "" )
69
- . replace ( "\r " , "" )
67
+ serde_json:: to_string ( self ) . unwrap ( )
70
68
}
71
69
}
72
70
@@ -124,7 +122,7 @@ impl Kaleido {
124
122
pub fn save (
125
123
& self ,
126
124
dst : & Path ,
127
- plotly_data : & str ,
125
+ plotly_data : & Value ,
128
126
image_format : & str ,
129
127
width : usize ,
130
128
height : usize ,
@@ -183,15 +181,39 @@ impl Kaleido {
183
181
184
182
#[ cfg( test) ]
185
183
mod tests {
186
- use super :: * ;
184
+ use serde_json :: { json , to_value } ;
187
185
use std:: path:: PathBuf ;
188
186
189
- const TEST_PLOT : & str = r#"{
190
- "data": [{"type":"scatter","x":[1,2,3,4],"y":[10,15,13,17],"name":"trace1","mode":"markers"},
191
- {"type":"scatter","x":[2,3,4,5],"y":[16,5,11,9],"name":"trace2","mode":"lines"},
192
- {"type":"scatter","x":[1,2,3,4],"y":[12,9,15,12],"name":"trace3"}],
193
- "layout": {}
194
- }"# ;
187
+ use super :: * ;
188
+
189
+ fn create_test_plot ( ) -> Value {
190
+ to_value ( json ! ( {
191
+ "data" : [
192
+ {
193
+ "type" : "scatter" ,
194
+ "x" : [ 1 , 2 , 3 , 4 ] ,
195
+ "y" : [ 10 , 15 , 13 , 17 ] ,
196
+ "name" : "trace1" ,
197
+ "mode" : "markers"
198
+ } ,
199
+ {
200
+ "type" : "scatter" ,
201
+ "x" : [ 2 , 3 , 4 , 5 ] ,
202
+ "y" : [ 16 , 5 , 11 , 9 ] ,
203
+ "name" : "trace2" ,
204
+ "mode" : "lines"
205
+ } ,
206
+ {
207
+ "type" : "scatter" ,
208
+ "x" : [ 1 , 2 , 3 , 4 ] ,
209
+ "y" : [ 12 , 9 , 15 , 12 ] ,
210
+ "name" : "trace3" ,
211
+ }
212
+ ] ,
213
+ "layout" : { }
214
+ } ) )
215
+ . unwrap ( )
216
+ }
195
217
196
218
#[ test]
197
219
fn test_can_find_kaleido_executable ( ) {
@@ -200,61 +222,76 @@ mod tests {
200
222
201
223
#[ test]
202
224
fn test_plot_data_to_json ( ) {
203
- let d = PlotData :: new ( TEST_PLOT , "png" , 400 , 500 , 1. ) ;
204
- println ! ( "{}" , d. to_json( ) ) ;
225
+ let test_plot = create_test_plot ( ) ;
226
+ let kaleido_data = PlotData :: new ( & test_plot, "png" , 400 , 500 , 1. ) ;
227
+ let expected = json ! ( {
228
+ "data" : test_plot,
229
+ "format" : "png" ,
230
+ "width" : 400 ,
231
+ "height" : 500 ,
232
+ "scale" : 1.0
233
+ } ) ;
234
+
235
+ assert_eq ! ( to_value( kaleido_data) . unwrap( ) , expected) ;
205
236
}
206
237
207
238
#[ test]
208
239
fn test_save_png ( ) {
240
+ let test_plot = create_test_plot ( ) ;
209
241
let k = Kaleido :: new ( ) ;
210
242
let dst = PathBuf :: from ( "example.png" ) ;
211
- let r = k. save ( dst. as_path ( ) , TEST_PLOT , "png" , 1200 , 900 , 4.5 ) ;
243
+ let r = k. save ( dst. as_path ( ) , & test_plot , "png" , 1200 , 900 , 4.5 ) ;
212
244
assert ! ( r. is_ok( ) ) ;
213
245
assert ! ( std:: fs:: remove_file( dst. as_path( ) ) . is_ok( ) ) ;
214
246
}
215
247
216
248
#[ test]
217
249
fn test_save_jpeg ( ) {
250
+ let test_plot = create_test_plot ( ) ;
218
251
let k = Kaleido :: new ( ) ;
219
252
let dst = PathBuf :: from ( "example.jpeg" ) ;
220
- let r = k. save ( dst. as_path ( ) , TEST_PLOT , "jpeg" , 1200 , 900 , 4.5 ) ;
253
+ let r = k. save ( dst. as_path ( ) , & test_plot , "jpeg" , 1200 , 900 , 4.5 ) ;
221
254
assert ! ( r. is_ok( ) ) ;
222
255
assert ! ( std:: fs:: remove_file( dst. as_path( ) ) . is_ok( ) ) ;
223
256
}
224
257
225
258
#[ test]
226
259
fn test_save_webp ( ) {
260
+ let test_plot = create_test_plot ( ) ;
227
261
let k = Kaleido :: new ( ) ;
228
262
let dst = PathBuf :: from ( "example.webp" ) ;
229
- let r = k. save ( dst. as_path ( ) , TEST_PLOT , "webp" , 1200 , 900 , 4.5 ) ;
263
+ let r = k. save ( dst. as_path ( ) , & test_plot , "webp" , 1200 , 900 , 4.5 ) ;
230
264
assert ! ( r. is_ok( ) ) ;
231
265
assert ! ( std:: fs:: remove_file( dst. as_path( ) ) . is_ok( ) ) ;
232
266
}
233
267
234
268
#[ test]
235
269
fn test_save_svg ( ) {
270
+ let test_plot = create_test_plot ( ) ;
236
271
let k = Kaleido :: new ( ) ;
237
272
let dst = PathBuf :: from ( "example.svg" ) ;
238
- let r = k. save ( dst. as_path ( ) , TEST_PLOT , "svg" , 1200 , 900 , 4.5 ) ;
273
+ let r = k. save ( dst. as_path ( ) , & test_plot , "svg" , 1200 , 900 , 4.5 ) ;
239
274
assert ! ( r. is_ok( ) ) ;
240
275
assert ! ( std:: fs:: remove_file( dst. as_path( ) ) . is_ok( ) ) ;
241
276
}
242
277
243
278
#[ test]
244
279
fn test_save_pdf ( ) {
280
+ let test_plot = create_test_plot ( ) ;
245
281
let k = Kaleido :: new ( ) ;
246
282
let dst = PathBuf :: from ( "example.pdf" ) ;
247
- let r = k. save ( dst. as_path ( ) , TEST_PLOT , "pdf" , 1200 , 900 , 4.5 ) ;
283
+ let r = k. save ( dst. as_path ( ) , & test_plot , "pdf" , 1200 , 900 , 4.5 ) ;
248
284
assert ! ( r. is_ok( ) ) ;
249
285
assert ! ( std:: fs:: remove_file( dst. as_path( ) ) . is_ok( ) ) ;
250
286
}
251
287
252
288
#[ test]
253
289
#[ ignore]
254
290
fn test_save_eps ( ) {
291
+ let test_plot = create_test_plot ( ) ;
255
292
let k = Kaleido :: new ( ) ;
256
293
let dst = PathBuf :: from ( "example.eps" ) ;
257
- let r = k. save ( dst. as_path ( ) , TEST_PLOT , "eps" , 1200 , 900 , 4.5 ) ;
294
+ let r = k. save ( dst. as_path ( ) , & test_plot , "eps" , 1200 , 900 , 4.5 ) ;
258
295
assert ! ( r. is_ok( ) ) ;
259
296
assert ! ( std:: fs:: remove_file( dst. as_path( ) ) . is_ok( ) ) ;
260
297
}
0 commit comments