native-theme 0.6.1

Cross-platform native theme detection and loading for Rust GUI applications
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
// Animated icon types: AnimatedIcon, TransformAnimation
//
// These types define the data model for animated icons in the native-theme
// icon system. Frame-based animations supply pre-rendered frames with timing;
// transform-based animations describe a CSS-like transform on a single icon.
//
// AnimatedIcon variant fields are private (via FramesData/TransformData wrapper
// structs) so that construction must go through validated constructors. This
// prevents invalid states like empty frame lists or zero durations.

use std::num::NonZeroU32;
use std::ops::Deref;

use serde::{Deserialize, Serialize};

use super::icons::IconData;

/// Error returned when attempting to create a [`FrameList`] from an empty vec.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EmptyFrameListError;

impl std::fmt::Display for EmptyFrameListError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("frame list must not be empty")
    }
}

impl std::error::Error for EmptyFrameListError {}

/// A non-empty list of icon frames for animation.
///
/// Construction via [`FrameList::new()`] enforces the non-empty invariant.
/// Derefs to `&[IconData]` for ergonomic slice access.
///
/// # Examples
///
/// ```
/// use native_theme::theme::{FrameList, IconData};
/// use std::borrow::Cow;
///
/// let frames = FrameList::new(vec![
///     IconData::Svg(Cow::Borrowed(b"<svg>frame1</svg>")),
/// ]);
/// assert!(frames.is_ok());
///
/// let empty = FrameList::new(vec![]);
/// assert!(empty.is_err());
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct FrameList(Vec<IconData>);

impl FrameList {
    /// Create a new `FrameList`, returning `Err` if `frames` is empty.
    pub fn new(frames: Vec<IconData>) -> Result<Self, EmptyFrameListError> {
        if frames.is_empty() {
            return Err(EmptyFrameListError);
        }
        Ok(Self(frames))
    }

    /// Returns the first frame (infallible -- list is guaranteed non-empty).
    pub fn first(&self) -> &IconData {
        // INVARIANT: FrameList is only constructible via new() which rejects empty,
        // or via custom Deserialize which also rejects empty. Safe Rust cannot
        // produce an empty FrameList; only unsafe transmute could violate it.
        // The `debug_assert!` is defense in depth for debug builds.
        debug_assert!(
            !self.0.is_empty(),
            "FrameList invariant violated: empty list"
        );
        #[allow(clippy::indexing_slicing)]
        {
            &self.0[0]
        }
    }

    /// Returns the number of frames (always >= 1).
    ///
    /// `is_empty()` is intentionally not provided because a `FrameList` is
    /// guaranteed non-empty by construction.
    #[must_use]
    #[allow(clippy::len_without_is_empty)]
    pub fn len(&self) -> usize {
        self.0.len()
    }
}

impl Deref for FrameList {
    type Target = [IconData];
    fn deref(&self) -> &[IconData] {
        &self.0
    }
}

// Custom Deserialize that enforces the non-empty invariant at the
// deserialization boundary (T-87-01 mitigation). Do NOT derive Deserialize.
impl<'de> serde::Deserialize<'de> for FrameList {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let frames = Vec::<IconData>::deserialize(deserializer)?;
        FrameList::new(frames).map_err(|_| serde::de::Error::custom("frame list must not be empty"))
    }
}

/// A CSS-like transform animation applied to a single icon.
///
/// # Examples
///
/// ```
/// use native_theme::theme::TransformAnimation;
/// use std::num::NonZeroU32;
///
/// let spin = TransformAnimation::Spin {
///     duration_ms: NonZeroU32::new(1000).unwrap(),
/// };
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum TransformAnimation {
    /// Continuous 360-degree rotation.
    Spin {
        /// Full rotation period in milliseconds (guaranteed non-zero).
        duration_ms: NonZeroU32,
    },
}

/// Data for a frame-based animation. Fields are private; use accessor methods.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FramesData {
    frames: FrameList,
    frame_duration_ms: NonZeroU32,
}

impl FramesData {
    /// The animation frames (guaranteed non-empty).
    #[must_use]
    pub fn frames(&self) -> &FrameList {
        &self.frames
    }

    /// Duration of each frame in milliseconds (guaranteed non-zero).
    #[must_use]
    pub fn frame_duration_ms(&self) -> NonZeroU32 {
        self.frame_duration_ms
    }
}

/// Data for a transform-based animation. Fields are private; use accessor methods.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TransformData {
    icon: IconData,
    animation: TransformAnimation,
}

