ggplot-rs 0.29.0

A Rust implementation of ggplot2's Grammar of Graphics
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
//! Decorative-layer support (#161): a way to mark a layer as purely
//! decorative (e.g. a boxplot's own mean dot, drawn by a separate
//! `geom_point` layer) so its marks carry **no** per-mark metadata —
//! `<title>`, `data-x`/`data-series`/`data-value`/`data-fvalue`, and
//! `data_attr`-driven attributes — while keeping their own visual style.
//! Without this, any `Geom::draw` that calls [`super::set_mark`]/
//! [`super::set_row_extras`] gets full mark metadata unconditionally, so a
//! decorative mark looks like an independent, selectable data point to any
//! SVG consumer that keys off "has a non-empty `<title>`" (hover lists, a
//! per-x crosshair, click-to-select). Each mark also carries
//! `data-part="decorative"` and `aria-hidden="true"` (`data-geom` still
//! names the real geom), so a consumer can positively select decorative
//! marks too, rather than only infer them from what they lack.
//!
//! [`GGPlot::decorative`](crate::plot::GGPlot::decorative) swaps the last
//! layer's `geom` for a private [`Decorative`] wrapper that delegates every
//! [`Geom`] method to the wrapped geom except `draw`, which runs the
//! wrapped geom against a [`MuteMarks`] backend wrapper turning the
//! per-mark-metadata setters into no-ops. This adds no field to `Layer`
//! (`Layer`'s fields are all `pub` with no `#[non_exhaustive]`, so a field
//! addition would be a semver break for a patch release) and no field to
//! any concrete geom.
//!
//! Nothing in the crate downcasts a `Box<dyn Geom>` (the `Geom` trait has
//! no `as_any`/`downcast`), so wrapping it this way is safe: no code path
//! needs to recover the original concrete geom type.

use crate::coord::Coord;
use crate::data::DataFrame;
use crate::position::Position;
use crate::render::backend::{DrawBackend, LineStyle, PointStyle, RectStyle, TextStyle};
use crate::render::RenderError;
use crate::scale::ScaleSet;
use crate::stat::Stat;
use crate::theme::Theme;

use super::{Aesthetic, Geom, GeomParams};

/// A [`DrawBackend`] wrapper that forwards drawing and query calls
/// unchanged but turns every per-mark-metadata setter — tooltip, `data-x`/
/// `data-series`/`data-value`/`data-fvalue`, and `data_attr`-driven
/// attributes — into a no-op, so a decorative layer's marks keep their
/// visual style (`draw_circle`/`draw_line`/...) but get none of that
/// metadata.
///
/// It still forwards [`set_mark_geom`](DrawBackend::set_mark_geom) and
/// [`set_mark_part`](DrawBackend::set_mark_part) to the inner backend
/// (further below), so a decorative mark keeps its `data-geom` (e.g.
/// `data-geom="point"`) but every one of its marks additionally carries
/// `data-part="decorative"` and `aria-hidden="true"` (review finding, PR
/// #166: without this, a decorative mark still looked exactly like an
/// ordinary data mark — `class="gg-mark gg-point" data-geom="point"` —
/// to any consumer that selects on those alone, even once its `<title>`/
/// `data-x`/.../`data_attr` were muted). `DECORATIVE_PART` is the same
/// mechanism `geom::arrow`'s own arrowhead sub-parts use for `"arrow"`.
const DECORATIVE_PART: &str = "decorative";

struct MuteMarks<'a> {
    inner: &'a mut dyn DrawBackend,
}

