plt 0.4.1

Plotting library with a focus on publication level aesthetics and ergonomic control.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
use crate::backend;
use crate::layout::{FractionalArea, Layout};
use crate::subplot::{
    AxisType, Grid, Line, LineStyle, MarkerStyle, Subplot, TickDirection, TickLabels, TickSpacing,
};
use crate::{Color, FileFormat, PltError};

use std::collections::HashMap;
use std::{f64, iter, marker, ops, path};

/// Represents a whole figure, containing subplots, which can be drawn as an image.
///
/// Backend defaults to Cairo if cairo feature is enabled.
#[derive(Debug)]
#[cfg(feature = "cairo")]
pub struct Figure<'a, B: backend::Canvas = backend::CairoCanvas> {
    subplots: Vec<Subplot<'a>>,
    subplot_areas: Vec<FractionalArea>,
    size: draw::Size,
    scaling: f32,
    dpi: u16,
    face_color: Color,
    phantom: marker::PhantomData<B>,
}
#[cfg(not(feature = "cairo"))]
pub struct Figure<'a, B: backend::Canvas> {
    subplots: Vec<Subplot<'a>>,
    subplot_areas: Vec<draw::Area>,
    size: draw::Size,
    scaling: f32,
    dpi: u16,
    face_color: Color,
    phantom: marker::PhantomData<B>,
}
impl<'a, B: backend::Canvas> Figure<'a, B> {
    /// The main constructor.
    pub fn new(format: &FigureFormat) -> Self {
        // scaling factor for different DPIs
        let scaling = format.dpi as f32 / FigureFormat::default().dpi as f32;

        // size of figure in pixels
        let width = (format.size.width * format.dpi as f32).floor() as u32;
        let height = (format.size.height * format.dpi as f32).floor() as u32;

        Self {
            subplots: Vec::new(),
            subplot_areas: Vec::new(),
            size: draw::Size { width, height },
            scaling,
            dpi: format.dpi,
            face_color: format.face_color,
            phantom: marker::PhantomData,
        }
    }

    /// Adds subplots to the figure through a [`Layout`].
    pub fn set_layout<'b, L: Layout<'a>>(&'b mut self, layout: L) -> Result<(), PltError> {
        let (mut subplots, mut frac_areas): (Vec<Subplot>, Vec<FractionalArea>) = layout.subplots()
            .into_iter()
            .unzip();

        if let Some(area) = frac_areas.iter().find(|area| !area.valid()) {
            return Err(PltError::InvalidSubplotArea(*area));
        }

        self.subplots.append(&mut subplots);
        self.subplot_areas.append(&mut frac_areas);

        Ok(())
    }

    /// Draw figure to provided backend.
    pub fn draw_to_backend(&mut self, backend: &mut B) -> Result<(), PltError> {
        let old_size = self.size;
        self.size = backend.size()?;

        for (subplot, subplot_area) in iter::zip(&self.subplots, &self.subplot_areas) {
            let subplot_area = subplot_area.to_area(self.size);
            draw_subplot(backend, subplot, &subplot_area, self.scaling)?;
        }

        self.size = old_size;

        Ok(())
    }

    /// Draw figure to a file.
    pub fn draw_file<P: AsRef<path::Path>>(
        &self,
        format: FileFormat,
        filename: P,
    ) -> Result<(), PltError> {
        // create canvas to draw to
        let image_format = match format {
            FileFormat::Png => draw::ImageFormat::Bitmap,
            FileFormat::Svg => draw::ImageFormat::Svg,
            _ => draw::ImageFormat::Bitmap,
        };
        let mut canvas = B::new(draw::CanvasDescriptor {
            size: self.size,
            face_color: self.face_color,
            image_format,
        })?;

        for (subplot, subplot_area) in iter::zip(&self.subplots, &self.subplot_areas) {
            let subplot_area = subplot_area.to_area(self.size);
            draw_subplot(&mut canvas, subplot, &subplot_area, self.scaling)?;
        }

        // save to file
        canvas.save_file(draw::SaveFileDescriptor {
            filename: filename.as_ref(),
            format,
            dpi: self.dpi,
        })?;

        Ok(())
    }

    /// Get reference to held subplots.
    #[deprecated]
    pub fn subplots<'b>(&'b mut self) -> &mut Vec<Subplot<'a>>
    where
        'a: 'b,
    {
        &mut self.subplots
    }

    /// Change size of figure.
    pub fn set_size(&mut self, size: FigSize) {
        let width = (size.width * self.dpi as f32).floor() as u32;
        let height = (size.height * self.dpi as f32).floor() as u32;

        self.size = draw::Size { width, height };
    }

    /// Removes all subplots from figure.
    pub fn clear(&mut self) {
        self.subplots.clear();
        self.subplot_areas.clear();
    }
}
impl<'a, B: backend::Canvas> Default for Figure<'a, B> {
    fn default() -> Self {
        Self::new(&FigureFormat::default())
    }
}

/// Describes the configuration of a [`Figure`].
#[derive(Clone, Debug)]
pub struct FigureFormat {
    /// The size of the figure, in inches.
    pub size: FigSize,
    /// The dots (pixels) per inch of the figure.
    pub dpi: u16,
    /// The background color of the figure.
    pub face_color: Color,
}
impl Default for FigureFormat {
    fn default() -> Self {
        Self {
            size: FigSize { width: 6.75, height: 5.00 },
            dpi: 100,
            face_color: Color::WHITE,
        }
    }
}