impl TransformData {
    /// The icon being animated.
    pub fn icon(&self) -> &IconData {
        &self.icon
    }

    /// The transform animation applied to the icon.
    #[must_use]
    pub fn animation(&self) -> &TransformAnimation {
        &self.animation
    }
}

/// An animated icon, either frame-based or transform-based.
///
/// `Frames` carries pre-rendered frames with uniform timing (loops infinitely).
/// `Transform` carries a single icon and a description of the motion.
///
/// Variant fields are private (via [`FramesData`] and [`TransformData`] wrapper
/// structs). Use the constructors [`AnimatedIcon::frames()`] and
/// [`AnimatedIcon::transform()`] to build instances, and accessor methods to
/// read fields.
///
/// # Examples
///
/// ```
/// use native_theme::theme::{AnimatedIcon, IconData, TransformAnimation};
/// use std::borrow::Cow;
/// use std::num::NonZeroU32;
///
/// // Frame-based animation (e.g., sprite sheet)
/// let frames_anim = AnimatedIcon::frames(
///     vec![
///         IconData::Svg(Cow::Borrowed(b"<svg>frame1</svg>")),
///         IconData::Svg(Cow::Borrowed(b"<svg>frame2</svg>")),
///     ],
///     NonZeroU32::new(83).unwrap(),
/// ).expect("non-empty frames");
///
/// // Transform-based animation (e.g., spinning icon)
/// let spin_anim = AnimatedIcon::transform(
///     IconData::Svg(Cow::Borrowed(b"<svg>spinner</svg>")),
///     TransformAnimation::Spin { duration_ms: NonZeroU32::new(1000).unwrap() },
/// );
///
/// // Pattern matching uses tuple variants + accessor methods
/// match &frames_anim {
///     AnimatedIcon::Frames(data) => {
///         assert_eq!(data.frames().len(), 2);
///         assert_eq!(data.frame_duration_ms().get(), 83);
///     }
///     AnimatedIcon::Transform(data) => {
///         let _icon = data.icon();
///         let _animation = data.animation();
///     }
///     _ => {}
/// }
///
/// // first_frame() returns &IconData (infallible, not Option)
/// let first = frames_anim.first_frame();
/// assert!(matches!(first, IconData::Svg(_)));
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum AnimatedIcon {
    /// A sequence of pre-rendered frames played at a fixed interval (loops infinitely).
    Frames(FramesData),
    /// A single icon with a continuous transform animation.
    Transform(TransformData),
}

impl AnimatedIcon {
    /// Create a frame-based animation.
    ///
    /// Returns `Err(EmptyFrameListError)` if `frames` is empty.
    /// `frame_duration_ms` uses [`NonZeroU32`] so zero duration is prevented at the type level.
    ///
    /// # Examples
    ///
    /// ```
    /// use native_theme::theme::{AnimatedIcon, IconData};
    /// use std::borrow::Cow;
    /// use std::num::NonZeroU32;
    ///
    /// let anim = AnimatedIcon::frames(
    ///     vec![IconData::Svg(Cow::Borrowed(b"<svg>f1</svg>"))],
    ///     NonZeroU32::new(83).unwrap(),
    /// );
    /// assert!(anim.is_ok());
    ///
    /// // Empty frames returns Err:
    /// let empty = AnimatedIcon::frames(vec![], NonZeroU32::new(83).unwrap());
    /// assert!(empty.is_err());
    /// ```
    pub fn frames(
        frames: Vec<IconData>,
        frame_duration_ms: NonZeroU32,
    ) -> Result<Self, EmptyFrameListError> {
        Ok(AnimatedIcon::Frames(FramesData {
            frames: FrameList::new(frames)?,
            frame_duration_ms,
        }))
    }

    /// Create a transform-based animation.
    ///
    /// # Examples
    ///
    /// ```
    /// use native_theme::theme::{AnimatedIcon, IconData, TransformAnimation};
    /// use std::borrow::Cow;
    /// use std::num::NonZeroU32;
    ///
    /// let anim = AnimatedIcon::transform(
    ///     IconData::Svg(Cow::Borrowed(b"<svg>spinner</svg>")),
    ///     TransformAnimation::Spin { duration_ms: NonZeroU32::new(1000).unwrap() },
    /// );
    /// ```
    #[must_use]
    pub fn transform(icon: IconData, animation: TransformAnimation) -> Self {
        AnimatedIcon::Transform(TransformData { icon, animation })
    }