impl DrawBackend for MuteMarks<'_> {
    fn draw_circle(
        &mut self,
        center: (f64, f64),
        radius: f64,
        style: &PointStyle,
    ) -> Result<(), RenderError> {
        self.inner.draw_circle(center, radius, style)
    }

    fn draw_line(&mut self, points: &[(f64, f64)], style: &LineStyle) -> Result<(), RenderError> {
        self.inner.draw_line(points, style)
    }

    fn draw_rect(
        &mut self,
        top_left: (f64, f64),
        bottom_right: (f64, f64),
        style: &RectStyle,
    ) -> Result<(), RenderError> {
        self.inner.draw_rect(top_left, bottom_right, style)
    }

    fn draw_text(
        &mut self,
        text: &str,
        pos: (f64, f64),
        style: &TextStyle,
    ) -> Result<(), RenderError> {
        self.inner.draw_text(text, pos, style)
    }

    fn draw_polygon(
        &mut self,
        points: &[(f64, f64)],
        style: &RectStyle,
    ) -> Result<(), RenderError> {
        self.inner.draw_polygon(points, style)
    }

    fn plot_area(&self) -> crate::render::Rect {
        self.inner.plot_area()
    }

    fn total_area(&self) -> crate::render::Rect {
        self.inner.total_area()
    }

    // `DrawBackend::draw_shape`/`draw_quadratic` have *default* trait
    // bodies that unconditionally fall back to `self.draw_circle`/
    // `self.draw_line` (for a backend that doesn't implement real shapes/
    // curves) — without an explicit override here, `self` resolves to
    // `MuteMarks` itself, so a shape-mapped point would flatten to a
    // circle and a `geom_curve` to a straight polyline even when the
    // *inner* backend (e.g. the SVG backend) implements both properly.
    // Forward both explicitly, the same way `PanelBackendAdapter`/the
    // bounding-box adapter in `compose.rs` already do.
    fn draw_shape(
        &mut self,
        center: (f64, f64),
        radius: f64,
        style: &PointStyle,
    ) -> Result<(), RenderError> {
        self.inner.draw_shape(center, radius, style)
    }

    fn draw_quadratic(
        &mut self,
        p0: (f64, f64),
        ctrl: (f64, f64),
        p1: (f64, f64),
        style: &LineStyle,
    ) -> Result<(), RenderError> {
        self.inner.draw_quadratic(p0, ctrl, p1, style)
    }

    // Per-mark metadata: muted. `Aes::tooltip`/`Aes::data_attr` must have
    // no visible effect on a decorative mark, however the wrapped geom's
    // own `draw` calls `set_mark`/`set_row_extras`.
    fn set_tooltip(&mut self, _tooltip: Option<String>) {}
    fn set_mark_axis(&mut self, _key: Option<String>) {}
    fn set_mark_series(&mut self, _series: Option<String>) {}
    fn set_mark_value(&mut self, _value: Option<String>) {}
    fn set_mark_attrs(&mut self, _attrs: Vec<(String, String)>) {}
    fn set_mark_fvalue(&mut self, _value: Option<String>) {}
    fn set_mark_fx(&mut self, _value: Option<String>) {}

    // Not metadata about *this* mark being a data point — forwarded as-is.
    fn set_mark_geom(&mut self, geom: Option<&str>) {
        self.inner.set_mark_geom(geom);
    }

    // A genuine sub-part name (e.g. `"arrow"`, from `geom::arrow`'s own
    // bracketed `set_mark_part(Some("arrow"))`/`set_mark_part(None)` around
    // an arrowhead) forwards as-is; a clear-back-to-`None` is turned into
    // `DECORATIVE_PART` instead of a true clear (review finding, PR #166:
    // without this, a mid-draw arrow bracket's own `set_mark_part(None)`
    // would otherwise drop the decorative tag for every mark drawn after
    // it in the same layer). `Decorative::draw` primes the real `"None"` →
    // `DECORATIVE_PART` state before delegating and restores a true `None`
    // afterwards, so nothing leaks into the next layer.
    fn set_mark_part(&mut self, part: Option<&str>) {
        self.inner
            .set_mark_part(Some(part.unwrap_or(DECORATIVE_PART)));
    }

    fn warn(&mut self, message: String) {
        self.inner.warn(message);
    }

    // Styling/structure, not data-point metadata (#221): forwarded, so a
    // decorative layer can still be classed, grouped and made inert.
    fn set_mark_class(&mut self, class: Option<String>) {
        self.inner.set_mark_class(class)
    }

    fn set_mark_group(&mut self, group: Option<(String, String)>) {
        self.inner.set_mark_group(group)
    }

    fn set_layer_attrs(&mut self, attrs: Vec<(String, String)>) {
        self.inner.set_layer_attrs(attrs)
    }

    fn wants_formatted_values(&self) -> bool {
        self.inner.wants_formatted_values()
    }
}

/// Wraps a [`Geom`] so its drawn marks carry no per-mark metadata (#161).
/// See the module docs for why this is a wrapper rather than a field.
pub(crate) struct Decorative(pub(crate) Box<dyn Geom>);