/// The size of a figure, in inches.
#[derive(Copy, Clone, Debug)]
pub struct FigSize {
    pub width: f32,
    pub height: f32,
}

// private

struct SubplotList<'a> {
    subplots: &'a mut Vec<Subplot<'a>>,
    rows: usize,
}
impl<'a> ops::Index<(usize, usize)> for SubplotList<'a> {
    type Output = Subplot<'a>;

    fn index(&self, indicies: (usize, usize)) -> &Self::Output {
        &self.subplots[indicies.0 + self.rows * indicies.1]
    }
}
impl ops::IndexMut<(usize, usize)> for SubplotList<'_> {
    fn index_mut(&mut self, indicies: (usize, usize)) -> &mut Self::Output {
        &mut self.subplots[indicies.0 + self.rows * indicies.1]
    }
}

struct AxisFinalized {
    pub label: String,
    pub major_tick_locs: Vec<f64>,
    pub major_tick_labels: Vec<String>,
    pub minor_tick_locs: Vec<f64>,
    pub minor_tick_labels: Vec<String>,
    pub label_multiplier: i32,
    pub label_offset: f64,
    pub major_grid: bool,
    pub minor_grid: bool,
    pub limits: (f64, f64),
    pub visible: bool,
}

fn sigdigit(mut num: f64) -> i32 {
    if num == 0.0 {
        return i32::MIN;
    }

    if num > 1.0 {
        let mut ret = 0;
        while num >= 10.0 {
            num /= 10.0;
            ret += 1;
        }
        ret
    } else {
        let mut ret = 0;
        while num < 1.0 {
            num *= 10.0;
            ret -= 1;
        }
        ret
    }
}

fn decimals(mut num: f64, ndigits: u8) -> Vec<u8> {
    let mut decimals = Vec::with_capacity(ndigits as usize);
    for _ in 0..ndigits {
        num -= num.floor();
        num *= 10.0;
        decimals.push(num.floor() as u8);
    }

    decimals
}

fn round_to(num: f64, place: i32) -> f64 {
    (num * f64::powi(10.0, place)).round() / f64::powi(10.0, place)
}

fn superscript(n: u16) -> String {
    if n == 0 {
        "".to_owned()
    } else if n == 1 {
        "¹".to_owned()
    } else if n == 2 {
        "²".to_owned()
    } else if n == 3 {
        "³".to_owned()
    } else if n == 4 {
        "".to_owned()
    } else if n == 5 {
        "".to_owned()
    } else if n == 6 {
        "".to_owned()
    } else if n == 7 {
        "".to_owned()
    } else if n == 8 {
        "".to_owned()
    } else if n == 9 {
        "".to_owned()
    } else if n >= 10 {
        superscript(n / 10) + &superscript(n % 10)
    } else {
        "".to_owned()
    }
}

fn tick_modifiers(ticks: &[f64]) -> Result<(f64, i32, usize), PltError> {
    // make sure there are no NaNs
    if ticks.iter().any(|&tick| tick.is_nan()) {
        return Err(PltError::BadTickPlacement("tick is NaN".to_owned()));
    }

    // return empty labels for empty ticks
    if ticks.is_empty() {
        return Ok((0.0, 0, 0));
    }

    // find the highest most significant digit location
    let mut max_multiplier = sigdigit(*ticks.last().unwrap());

    // get differences between ticks
    let difs = ticks
        .windows(2)
        .map(|window| window[1] - window[0])
        .collect::<Vec<_>>();
    // find the largest difference between any two consecutive ticks
    let max_dif = *difs.iter()
        .reduce(|max, dif| if dif > max { dif } else { max })
        .unwrap();
    // find the highest most significant digit of the max tick difference
    let dif_multiplier = if max_dif != 0.0 {
        sigdigit(max_dif)
    } else {
        max_multiplier
    };

    // if multiplier of max dif is less than max_multiplier - 3, use offset
    let offset = if dif_multiplier < max_multiplier - 3 {
        ticks[0]
    } else {
        0.0
    };

    // get true multiplier
    max_multiplier = sigdigit(round_to(
        *ticks.last().unwrap() - offset,
        3 - dif_multiplier,
    ));
    let multiplier = if !(-2..=3).contains(&max_multiplier) {
        max_multiplier
    } else {
        0
    };

    // get precision
    let max_precision = if multiplier != 0 || max_multiplier < 0 {
        3
    } else {
        3 - max_multiplier
    };
    let shifted_ticks = if multiplier != 0 {
        ticks.iter()
            .map(|&tick| {
                let rounded = (tick * f64::powi(10.0, 3 - multiplier)).round();
                rounded * f64::powi(10.0, -3)
            })
            .collect::<Vec<_>>()
    } else {
        ticks.to_vec()
    };
    let precision = shifted_ticks.iter()
        .map(|&tick| {
            decimals(tick, max_precision as u8)
                .iter()
                .rposition(|&digit| digit != 0)
                .map(|prec| prec + 1)
                .unwrap_or(0)
        })
        .max()
        .unwrap();

    Ok((offset, multiplier, precision))
}