    /// Return a reference to the first displayable frame (infallible).
    ///
    /// For `Frames`, returns the first element (guaranteed to exist via [`FrameList`]).
    /// For `Transform`, returns the underlying icon.
    ///
    /// # Examples
    ///
    /// ```
    /// use native_theme::theme::{AnimatedIcon, IconData, TransformAnimation};
    /// use std::borrow::Cow;
    /// use std::num::NonZeroU32;
    ///
    /// let anim = AnimatedIcon::frames(
    ///     vec![IconData::Svg(Cow::Borrowed(b"<svg>f1</svg>"))],
    ///     NonZeroU32::new(83).unwrap(),
    /// ).unwrap();
    /// let first: &IconData = anim.first_frame();
    /// assert!(matches!(first, IconData::Svg(_)));
    ///
    /// let spin = AnimatedIcon::transform(
    ///     IconData::Svg(Cow::Borrowed(b"<svg>spinner</svg>")),
    ///     TransformAnimation::Spin { duration_ms: NonZeroU32::new(1000).unwrap() },
    /// );
    /// let first: &IconData = spin.first_frame();
    /// assert!(matches!(first, IconData::Svg(_)));
    /// ```
    pub fn first_frame(&self) -> &IconData {
        match self {
            AnimatedIcon::Frames(data) => data.frames.first(),
            AnimatedIcon::Transform(data) => &data.icon,
        }
    }

    /// Return the frame list (for Frames variant) or None (for Transform).
    #[must_use]
    pub fn frame_list(&self) -> Option<&FrameList> {
        match self {
            AnimatedIcon::Frames(data) => Some(&data.frames),
            AnimatedIcon::Transform(_) => None,
        }
    }

    /// Return the frame duration in milliseconds (for Frames variant) or None.
    #[must_use]
    pub fn frame_duration_ms(&self) -> Option<NonZeroU32> {
        match self {
            AnimatedIcon::Frames(data) => Some(data.frame_duration_ms),
            AnimatedIcon::Transform(_) => None,
        }
    }

    /// Return the icon data (for Transform variant) or None.
    #[must_use]
    pub fn icon(&self) -> Option<&IconData> {
        match self {
            AnimatedIcon::Transform(data) => Some(&data.icon),
            AnimatedIcon::Frames(_) => None,
        }
    }

    /// Return the transform animation (for Transform variant) or None.
    #[must_use]
    pub fn animation(&self) -> Option<&TransformAnimation> {
        match self {
            AnimatedIcon::Transform(data) => Some(&data.animation),
            AnimatedIcon::Frames(_) => None,
        }
    }