impl Geom for Decorative {
    fn draw(
        &self,
        data: &DataFrame,
        coord: &dyn Coord,
        scales: &ScaleSet,
        theme: &Theme,
        backend: &mut dyn DrawBackend,
    ) -> Result<(), RenderError> {
        // Prime the real backend's own mark-part state to `DECORATIVE_PART`
        // before delegating (nothing else would — most geoms never call
        // `set_mark_part` at all), and restore a true `None` afterwards so
        // nothing leaks into whatever draws next.
        backend.set_mark_part(Some(DECORATIVE_PART));
        let result = {
            let mut muted = MuteMarks { inner: backend };
            self.0.draw(data, coord, scales, theme, &mut muted)
        };
        backend.set_mark_part(None);
        result
    }

    fn required_aes(&self) -> Vec<Aesthetic> {
        self.0.required_aes()
    }

    fn default_stat(&self) -> Box<dyn Stat> {
        self.0.default_stat()
    }

    fn default_position(&self) -> Box<dyn Position> {
        self.0.default_position()
    }

    fn default_params(&self) -> GeomParams {
        self.0.default_params()
    }

    fn name(&self) -> &str {
        self.0.name()
    }

    fn set_series_color(&mut self, color: (u8, u8, u8)) {
        self.0.set_series_color(color);
    }

    fn apply_element_geom(&mut self, el: &crate::theme::elements::ElementGeom) {
        self.0.apply_element_geom(el);
    }

    fn include_zero_baseline(&self) -> bool {
        self.0.include_zero_baseline()
    }

    fn allows_infinite(&self) -> bool {
        self.0.allows_infinite()
    }

    fn keeps_na_as_break(&self) -> bool {
        self.0.keeps_na_as_break()
    }

    // Always false: even if the wrapped geom reads `Aes::tooltip`, the
    // `MuteMarks` wrapper above suppresses the resulting `<title>`, so a
    // `.tooltip(...)` template on a decorative layer has no visible effect
    // — the build pipeline's existing has-no-effect warning should fire,
    // the same as for any other geom that doesn't consume tooltips.
    fn consumes_tooltips(&self) -> bool {
        false
    }

    fn setup_data(&self, data: &mut DataFrame, warnings: &mut Vec<String>) {
        self.0.setup_data(data, warnings);
    }

    fn inherit_aes(&self) -> bool {
        self.0.inherit_aes()
    }