fn ticks_to_labels(ticks: &[f64], modifiers: (f64, i32, usize)) -> Result<Vec<String>, PltError> {
    // make sure there are no NaNs
    if ticks.iter().any(|&tick| tick.is_nan()) {
        return Err(PltError::BadTickPlacement("tick is NaN".to_owned()));
    }

    // return empty labels for empty ticks
    if ticks.is_empty() {
        return Ok(vec![]);
    }

    let (offset, multiplier, precision) = modifiers;

    // sort ticks
    let mut ticks = ticks.to_vec();
    ticks.sort_by(|a, b| a.partial_cmp(b).unwrap());

    for tick in ticks.iter_mut() {
        *tick = round_to(*tick - offset, 4 - multiplier);
    }

    // shift numbers if necessary
    let shifted_ticks = if multiplier != 0 {
        ticks.iter()
            .map(|&tick| {
                let rounded = (tick * f64::powi(10.0, 3 - multiplier)).round();
                rounded * f64::powi(10.0, -3)
            })
            .collect::<Vec<_>>()
    } else {
        ticks.to_vec()
    };

    let labels = shifted_ticks.iter()
        .map(|tick| format!("{0:.1$}", tick, precision))
        .collect::<Vec<_>>();

    Ok(labels)
}

fn draw_subplot<B: backend::Canvas>(
    canvas: &mut B,
    subplot: &Subplot,
    subplot_area: &draw::Area,
    scaling: f32,
) -> Result<(), PltError> {
    // set formatting parameters

    // line formatting
    let line_width = subplot.format.line_width * scaling.round() as u32;
    let line_color = subplot.format.line_color;

    let grid_color = subplot.format.grid_color;

    // text formatting
    let font_name = subplot.format.font_name;
    let font_size = subplot.format.font_size * scaling;
    let font_color = subplot.format.text_color;

    // colors
    let default_marker_color = subplot.format.default_marker_color;
    let default_fill_color = subplot.format.default_fill_color;

    // major tick formatting
    let inner_major_tick_length = match subplot.format.tick_direction {
        TickDirection::Inner | TickDirection::Both => {
            subplot.format.tick_length * scaling.round() as u32
        },
        _ => 0,
    };
    let outer_major_tick_length = match subplot.format.tick_direction {
        TickDirection::Outer | TickDirection::Both => {
            subplot.format.tick_length * scaling.round() as u32
        },
        _ => 0,
    };
    // minor tick formatting
    let inner_minor_tick_length = match subplot.format.tick_direction {
        TickDirection::Inner | TickDirection::Both => {
            if let Some(length) = subplot.format.override_minor_tick_length {
                length * scaling.round() as u32
            } else {
                subplot.format.tick_length * scaling.round() as u32 / 2
            }
        },
        _ => 0,
    };
    let outer_minor_tick_length = match subplot.format.tick_direction {
        TickDirection::Outer | TickDirection::Both => {
            if let Some(length) = subplot.format.override_minor_tick_length {
                length * scaling.round() as u32
            } else {
                subplot.format.tick_length * scaling.round() as u32 / 2
            }
        },
        _ => 0,
    };

    // layout depends on the font size
    let letter_size = canvas.text_size(draw::TextDescriptor {
        text: format!("{}", 0),
        font: draw::Font {
            name: font_name,
            size: font_size / scaling,
            ..Default::default()
        },
        ..Default::default()
    })?;
    let letter_size = draw::Size {
        width: (letter_size.width as f32 * scaling) as u32,
        height: (letter_size.height as f32 * scaling) as u32,
    };

    // the pixel buffer sizes for fitting text on the figure sides
    let buffer_offset = ((letter_size.height as f64) * 0.6) as u32;
    let mut subplot_buffer = HashMap::from([
        (AxisType::Y, 0),
        (AxisType::SecondaryY, 0),
        (AxisType::SecondaryX, 0),
        (AxisType::X, 0),
    ]);
    let mut label_buffer = HashMap::from([
        (AxisType::Y, 0),
        (AxisType::SecondaryY, 0),
        (AxisType::SecondaryX, 0),
        (AxisType::X, 0),
    ]);
    let mut modifier_buffer = HashMap::from([
        (AxisType::Y, 0),
        (AxisType::SecondaryY, 0),
        (AxisType::SecondaryX, 0),
        (AxisType::X, 0),
    ]);
    let mut tick_label_buffer = HashMap::from([
        (AxisType::Y, 0),
        (AxisType::SecondaryY, 0),
        (AxisType::SecondaryX, 0),
        (AxisType::X, 0),
    ]);
    let mut tick_buffer = HashMap::from([
        (AxisType::Y, 0),
        (AxisType::X, 0),
        (AxisType::SecondaryY, 0),
        (AxisType::SecondaryX, 0),
    ]);

    // get ticks and tick labels
    let mut finalized_axes = HashMap::<AxisType, AxisFinalized>::new();
    for placement in AxisType::iter() {
        let axis = match placement {
            AxisType::Y => &subplot.yaxis,
            AxisType::X => &subplot.xaxis,
            AxisType::SecondaryY => &subplot.secondary_yaxis,
            AxisType::SecondaryX => &subplot.secondary_xaxis,
        };

        // get span and limits for each axis, if None, use values from opposite side
        let (span, limits) = if let (Some(span), Some(limits)) = (axis.span, axis.limits) {
            (span, limits)
        } else {
            // use opposite side, if it has a value, otherwise default to (-1.0, 1.0)
            let opposite_axis = match placement {
                AxisType::X => {
                    &subplot.secondary_xaxis
                },
                AxisType::SecondaryX => {
                    &subplot.xaxis
                },
                AxisType::Y => {
                    &subplot.secondary_yaxis
                },
                AxisType::SecondaryY => {
                    &subplot.yaxis
                },
            };

            if let (Some(span), Some(limits)) = (opposite_axis.span, opposite_axis.limits) {
                (span, limits)
            } else {
                ((-1.0, 1.0), (-1.0, 1.0))
            }
        };

        let is_primary = subplot.plot_infos.iter()
            .any(|info| info.xaxis == placement || info.yaxis == placement)
            | subplot.fill_infos.iter()
            .any(|info| info.xaxis == placement || info.yaxis == placement);

        // get major tick marks
        let major_ticks = if let TickSpacing::Manual(ticks) = &axis.major_tick_marks {
            ticks.clone()
        } else {
            let nticks = match &axis.major_tick_marks {
                TickSpacing::Count(n) => *n,
                TickSpacing::On => 5,
                TickSpacing::Auto => {
                    if is_primary {
                        5
                    } else {
                        0
                    }
                },
                TickSpacing::None => 0,
                _ => 0,
            };

            (0..nticks)
                .map(|n| span.0 + (span.1 - span.0) * (n as f64 / (nticks - 1) as f64))
                .collect::<Vec<_>>()
        };
        // get minor tick marks
        let minor_ticks = if let TickSpacing::Manual(ticks) = &axis.minor_tick_marks {
            ticks.clone()
        } else {
            let nticks_per_major = match &axis.minor_tick_marks {
                TickSpacing::Count(n) => *n,
                TickSpacing::On => 4,
                TickSpacing::Auto => {
                    if is_primary {
                        4
                    } else {
                        0
                    }
                },
                TickSpacing::None => 0,
                _ => 0,
            };

            if !major_ticks.is_empty() {
                let major_tick_delta = (span.1 - span.0) / (major_ticks.len() - 1) as f64;
                let minor_tick_delta = major_tick_delta / (nticks_per_major + 1) as f64;

                let nticks_before_first = ((span.0 - limits.0) as f64 / minor_tick_delta).floor();
                let start = span.0 - (nticks_before_first * minor_tick_delta);
                let nticks = ((limits.1 - start) as f64 / minor_tick_delta).floor() as usize + 1;

            (0..nticks)
                .map(|n| start + (minor_tick_delta * n as f64))
                .collect::<Vec<_>>()
            } else {
                vec![]
            }


        };
        // remove overlap between major and minor ticks
        let minor_ticks = minor_ticks.iter()
            .filter(|tick| !major_ticks.contains(tick))
            .copied()
            .collect::<Vec<_>>();

        // get major tick labels
        let (major_labels, multiplier, offset) = match &axis.major_tick_labels {
            TickLabels::Manual(labels) => (labels.clone(), 0, 0.0),
            TickLabels::On => {
                let modifiers = tick_modifiers(major_ticks.as_slice())?;
                let labels = ticks_to_labels(major_ticks.as_slice(), modifiers)?;
                (labels, modifiers.1, modifiers.0)
            },
            TickLabels::None => (vec![], 0, 0.0),
            TickLabels::Auto => {
                if is_primary {
                    let modifiers = tick_modifiers(major_ticks.as_slice())?;
                    let labels = ticks_to_labels(major_ticks.as_slice(), modifiers)?;
                    (labels, modifiers.1, modifiers.0)
                } else {
                    (vec![], 0, 0.0)
                }
            },
        };
        // get minor tick labels
        let minor_labels = match &axis.minor_tick_labels {
            TickLabels::Manual(labels) => labels.clone(),
            TickLabels::On => {
                let modifiers = tick_modifiers(major_ticks.as_slice())?; // use major modifiers
                ticks_to_labels(minor_ticks.as_slice(), modifiers)?
            },
            TickLabels::None => vec![],
            TickLabels::Auto => {
                if is_primary {
                    let modifiers = tick_modifiers(major_ticks.as_slice())?; // use major modifiers
                    ticks_to_labels(minor_ticks.as_slice(), modifiers)?
                } else {
                    vec![]
                }
            },
        };

        let (major_grid, minor_grid) = match axis.grid {
            Grid::None => (false, false),
            Grid::Major => (true, false),
            Grid::Full => (true, true),
        };

        // adjust buffers

        // add space for outer tick marks if necessary
        if !major_ticks.is_empty() {
            *tick_buffer.get_mut(&placement).unwrap() += outer_major_tick_length;
        } else if !minor_ticks.is_empty() {
            *tick_buffer.get_mut(&placement).unwrap() += outer_minor_tick_length;
        }

        // add space for tick labels if necessary
        if !major_labels.is_empty() {
            let tick_label_size = match placement {
                AxisType::Y | AxisType::SecondaryY => 5 * letter_size.width,
                AxisType::X | AxisType::SecondaryX => letter_size.height,
            };
            *modifier_buffer.get_mut(&placement).unwrap() += tick_label_size;
            *tick_buffer.get_mut(&placement).unwrap() += buffer_offset;
        } else if !minor_labels.is_empty() {
            let tick_label_size = match placement {
                AxisType::Y | AxisType::SecondaryY => 5 * letter_size.width,
                AxisType::X | AxisType::SecondaryX => letter_size.height,
            };
            *modifier_buffer.get_mut(&placement).unwrap() += tick_label_size;
            *tick_buffer.get_mut(&placement).unwrap() += buffer_offset;
        }

        // add space for multiplier and offset if necessary
        if multiplier != 0 || offset != 0.0 {
            match placement {
                AxisType::Y => {
                    *modifier_buffer.get_mut(&AxisType::SecondaryX).unwrap() += letter_size.height * 2 / 3;
                    *tick_label_buffer.get_mut(&AxisType::SecondaryX).unwrap() += buffer_offset;
                },
                AxisType::X => {
                    *modifier_buffer.get_mut(&AxisType::X).unwrap() += letter_size.height * 2 / 3;
                    *tick_label_buffer.get_mut(&AxisType::X).unwrap() += buffer_offset;
                },
                _ => {},
            };
        }

        // add space for axis label if necessary
        if !axis.label.is_empty() {
            //*label_buffer.get_mut(&placement).unwrap() += letter_size.height * 3 / 2;
            *label_buffer.get_mut(&placement).unwrap() += letter_size.height;
            *tick_label_buffer.get_mut(&placement).unwrap() += buffer_offset;
        }

        // adjust total subplot buffer
        *subplot_buffer.get_mut(&placement).unwrap() = if (tick_buffer[&placement]
            + tick_label_buffer[&placement]
            + modifier_buffer[&placement]
            + label_buffer[&placement])
            < letter_size.width * 2
        {
            letter_size.width * 3
        } else {
            buffer_offset
        };

        // save finalized axis info
        finalized_axes.insert(
            placement,
            AxisFinalized {
                label: axis.label.clone(),
                major_tick_locs: major_ticks,
                major_tick_labels: major_labels,
                minor_tick_locs: minor_ticks,
                minor_tick_labels: minor_labels,
                label_multiplier: multiplier,
                label_offset: offset,
                major_grid,
                minor_grid,
                limits,
                visible: axis.visible,
            },
        );
    }

    // add space for title
    let mut title_buffer = 0;
    if !subplot.title.is_empty() {
        title_buffer += letter_size.height;
        *label_buffer.get_mut(&AxisType::SecondaryX).unwrap() += buffer_offset;
    }

    // setup figure areas

    let title_boundary = subplot_area.ymax - subplot_buffer[&AxisType::SecondaryX] - title_buffer;

    let label_boundary = draw::Area {
        xmin: subplot_area.xmin + subplot_buffer[&AxisType::Y] + label_buffer[&AxisType::Y],
        xmax: subplot_area.xmax - subplot_buffer[&AxisType::SecondaryY] - label_buffer[&AxisType::SecondaryY],
        ymin: subplot_area.ymin + subplot_buffer[&AxisType::X] + label_buffer[&AxisType::X],
        ymax: title_boundary - label_buffer[&AxisType::SecondaryX],
    };
    let modifier_boundary = draw::Area {
        xmin: label_boundary.xmin + modifier_buffer[&AxisType::Y],
        xmax: label_boundary.xmax - modifier_buffer[&AxisType::SecondaryY],
        ymin: label_boundary.ymin + modifier_buffer[&AxisType::X],
        ymax: label_boundary.ymax - modifier_buffer[&AxisType::SecondaryX],
    };
    let tick_label_boundary = draw::Area {
        xmin: modifier_boundary.xmin + tick_label_buffer[&AxisType::Y],
        xmax: modifier_boundary.xmax - tick_label_buffer[&AxisType::SecondaryY],
        ymin: modifier_boundary.ymin + tick_label_buffer[&AxisType::X],
        ymax: modifier_boundary.ymax - tick_label_buffer[&AxisType::SecondaryX],
    };
    let tick_boundary = draw::Area {
        xmin: tick_label_boundary.xmin + tick_buffer[&AxisType::Y],
        xmax: tick_label_boundary.xmax - tick_buffer[&AxisType::SecondaryY],
        ymin: tick_label_boundary.ymin + tick_buffer[&AxisType::X],
        ymax: tick_label_boundary.ymax - tick_buffer[&AxisType::SecondaryX],
    };

    // plot area in figure as pixel indices
    let plot_area = draw::Area {
        xmin: tick_boundary.xmin,
        xmax: tick_boundary.xmax,
        ymin: tick_boundary.ymin,
        ymax: tick_boundary.ymax,
    };

    // set plot color
    canvas.draw_shape(draw::ShapeDescriptor {
        point: draw::Point {
            x: plot_area.xmin as f64 + plot_area.xsize() as f64 / 2.0,
            y: plot_area.ymin as f64 + plot_area.ysize() as f64 / 2.0,
        },
        shape: draw::Shape::Rectangle {
            h: plot_area.ysize(),
            w: plot_area.xsize(),
        },
        fill_color: subplot.format.plot_color,
        line_color: Color::TRANSPARENT,
        ..Default::default()
    })?;

    // draw grid lines
    for (placement, axis) in finalized_axes.iter() {
        // draw ticks
        for (ticks, grid) in [
            (&axis.major_tick_locs, &axis.major_grid),
            (&axis.minor_tick_locs, &axis.minor_grid),
        ] {
            // convert tick numbers to pixel locations
            let tick_locs = ticks.iter()
                // convert to fraction
                .map(|tick| (tick - axis.limits.0) / (axis.limits.1 - axis.limits.0))
                // convert to pixel
                .map(|frac| plot_area.fractional_to_point(draw::Point { x: frac, y: frac }))
                .collect::<Vec<_>>();

            // draw grid lines
            if *grid {
                for loc in tick_locs.iter() {
                    let line = match placement {
                        AxisType::Y | AxisType::SecondaryY => draw::Line {
                            p1: draw::Point {
                                x: plot_area.xmin as f64,
                                y: loc.y.round(),
                            },
                            p2: draw::Point {
                                x: plot_area.xmax as f64,
                                y: loc.y.round(),
                            },
                        },
                        AxisType::X | AxisType::SecondaryX => draw::Line {
                            p1: draw::Point {
                                x: loc.x.round(),
                                y: plot_area.ymin as f64,
                            },
                            p2: draw::Point {
                                x: loc.x.round(),
                                y: plot_area.ymax as f64,
                            },
                        },
                    };
                    canvas.draw_line(draw::LineDescriptor {
                        line,
                        line_color: grid_color,
                        line_width,
                        ..Default::default()
                    })?;
                }
            }
        }
    }

    // draw data

    let mut plot_info_iter = subplot.plot_infos.iter();
    let mut fill_info_iter = subplot.fill_infos.iter();

    // if there is a color cycle, default to those colors, otherwise default to black for series
    let default_color = if !subplot.format.color_cycle.is_empty() {
        subplot.format.color_cycle.clone()
    } else {
        vec![default_marker_color]
    };
    let mut default_color = default_color.iter().cycle();

    // if there is a color cycle, default to those colors, otherwise default to red for fill
    let default_fill_color = if !subplot.format.color_cycle.is_empty() {
        subplot.format.color_cycle.iter().map(|&c| Color { a: 0.5, ..c }).collect()
    } else {
        vec![default_fill_color]
    };
    let mut default_fill_color = default_fill_color.iter().cycle();

    // draw all data sets in the order called
    for plot_type in subplot.plot_order.iter() { match plot_type {
        // draw series data
        crate::subplot::PlotType::Series => {
            let plot_info = plot_info_iter.next().unwrap();

            let xlim = finalized_axes[&plot_info.xaxis].limits;
            let ylim = finalized_axes[&plot_info.yaxis].limits;
            let plot_data = &plot_info.data;

            // draw line
            if let Some(line) = plot_info.line {
                let line_color = if let Some(color) = line.color_override {
                    color
                } else {
                    *default_color.next().unwrap()
                };
                let dashes = match line.style {
                    LineStyle::Solid => vec![],
                    LineStyle::Dashed => vec![
                        (10.0 * scaling).into(),
                        (10.0 * scaling).into(),
                        (10.0 * scaling).into(),
                        (10.0 * scaling).into(),
                    ],
                    LineStyle::ShortDashed => vec![
                        (4.0 * scaling).into(),
                        (4.0 * scaling).into(),
                        (4.0 * scaling).into(),
                        (4.0 * scaling).into(),
                    ],
                };
                canvas.draw_curve(draw::CurveDescriptor {
                    points: plot_data.data()
                        .map(|(x, y)| {
                            let xfrac = (x - xlim.0) / (xlim.1 - xlim.0);
                            let yfrac = (y - ylim.0) / (ylim.1 - ylim.0);

                            let point = plot_area.fractional_to_point(draw::Point {
                                x: xfrac,
                                y: yfrac,
                            });
                            if plot_info.pixel_perfect {
                                draw::Point { x: point.x.round(), y: point.y.round() }
                            } else {
                                point
                            }
                        })
                        .collect::<Vec<_>>(),
                    line_color,
                    line_width: line.width * scaling.round() as u32,
                    dashes: dashes.as_slice(),
                    clip_area: Some(plot_area),
                })?;
            }

            // draw markers
            if let Some(marker) = &plot_info.marker {
                let mut shape = match marker.style {
                    MarkerStyle::Circle => draw::Shape::Circle { r: marker.size },
                    MarkerStyle::Square => draw::Shape::Square { l: marker.size },
                };
                shape.scale(scaling.round() as u32);
                let fill_color = if let Some(color) = marker.color_override {
                    color
                } else {
                    *default_color.next().unwrap()
                };
                let line = if marker.outline {
                    marker.outline_format
                } else {
                    Line {
                        style: LineStyle::Solid,
                        width: Line::default().width,
                        color_override: Some(Color::TRANSPARENT),
                    }
                };
                let line_color = if let Some(color) = line.color_override {
                    color
                } else {
                    fill_color
                };
                let line_dashes = match line.style {
                    LineStyle::Solid => vec![],
                    LineStyle::Dashed => vec![
                        (10.0 * scaling).into(),
                        (10.0 * scaling).into(),
                        (10.0 * scaling).into(),
                        (10.0 * scaling).into(),
                    ],
                    LineStyle::ShortDashed => vec![
                        (4.0 * scaling).into(),
                        (4.0 * scaling).into(),
                        (4.0 * scaling).into(),
                        (4.0 * scaling).into(),
                    ],
                };
                for point in plot_data.data().map(|(x, y)| {
                    let xfrac = (x - xlim.0) / (xlim.1 - xlim.0);
                    let yfrac = (y - ylim.0) / (ylim.1 - ylim.0);

                    let point = plot_area.fractional_to_point(draw::Point {
                        x: xfrac,
                        y: yfrac,
                    });

                    if plot_info.pixel_perfect {
                        draw::Point { x: point.x.round(), y: point.y.round() }
                    } else {
                        point
                    }
                }) {
                    canvas.draw_shape(draw::ShapeDescriptor {
                        point,
                        shape,
                        fill_color,
                        line_color,
                        line_width: line.width * scaling.round() as u32,
                        line_dashes: line_dashes.as_slice(),
                        clip_area: Some(plot_area),
                    })?;
                }
            }
        }
        // draw fill data
        crate::subplot::PlotType::Fill => {
            let fill_info = fill_info_iter.next().unwrap();

            let xlim = finalized_axes[&fill_info.xaxis].limits;
            let ylim = finalized_axes[&fill_info.yaxis].limits;
            //let color = fill_info.color;
            let color = if let Some(color) = fill_info.color_override {
                color
            } else {
                *default_fill_color.next().unwrap()
            };
            let data = &fill_info.data;

            let shape_points: Vec<_> = Iterator::chain(data.curve1(), data.curve2().rev())
                .map(|(x, y)| {
                    let xfrac = (x - xlim.0) / (xlim.1 - xlim.0);
                    let yfrac = (y - ylim.0) / (ylim.1 - ylim.0);

                    plot_area.fractional_to_point(draw::Point {
                        x: xfrac,
                        y: yfrac,
                    })
                })
                .collect();

            canvas.fill_region(draw::FillDescriptor {
                points: shape_points,
                fill_color: color,
                clip_area: Some(plot_area),
            })?;
        }
    }}

    // draw axis lines, labels, ticks, and tick labels for each axis
    for (placement, axis) in finalized_axes {
        // get line placement
        let axis_offset = line_width as f64 / 2.0;
        let line = match placement {
            AxisType::Y => draw::Line {
                p1: draw::Point {
                    x: plot_area.xmin as f64,
                    y: plot_area.ymin as f64 + axis_offset,
                },
                p2: draw::Point {
                    x: plot_area.xmin as f64,
                    y: plot_area.ymax as f64 + axis_offset,
                },
            },
            AxisType::SecondaryY => draw::Line {
                p1: draw::Point {
                    x: plot_area.xmax as f64,
                    y: plot_area.ymin as f64 + axis_offset,
                },
                p2: draw::Point {
                    x: plot_area.xmax as f64,
                    y: plot_area.ymax as f64 - axis_offset,
                },
            },
            AxisType::X => draw::Line {
                p1: draw::Point {
                    x: plot_area.xmin as f64 - axis_offset,
                    y: plot_area.ymin as f64,
                },
                p2: draw::Point {
                    x: plot_area.xmax as f64 + axis_offset,
                    y: plot_area.ymin as f64,
                },
            },
            AxisType::SecondaryX => draw::Line {
                p1: draw::Point {
                    x: plot_area.xmin as f64 + axis_offset,
                    y: plot_area.ymax as f64,
                },
                p2: draw::Point {
                    x: plot_area.xmax as f64 + axis_offset,
                    y: plot_area.ymax as f64,
                },
            },
        };

        let axis_line_color = if axis.visible {
            line_color
        } else {
            Color::TRANSPARENT
        };
        // draw axis
        canvas.draw_line(draw::LineDescriptor {
            line,
            line_width,
            line_color: axis_line_color,
            ..Default::default()
        })?;

        // draw tick label modifiers if necessary
        let mult_offset_text = if axis.label_multiplier != 0 && axis.label_offset != 0.0 {
            let exponent = superscript(axis.label_multiplier as u16);
            format!("x10{} + {}", exponent, axis.label_offset)
        } else if axis.label_multiplier != 0 {
            let exponent = superscript(axis.label_multiplier as u16);
            format!("x10{}", exponent)
        } else if axis.label_offset != 0.0 {
            format!("+ {}", axis.label_offset)
        } else {
            String::new()
        };
        // determine position of modifier
        let (modifier_position, modifier_alignment) = match placement {
            AxisType::Y => (
                draw::Point {
                    x: plot_area.xmin as f64 - letter_size.width as f64 / 2.0,
                    y: modifier_boundary.ymax as f64,
                },
                draw::Alignment::BottomLeft,
            ),
            AxisType::SecondaryY => (
                draw::Point {
                    x: plot_area.xmax as f64 - letter_size.width as f64 / 2.0,
                    y: modifier_boundary.ymax as f64,
                },
                draw::Alignment::BottomLeft,
            ),
            AxisType::SecondaryX => (
                draw::Point {
                    x: tick_label_boundary.xmax as f64 + letter_size.width as f64,
                    y: tick_label_boundary.ymax as f64,
                },
                draw::Alignment::BottomLeft,
            ),
            AxisType::X => (
                draw::Point {
                    x: plot_area.xmax as f64,
                    y: modifier_boundary.ymin as f64,
                },
                draw::Alignment::TopRight,
            ),
        };
        canvas.draw_text(draw::TextDescriptor {
            text: mult_offset_text,
            position: modifier_position,
            alignment: modifier_alignment,
            color: font_color,
            font: draw::Font {
                name: font_name,
                size: font_size,
                ..Default::default()
            },
            ..Default::default()
        })?;

        // draw axis label
        let label_font = draw::Font {
            name: font_name,
            size: font_size,
            ..Default::default()
        };
        match placement {
            AxisType::Y => canvas.draw_text(draw::TextDescriptor {
                text: axis.label,
                position: draw::Point {
                    x: label_boundary.xmin as f64,
                    y: (plot_area.ymax + plot_area.ymin) as f64 / 2.0,
                },
                alignment: draw::Alignment::Right,
                rotation: 1.5 * f64::consts::PI,
                color: font_color,
                font: label_font,
                ..Default::default()
            })?,
            AxisType::X => canvas.draw_text(draw::TextDescriptor {
                text: axis.label,
                position: draw::Point {
                    x: (plot_area.xmax + plot_area.xmin) as f64 / 2.0,
                    y: label_boundary.ymin as f64,
                },
                alignment: draw::Alignment::Top,
                rotation: 0.0,
                color: font_color,
                font: label_font,
                ..Default::default()
            })?,
            AxisType::SecondaryY => canvas.draw_text(draw::TextDescriptor {
                text: axis.label,
                position: draw::Point {
                    x: label_boundary.xmax as f64,
                    y: (plot_area.ymax + plot_area.ymin) as f64 / 2.0,
                },
                alignment: draw::Alignment::Left,
                rotation: 0.5 * f64::consts::PI,
                color: font_color,
                font: label_font,
                ..Default::default()
            })?,
            AxisType::SecondaryX => canvas.draw_text(draw::TextDescriptor {
                text: axis.label,
                position: draw::Point {
                    x: (plot_area.xmax + plot_area.xmin) as f64 / 2.0,
                    y: label_boundary.ymax as f64,
                },
                alignment: draw::Alignment::Bottom,
                rotation: 0.0,
                color: font_color,
                font: label_font,
                ..Default::default()
            })?,
        }

        // draw ticks
        for (ticks, labels, outer_tick_length, inner_tick_length) in [
            (
                axis.major_tick_locs,
                axis.major_tick_labels,
                outer_major_tick_length,
                inner_major_tick_length,
            ),
            (
                axis.minor_tick_locs,
                axis.minor_tick_labels,
                outer_minor_tick_length,
                inner_minor_tick_length,
            ),
        ] {
            // deal with cases of no provided labels or wrong number of labels
            let labels = if labels.is_empty() {
                (0..ticks.len()).map(|_| String::new()).collect()
            } else if labels.len() != ticks.len() {
                let axis = match placement {
                    AxisType::Y => "y-axis",
                    AxisType::X => "x-axis",
                    AxisType::SecondaryY => "secondary y-axis",
                    AxisType::SecondaryX => "secondary x-axis",
                };
                return Err(PltError::BadTickLabels(format!(
                    "number of tick labels does not match number of ticks on {}",
                    axis,
                )));
            } else {
                labels
            };

            // convert tick numbers to pixel locations
            let tick_locs = ticks.iter()
                // convert to fraction
                .map(|tick| (tick - axis.limits.0) / (axis.limits.1 - axis.limits.0))
                // convert to pixel
                .map(|frac| plot_area.fractional_to_point(draw::Point { x: frac, y: frac }))
                .collect::<Vec<_>>();

            // draw ticks and labels
            for (tick, loc) in iter::zip(labels, tick_locs) {
                // get positions specific to the axis
                let (tick_line, text_position, text_alignment) = match placement {
                    AxisType::Y => (
                        draw::Line {
                            p1: draw::Point {
                                x: (plot_area.xmin - outer_tick_length) as f64,
                                y: loc.y.round(),
                            },
                            p2: draw::Point {
                                x: (plot_area.xmin + inner_tick_length) as f64,
                                y: loc.y.round(),
                            },
                        },
                        draw::Point {
                            x: tick_label_boundary.xmin as f64,
                            y: loc.y.round(),
                        },
                        draw::Alignment::Right,
                    ),
                    AxisType::X => (
                        draw::Line {
                            p1: draw::Point {
                                x: loc.x.round(),
                                y: (plot_area.ymin - outer_tick_length) as f64,
                            },
                            p2: draw::Point {
                                x: loc.x.round(),
                                y: (plot_area.ymin + inner_tick_length) as f64,
                            },
                        },
                        draw::Point {
                            x: loc.x.round(),
                            y: tick_label_boundary.ymin as f64,
                        },
                        draw::Alignment::Top,
                    ),
                    AxisType::SecondaryY => (
                        draw::Line {
                            p1: draw::Point {
                                x: (plot_area.xmax - inner_tick_length) as f64,
                                y: loc.y.round(),
                            },
                            p2: draw::Point {
                                x: (plot_area.xmax + outer_tick_length) as f64,
                                y: loc.y.round(),
                            },
                        },
                        draw::Point {
                            x: tick_label_boundary.xmax as f64,
                            y: loc.y.round(),
                        },
                        draw::Alignment::Left,
                    ),
                    AxisType::SecondaryX => (
                        draw::Line {
                            p1: draw::Point {
                                x: loc.x.round(),
                                y: (plot_area.ymax - inner_tick_length) as f64,
                            },
                            p2: draw::Point {
                                x: loc.x.round(),
                                y: (plot_area.ymax + outer_tick_length) as f64,
                            },
                        },
                        draw::Point {
                            x: loc.x.round(),
                            y: tick_label_boundary.ymax as f64,
                        },
                        draw::Alignment::Bottom,
                    ),
                };

                // draw line and text
                canvas.draw_line(draw::LineDescriptor {
                    line: tick_line,
                    line_color,
                    line_width,
                    ..Default::default()
                })?;
                canvas.draw_text(draw::TextDescriptor {
                    text: tick.to_string(),
                    position: text_position,
                    alignment: text_alignment,
                    color: font_color,
                    font: draw::Font {
                        name: font_name,
                        size: font_size,
                        ..Default::default()
                    },
                    ..Default::default()
                })?;
            }
        }
    }

    // draw title
    canvas.draw_text(draw::TextDescriptor {
        text: subplot.title.clone(),
        position: draw::Point {
            x: (plot_area.xmax + plot_area.xmin) as f64 / 2.0,
            y: title_boundary as f64,
        },
        alignment: draw::Alignment::Bottom,
        color: font_color,
        font: draw::Font {
            name: font_name,
            size: font_size,
            ..Default::default()
        },
        ..Default::default()
    })?;

    Ok(())
}