    /// Deprecated: Use [`AnimatedIcon::frames()`] instead.
    #[deprecated(
        since = "0.5.7",
        note = "use AnimatedIcon::frames() which returns Result"
    )]
    #[must_use]
    pub fn new_frames(frames: Vec<IconData>, frame_duration_ms: u32) -> Option<Self> {
        let dur = NonZeroU32::new(frame_duration_ms)?;
        Self::frames(frames, dur).ok()
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use std::borrow::Cow;

    fn test_icon(label: &'static str) -> IconData {
        IconData::Svg(Cow::Owned(format!("<svg>{label}</svg>").into_bytes()))
    }

    fn test_icon_borrowed() -> IconData {
        IconData::Svg(Cow::Borrowed(b"<svg>test</svg>"))
    }

    fn nz(val: u32) -> NonZeroU32 {
        NonZeroU32::new(val).unwrap()
    }

    // === FrameList tests ===

    #[test]
    fn frame_list_new_with_one_frame() {
        let fl = FrameList::new(vec![test_icon("f1")]);
        assert!(fl.is_ok());
        assert_eq!(fl.unwrap().len(), 1);
    }

    #[test]
    fn frame_list_new_with_multiple_frames() {
        let fl = FrameList::new(vec![test_icon("f1"), test_icon("f2"), test_icon("f3")]).unwrap();
        assert_eq!(fl.len(), 3);
    }

    #[test]
    fn frame_list_new_rejects_empty() {
        let fl = FrameList::new(vec![]);
        assert!(fl.is_err());
        assert_eq!(fl.unwrap_err(), EmptyFrameListError);
    }

    #[test]
    fn frame_list_first_returns_first_frame() {
        let icon = test_icon("first");
        let fl = FrameList::new(vec![icon.clone(), test_icon("second")]).unwrap();
        assert_eq!(fl.first(), &icon);
    }

    #[test]
    fn frame_list_deref_to_slice() {
        let fl = FrameList::new(vec![test_icon("a"), test_icon("b")]).unwrap();
        let slice: &[IconData] = &fl;
        assert_eq!(slice.len(), 2);
        // Can iterate via Deref
        assert_eq!(fl.iter().count(), 2);
    }

    #[test]
    fn empty_frame_list_error_display() {
        let err = EmptyFrameListError;
        assert_eq!(err.to_string(), "frame list must not be empty");
    }

    #[test]
    fn empty_frame_list_error_is_std_error() {
        let err: Box<dyn std::error::Error> = Box::new(EmptyFrameListError);
        assert_eq!(err.to_string(), "frame list must not be empty");
    }

    // === FrameList serde tests ===

    #[test]
    fn frame_list_deserialize_rejects_empty_json() {
        let json = "[]";
        let result: Result<FrameList, _> = serde_json::from_str(json);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("frame list must not be empty"),
            "expected custom error message, got: {err}"
        );
    }

    #[test]
    fn frame_list_deserialize_non_empty_json_succeeds() {
        // IconData::Svg is serialized as {"Svg": [bytes...]}, so we need valid JSON
        let fl = FrameList::new(vec![test_icon_borrowed()]).unwrap();
        let json = serde_json::to_string(&fl).unwrap();
        let deserialized: FrameList = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.len(), 1);
    }

    #[test]
    fn frame_list_round_trips_through_json() {
        let fl = FrameList::new(vec![test_icon("a"), test_icon("b")]).unwrap();
        let json = serde_json::to_string(&fl).unwrap();
        let back: FrameList = serde_json::from_str(&json).unwrap();
        assert_eq!(fl, back);
    }

    // === AnimatedIcon construction tests ===

    #[test]
    fn frames_variant_constructs() {
        let icon = AnimatedIcon::frames(vec![test_icon("f1")], nz(83)).unwrap();
        assert!(matches!(icon, AnimatedIcon::Frames(_)));
        if let AnimatedIcon::Frames(data) = &icon {
            assert_eq!(data.frames().len(), 1);
            assert_eq!(data.frame_duration_ms().get(), 83);
        }
    }

    #[test]
    fn frames_constructor_rejects_empty() {
        let result = AnimatedIcon::frames(vec![], nz(83));
        assert!(result.is_err());
    }

    #[test]
    fn transform_variant_constructs() {
        let icon = AnimatedIcon::transform(
            test_icon("spinner"),
            TransformAnimation::Spin {
                duration_ms: nz(1000),
            },
        );
        assert!(matches!(icon, AnimatedIcon::Transform(_)));
        if let AnimatedIcon::Transform(data) = &icon {
            assert_eq!(
                *data.animation(),
                TransformAnimation::Spin {
                    duration_ms: nz(1000)
                }
            );
        }
    }

    #[test]
    #[allow(clippy::clone_on_copy)]
    fn transform_animation_is_copy_clone_debug_eq_hash() {
        let a = TransformAnimation::Spin {
            duration_ms: nz(500),
        };
        let a2 = a; // Copy
        let a3 = a.clone(); // Clone
        assert_eq!(a2, a3); // PartialEq + Eq
        use std::hash::Hash;
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        a.hash(&mut hasher);
        let _ = format!("{a:?}"); // Debug
    }

    #[test]
    fn animated_icon_is_clone_debug_eq_not_copy() {
        let icon = AnimatedIcon::frames(vec![test_icon("f1")], nz(100)).unwrap();
        let cloned = icon.clone(); // Clone
        assert_eq!(icon, cloned); // PartialEq + Eq
        let _ = format!("{icon:?}"); // Debug
    }

    // === first_frame() tests ===

    #[test]
    fn first_frame_frames_with_items() {
        let f0 = test_icon("frame0");
        let icon = AnimatedIcon::frames(vec![f0.clone(), test_icon("frame1")], nz(100)).unwrap();
        // first_frame() returns &IconData, not Option
        assert_eq!(icon.first_frame(), &f0);
    }

    #[test]
    fn first_frame_transform() {
        let data = test_icon("spin");
        let icon = AnimatedIcon::transform(
            data.clone(),
            TransformAnimation::Spin {
                duration_ms: nz(1000),
            },
        );
        assert_eq!(icon.first_frame(), &data);
    }

    // === Accessor method tests ===

    #[test]
    fn frame_list_accessor_returns_some_for_frames() {
        let icon = AnimatedIcon::frames(vec![test_icon("f1")], nz(83)).unwrap();
        assert!(icon.frame_list().is_some());
        assert_eq!(icon.frame_list().unwrap().len(), 1);
    }

    #[test]
    fn frame_list_accessor_returns_none_for_transform() {
        let icon = AnimatedIcon::transform(
            test_icon("spin"),
            TransformAnimation::Spin {
                duration_ms: nz(1000),
            },
        );
        assert!(icon.frame_list().is_none());
    }

    #[test]
    fn frame_duration_ms_accessor() {
        let icon = AnimatedIcon::frames(vec![test_icon("f1")], nz(42)).unwrap();
        assert_eq!(icon.frame_duration_ms().unwrap().get(), 42);
    }

    #[test]
    fn icon_accessor_returns_some_for_transform() {
        let data = test_icon("spin");
        let icon = AnimatedIcon::transform(
            data.clone(),
            TransformAnimation::Spin {
                duration_ms: nz(1000),
            },
        );
        assert_eq!(icon.icon(), Some(&data));
    }

    #[test]
    fn icon_accessor_returns_none_for_frames() {
        let icon = AnimatedIcon::frames(vec![test_icon("f1")], nz(83)).unwrap();
        assert!(icon.icon().is_none());
    }

    #[test]
    fn animation_accessor_returns_some_for_transform() {
        let anim = TransformAnimation::Spin {
            duration_ms: nz(1000),
        };
        let icon = AnimatedIcon::transform(test_icon("spin"), anim);
        assert_eq!(icon.animation(), Some(&anim));
    }

    #[test]
    fn animation_accessor_returns_none_for_frames() {
        let icon = AnimatedIcon::frames(vec![test_icon("f1")], nz(83)).unwrap();
        assert!(icon.animation().is_none());
    }

    // === Deprecated new_frames() compatibility tests ===

    #[test]
    #[allow(deprecated)]
    fn new_frames_valid() {
        let anim = AnimatedIcon::new_frames(vec![test_icon("f1")], 83);
        assert!(anim.is_some());
    }

    #[test]
    #[allow(deprecated)]
    fn new_frames_rejects_empty() {
        assert!(AnimatedIcon::new_frames(vec![], 83).is_none());
    }

    #[test]
    #[allow(deprecated)]
    fn new_frames_rejects_zero_duration() {
        let frames = vec![test_icon("f1")];
        assert!(AnimatedIcon::new_frames(frames, 0).is_none());
    }

    // === Serde round-trip tests ===

    #[test]
    fn animated_icon_frames_round_trips_json() {
        let icon = AnimatedIcon::frames(vec![test_icon("f1"), test_icon("f2")], nz(83)).unwrap();
        let json = serde_json::to_string(&icon).unwrap();
        let back: AnimatedIcon = serde_json::from_str(&json).unwrap();
        assert_eq!(icon, back);
    }

    #[test]
    fn animated_icon_transform_round_trips_json() {
        let icon = AnimatedIcon::transform(
            test_icon("spinner"),
            TransformAnimation::Spin {
                duration_ms: nz(1000),
            },
        );
        let json = serde_json::to_string(&icon).unwrap();
        let back: AnimatedIcon = serde_json::from_str(&json).unwrap();
        assert_eq!(icon, back);
    }

    #[test]
    fn transform_animation_spin_uses_non_zero_u32() {
        // NonZeroU32::new(0) returns None, so zero is impossible
        assert!(NonZeroU32::new(0).is_none());
        // Valid duration works
        let anim = TransformAnimation::Spin {
            duration_ms: nz(500),
        };
        let TransformAnimation::Spin { duration_ms } = anim;
        assert_eq!(duration_ms.get(), 500);
    }

    // === FramesData and TransformData accessor tests ===

    #[test]
    fn frames_data_accessors() {
        let icon = AnimatedIcon::frames(vec![test_icon("f1")], nz(83)).unwrap();
        if let AnimatedIcon::Frames(data) = &icon {
            assert_eq!(data.frames().len(), 1);
            assert_eq!(data.frame_duration_ms().get(), 83);
        } else {
            panic!("expected Frames variant");
        }
    }

    #[test]
    fn transform_data_accessors() {
        let icon_data = test_icon("spin");
        let anim = TransformAnimation::Spin {
            duration_ms: nz(1000),
        };
        let icon = AnimatedIcon::transform(icon_data.clone(), anim);
        if let AnimatedIcon::Transform(data) = &icon {
            assert_eq!(data.icon(), &icon_data);
            assert_eq!(*data.animation(), anim);
        } else {
            panic!("expected Transform variant");
        }
    }
}