    fn mapped_only_columns(&self) -> &'static [&'static str] {
        self.0.mapped_only_columns()
    }

    fn validate_layer(&self, pre_stat_columns: &[String], stat_name: &str) -> Result<(), String> {
        self.0.validate_layer(pre_stat_columns, stat_name)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::geom::point::GeomPoint;

    #[test]
    fn decorative_delegates_name_and_required_aes() {
        let inner = GeomPoint::default();
        let name = inner.name().to_string();
        let required = inner.required_aes();
        let wrapped = Decorative(Box::new(inner));
        assert_eq!(wrapped.name(), name);
        assert_eq!(wrapped.required_aes(), required);
    }

    #[test]
    fn decorative_delegates_default_stat_position_and_params() {
        let plain = GeomPoint::default();
        let (stat_name, position_name) = (
            plain.default_stat().name().to_string(),
            plain.default_position().name().to_string(),
        );
        let wrapped = Decorative(Box::new(GeomPoint::default()));
        assert_eq!(wrapped.default_stat().name(), stat_name);
        assert_eq!(wrapped.default_position().name(), position_name);
        // `GeomParams` has no `PartialEq`; compare via `Debug`.
        assert_eq!(
            format!("{:?}", wrapped.default_params()),
            format!("{:?}", GeomPoint::default().default_params())
        );
    }

    #[test]
    fn decorative_never_consumes_tooltips() {
        // GeomPoint itself consumes tooltips; the wrapper must not, since
        // the wrapped tooltip would have no visible effect.
        assert!(GeomPoint::default().consumes_tooltips());
        let wrapped = Decorative(Box::new(GeomPoint::default()));
        assert!(!wrapped.consumes_tooltips());
    }

    #[test]
    fn decorative_delegates_set_series_color_and_apply_element_geom() {
        // Neither `Geom` method is observable from outside the trait
        // (no `as_any`/`Debug` on every geom), so exercise both through a
        // full render: a decorative `geom_line` with no explicit colour
        // still picks up the brand colour via `set_series_color`, and
        // still recolours from theme ink via `apply_element_geom` when
        // there's no brand colour — the same as a non-decorative layer.
        use crate::aes::Aes;
        use crate::data::Value;
        use crate::plot::GGPlot;
        let data = vec![
            ("x".to_string(), vec![Value::Float(1.0), Value::Float(2.0)]),
            ("y".to_string(), vec![Value::Float(1.0), Value::Float(2.0)]),
        ];

        let brand_svg = GGPlot::new(data.clone())
            .aes(Aes::new().x("x").y("y"))
            .geom_line()
            .decorative()
            .primary_color((9, 9, 9))
            .render_svg_native_with_size(200, 200)
            .unwrap();
        assert!(brand_svg.contains("#090909"), "{brand_svg}");

        let ink_svg = GGPlot::new(data)
            .aes(Aes::new().x("x").y("y"))
            .geom_line()
            .decorative()
            .theme(Theme::default().with_ink_paper((10, 20, 30), (255, 255, 255)))
            .render_svg_native_with_size(200, 200)
            .unwrap();
        assert!(ink_svg.contains("#0A141E"), "{ink_svg}");
    }

    #[test]
    fn decorative_delegates_include_zero_baseline_allows_infinite_and_keeps_na_as_break() {
        use crate::geom::bar::GeomBar;
        use crate::geom::line::GeomLine;
        use crate::geom::rect::GeomRect;

        let bar = Decorative(Box::new(GeomBar::default()));
        assert!(bar.include_zero_baseline());
        assert!(!Decorative(Box::new(GeomPoint::default())).include_zero_baseline());

        let rect = Decorative(Box::new(GeomRect::default()));
        assert!(rect.allows_infinite());
        assert!(!Decorative(Box::new(GeomPoint::default())).allows_infinite());

        let line = Decorative(Box::new(GeomLine::default()));
        assert!(line.keeps_na_as_break());
        assert!(!Decorative(Box::new(GeomPoint::default())).keeps_na_as_break());
    }

    #[test]
    fn decorative_delegates_inherit_aes() {
        use crate::geom::refline::GeomHline;
        // Reference lines return false; an ordinary geom returns true.
        assert!(!Decorative(Box::new(GeomHline::new(1.0))).inherit_aes());
        assert!(Decorative(Box::new(GeomPoint::default())).inherit_aes());
    }

    #[test]
    fn decorative_delegates_mapped_only_columns_and_validate_layer() {
        use crate::geom::boxplot::GeomBoxplot;
        let wrapped = Decorative(Box::new(GeomBoxplot::default()));
        assert_eq!(
            wrapped.mapped_only_columns(),
            GeomBoxplot::default().mapped_only_columns()
        );
        // stat="identity" with none of the five-number-summary columns
        // mapped must still error through the wrapper.
        assert!(wrapped.validate_layer(&[], "identity").is_err());
        assert!(wrapped
            .validate_layer(&["y".to_string()], "boxplot")
            .is_ok());
    }

    #[test]
    fn decorative_delegates_setup_data() {
        use crate::data::Value;
        use crate::geom::boxplot::GeomBoxplot;

        fn frame() -> DataFrame {
            let mut d = DataFrame::new();
            d.add_column("outliers".to_string(), vec![Value::Str("1,2".to_string())]);
            d.add_column("ymin".to_string(), vec![Value::Float(0.0)]);
            d.add_column("ymax".to_string(), vec![Value::Float(10.0)]);
            d
        }

        let mut plain_data = frame();
        let mut plain_warnings = Vec::new();
        GeomBoxplot::default().setup_data(&mut plain_data, &mut plain_warnings);

        let mut wrapped_data = frame();
        let mut wrapped_warnings = Vec::new();
        Decorative(Box::new(GeomBoxplot::default()))
            .setup_data(&mut wrapped_data, &mut wrapped_warnings);

        assert_eq!(
            wrapped_data.column_names(),
            plain_data.column_names(),
            "setup_data must reach the wrapped geom, adding the same columns"
        );
    }

    /// A minimal `DrawBackend` that records every call, so `MuteMarks`'s
    /// forwarding (drawing/query calls) and muting (per-mark metadata) can
    /// be asserted directly without going through a full plot render.
    #[derive(Default)]
    struct MockBackend {
        circles: usize,
        lines: usize,
        rects: usize,
        texts: usize,
        polygons: usize,
        mark_geom_calls: usize,
        mark_part_calls: usize,
        last_mark_part: Option<String>,
        warn_calls: usize,
        tooltip_calls: usize,
        mark_axis_calls: usize,
        mark_series_calls: usize,
        mark_value_calls: usize,
        mark_attrs_calls: usize,
        mark_fvalue_calls: usize,
        mark_fx_calls: usize,
    }

    impl DrawBackend for MockBackend {
        fn draw_circle(
            &mut self,
            _center: (f64, f64),
            _radius: f64,
            _style: &PointStyle,
        ) -> Result<(), RenderError> {
            self.circles += 1;
            Ok(())
        }
        fn draw_line(
            &mut self,
            _points: &[(f64, f64)],
            _style: &LineStyle,
        ) -> Result<(), RenderError> {
            self.lines += 1;
            Ok(())
        }
        fn draw_rect(
            &mut self,
            _tl: (f64, f64),
            _br: (f64, f64),
            _style: &RectStyle,
        ) -> Result<(), RenderError> {
            self.rects += 1;
            Ok(())
        }
        fn draw_text(
            &mut self,
            _text: &str,
            _pos: (f64, f64),
            _style: &TextStyle,
        ) -> Result<(), RenderError> {
            self.texts += 1;
            Ok(())
        }
        fn draw_polygon(
            &mut self,
            _points: &[(f64, f64)],
            _style: &RectStyle,
        ) -> Result<(), RenderError> {
            self.polygons += 1;
            Ok(())
        }
        fn plot_area(&self) -> crate::render::Rect {
            crate::render::Rect {
                x: 1.0,
                y: 2.0,
                width: 3.0,
                height: 4.0,
            }
        }
        fn total_area(&self) -> crate::render::Rect {
            crate::render::Rect {
                x: 5.0,
                y: 6.0,
                width: 7.0,
                height: 8.0,
            }
        }
        fn set_tooltip(&mut self, _tooltip: Option<String>) {
            self.tooltip_calls += 1;
        }
        fn set_mark_axis(&mut self, _key: Option<String>) {
            self.mark_axis_calls += 1;
        }
        fn set_mark_series(&mut self, _series: Option<String>) {
            self.mark_series_calls += 1;
        }
        fn set_mark_value(&mut self, _value: Option<String>) {
            self.mark_value_calls += 1;
        }
        fn set_mark_geom(&mut self, _geom: Option<&str>) {
            self.mark_geom_calls += 1;
        }
        fn warn(&mut self, _message: String) {
            self.warn_calls += 1;
        }
        fn set_mark_part(&mut self, part: Option<&str>) {
            self.mark_part_calls += 1;
            self.last_mark_part = part.map(str::to_string);
        }
        fn set_mark_attrs(&mut self, _attrs: Vec<(String, String)>) {
            self.mark_attrs_calls += 1;
        }
        fn wants_formatted_values(&self) -> bool {
            true
        }
        fn set_mark_fvalue(&mut self, _value: Option<String>) {
            self.mark_fvalue_calls += 1;
        }
        fn set_mark_fx(&mut self, _value: Option<String>) {
            self.mark_fx_calls += 1;
        }
    }

    #[test]
    fn mute_marks_forwards_drawing_and_query_calls() {
        let mut inner = MockBackend::default();
        {
            let mut muted = MuteMarks { inner: &mut inner };
            muted
                .draw_circle((0.0, 0.0), 1.0, &PointStyle::default())
                .unwrap();
            muted
                .draw_line(&[(0.0, 0.0), (1.0, 1.0)], &LineStyle::default())
                .unwrap();
            muted
                .draw_rect((0.0, 0.0), (1.0, 1.0), &RectStyle::default())
                .unwrap();
            muted
                .draw_text("hi", (0.0, 0.0), &TextStyle::default())
                .unwrap();
            muted
                .draw_polygon(&[(0.0, 0.0)], &RectStyle::default())
                .unwrap();
            assert_eq!(muted.plot_area().x, 1.0);
            assert_eq!(muted.total_area().x, 5.0);
            muted.set_mark_geom(Some("point"));
            muted.set_mark_part(Some("arrow"));
            muted.warn("oops".to_string());
            assert!(muted.wants_formatted_values());
        }
        assert_eq!(inner.circles, 1);
        assert_eq!(inner.lines, 1);
        assert_eq!(inner.rects, 1);
        assert_eq!(inner.texts, 1);
        assert_eq!(inner.polygons, 1);
        assert_eq!(inner.mark_geom_calls, 1);
        assert_eq!(inner.mark_part_calls, 1);
        assert_eq!(inner.warn_calls, 1);
    }

    #[test]
    fn mute_marks_mutes_per_mark_metadata() {
        let mut inner = MockBackend::default();
        {
            let mut muted = MuteMarks { inner: &mut inner };
            muted.set_tooltip(Some("tip".to_string()));
            muted.set_mark_axis(Some("1".to_string()));
            muted.set_mark_series(Some("a".to_string()));
            muted.set_mark_value(Some("1".to_string()));
            muted.set_mark_attrs(vec![("x".to_string(), "y".to_string())]);
            muted.set_mark_fvalue(Some("1".to_string()));
            muted.set_mark_fx(Some("1".to_string()));
        }
        // None of these reached the inner backend.
        assert_eq!(inner.tooltip_calls, 0);
        assert_eq!(inner.mark_axis_calls, 0);
        assert_eq!(inner.mark_series_calls, 0);
        assert_eq!(inner.mark_value_calls, 0);
        assert_eq!(inner.mark_attrs_calls, 0);
        assert_eq!(inner.mark_fvalue_calls, 0);
        assert_eq!(inner.mark_fx_calls, 0);
    }

    #[test]
    fn mute_marks_turns_a_part_clear_into_decorative_but_preserves_a_real_sub_part() {
        let mut inner = MockBackend::default();
        // A `None` clear (what every well-behaved geom calls once it's
        // done with its own sub-part, or what a geom that never touches
        // `set_mark_part` at all never calls in the first place) must not
        // reach the inner backend as a true clear — it must come through
        // as `DECORATIVE_PART`, so a later mark in the same draw doesn't
        // lose the decorative tag.
        MuteMarks { inner: &mut inner }.set_mark_part(None);
        assert_eq!(inner.last_mark_part.as_deref(), Some(DECORATIVE_PART));
        // A genuine sub-part (e.g. an arrowhead) still forwards as itself,
        // unchanged.
        MuteMarks { inner: &mut inner }.set_mark_part(Some("arrow"));
        assert_eq!(inner.last_mark_part.as_deref(), Some("arrow"));
        // And clearing *that* reverts to decorative, not a true clear.
        MuteMarks { inner: &mut inner }.set_mark_part(None);
        assert_eq!(inner.last_mark_part.as_deref(), Some(DECORATIVE_PART));
        assert_eq!(inner.mark_part_calls, 3);
    }

    #[test]
    fn mock_backend_itself_records_the_metadata_calls_mute_marks_suppresses() {
        // Sanity on the mock used above: called directly (not through
        // `MuteMarks`), its own per-mark-metadata methods do record —
        // confirming `mute_marks_mutes_per_mark_metadata`'s zero counts
        // come from `MuteMarks` muting them, not from the mock being a
        // no-op itself.
        let mut backend = MockBackend::default();
        backend.set_tooltip(Some("tip".to_string()));
        backend.set_mark_axis(Some("1".to_string()));
        backend.set_mark_series(Some("a".to_string()));
        backend.set_mark_value(Some("1".to_string()));
        backend.set_mark_attrs(vec![("x".to_string(), "y".to_string())]);
        backend.set_mark_fvalue(Some("1".to_string()));
        backend.set_mark_fx(Some("1".to_string()));
        assert_eq!(backend.tooltip_calls, 1);
        assert_eq!(backend.mark_axis_calls, 1);
        assert_eq!(backend.mark_series_calls, 1);
        assert_eq!(backend.mark_value_calls, 1);
        assert_eq!(backend.mark_attrs_calls, 1);
        assert_eq!(backend.mark_fvalue_calls, 1);
        assert_eq!(backend.mark_fx_calls, 1);
    }

    #[test]
    fn decorative_draw_runs_the_wrapped_geom_through_mute_marks() {
        // End-to-end sanity: `Decorative::draw` actually constructs a
        // `MuteMarks` around the given backend and calls the wrapped
        // geom's own `draw`, rather than skipping it.
        use crate::coord::cartesian::CoordCartesian;
        use crate::data::Value;

        let mut data = DataFrame::new();
        data.add_column("x".to_string(), vec![Value::Float(1.0)]);
        data.add_column("y".to_string(), vec![Value::Float(1.0)]);
        let coord = CoordCartesian::default();
        let scales = ScaleSet::default();
        let theme = Theme::default();
        let mut backend = MockBackend::default();
        let wrapped = Decorative(Box::new(GeomPoint::default()));
        wrapped
            .draw(&data, &coord, &scales, &theme, &mut backend)
            .unwrap();
        assert_eq!(
            backend.circles, 1,
            "the wrapped geom's own mark must still be drawn"
        );
        assert_eq!(
            backend.tooltip_calls, 0,
            "but its tooltip call must be muted"
        );
    }
}