token-value-map 0.2.5

A token-value map with interpolation of values: what you need for DCCs
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
//! Macro for defining custom data type systems.
//!
//! This module provides the [`define_data_types!`] macro which generates
//! all the boilerplate needed to create a custom data system compatible
//! with [`GenericValue`](crate::GenericValue) and
//! [`GenericTokenValueMap`](crate::GenericTokenValueMap).

/// Define a custom data type system with full interpolation support.
///
/// This macro generates three enums and implements all necessary traits
/// for use with [`GenericValue`](crate::GenericValue) and
/// [`GenericTokenValueMap`](crate::GenericTokenValueMap):
///
/// 1. A discriminant enum (like `DataType`) with unit variants.
/// 2. A data enum (like `Data`) holding actual values.
/// 3. An animated data enum (like `AnimatedData`) holding `TimeDataMap<T>` values.
///
/// # Example
///
/// ```rust,ignore
/// use token_value_map::{define_data_types, TimeDataMap, Time, DataSystem};
///
/// define_data_types! {
///     /// My custom data types.
///     #[derive(Clone, Debug, PartialEq)]
///     pub MyData / MyAnimatedData / MyDataType {
///         /// A floating point value.
///         Float(MyFloat),
///         /// An integer value.
///         Int(MyInt),
///     }
/// }
///
/// // Now you can use GenericValue<MyData> and GenericTokenValueMap<MyData>.
/// use token_value_map::GenericValue;
///
/// let uniform = GenericValue::<MyData>::uniform(MyData::Float(MyFloat(42.0)));
/// let animated = GenericValue::<MyData>::animated(vec![
///     (Time::default(), MyData::Float(MyFloat(0.0))),
///     (Time::from(10.0), MyData::Float(MyFloat(100.0))),
/// ]).unwrap();
/// ```
///
/// # Generated Types
///
/// Given `MyData / MyAnimatedData / MyDataType`:
///
/// - `MyDataType`: Discriminant enum with unit variants (`Float`, `Int`, `Text`).
/// - `MyData`: Data enum holding values (`Float(f32)`, `Int(i32)`, `Text(String)`).
/// - `MyAnimatedData`: Animated enum holding time maps
///   (`Float(TimeDataMap<f32>)`, etc.).
///
/// # Requirements
///
/// Each variant type must implement:
/// - `Clone + Debug + PartialEq + Eq + Hash + Send + Sync + 'static`
///
/// For interpolation support, types should also implement:
/// - `Add<Output = Self> + Sub<Output = Self>`
/// - `Mul<f32, Output = Self> + Mul<f64, Output = Self>`
/// - `Div<f32, Output = Self> + Div<f64, Output = Self>`
///
/// Types that don't support interpolation will use sample-and-hold behavior.
#[macro_export]
macro_rules! define_data_types {
    (
        $(#[$meta:meta])*
        $vis:vis $data_name:ident / $animated_name:ident / $discriminant_name:ident {
            $(
                $(#[$variant_meta:meta])*
                $variant:ident($inner_ty:ty)
            ),+ $(,)?
        }
    ) => {
        // Generate the discriminant enum.
        $(#[$meta])*
        #[derive(Copy, Eq, Hash)]
        $vis enum $discriminant_name {
            $(
                $(#[$variant_meta])*
                $variant,
            )+
        }

        // Generate the data enum.
        $(#[$meta])*
        #[derive(Eq, Hash)]
        $vis enum $data_name {
            $(
                $(#[$variant_meta])*
                $variant($inner_ty),
            )+
        }

        // Generate the animated data enum.
        $(#[$meta])*
        #[derive(Eq, Hash)]
        $vis enum $animated_name {
            $(
                $(#[$variant_meta])*
                $variant($crate::TimeDataMap<$inner_ty>),
            )+
        }

        // Implement DataSystem for the data enum.
        impl $crate::DataSystem for $data_name {
            type Animated = $animated_name;
            type DataType = $discriminant_name;

            fn discriminant(&self) -> Self::DataType {
                match self {
                    $(
                        $data_name::$variant(_) => $discriminant_name::$variant,
                    )+
                }
            }

            fn variant_name(&self) -> &'static str {
                match self {
                    $(
                        $data_name::$variant(_) => stringify!($variant),
                    )+
                }
            }
        }

        // Implement AnimatedDataSystem for the animated data enum.
        impl $crate::AnimatedDataSystem for $animated_name {
            type Data = $data_name;

            fn keyframe_count(&self) -> usize {
                match self {
                    $(
                        $animated_name::$variant(map) => map.len(),
                    )+
                }
            }

            fn times(&self) -> ::smallvec::SmallVec<[$crate::Time; 10]> {
                match self {
                    $(
                        $animated_name::$variant(map) => {
                            map.iter().map(|(t, _)| *t).collect()
                        }
                    )+
                }
            }

            fn interpolate(&self, time: $crate::Time) -> Self::Data {
                match self {
                    $(
                        $animated_name::$variant(map) => {
                            $data_name::$variant(map.interpolate(time))
                        }
                    )+
                }
            }

            fn sample_at(&self, time: $crate::Time) -> ::core::option::Option<Self::Data> {
                match self {
                    $(
                        $animated_name::$variant(map) => {
                            map.get(&time).cloned().map($data_name::$variant)
                        }
                    )+
                }
            }

            fn try_insert(
                &mut self,
                time: $crate::Time,
                value: Self::Data,
            ) -> $crate::Result<()> {
                match (self, value) {
                    $(
                        ($animated_name::$variant(map), $data_name::$variant(v)) => {
                            map.insert(time, v);
                            Ok(())
                        }
                    )+
                    #[allow(unreachable_patterns)]
                    (this, val) => Err($crate::Error::GenericTypeMismatch {
                        expected: this.variant_name(),
                        got: val.variant_name(),
                    }),
                }
            }

            fn remove_at(&mut self, time: &$crate::Time) -> ::core::option::Option<Self::Data> {
                match self {
                    $(
                        $animated_name::$variant(map) => {
                            map.remove(time).ok()?.map($data_name::$variant)
                        }
                    )+
                }
            }

            fn discriminant(&self) -> <Self::Data as $crate::DataSystem>::DataType {
                match self {
                    $(
                        $animated_name::$variant(_) => $discriminant_name::$variant,
                    )+
                }
            }

            fn from_single(time: $crate::Time, value: Self::Data) -> Self {
                match value {
                    $(
                        $data_name::$variant(v) => {
                            $animated_name::$variant($crate::KeyDataMap::from_single(time, v))
                        }
                    )+
                }
            }

            fn variant_name(&self) -> &'static str {
                match self {
                    $(
                        $animated_name::$variant(_) => stringify!($variant),
                    )+
                }
            }
        }

        // Implement From conversions from inner types to Data.
        $(
            impl ::core::convert::From<$inner_ty> for $data_name {
                fn from(value: $inner_ty) -> Self {
                    $data_name::$variant(value)
                }
            }
        )+
    };
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{AnimatedDataSystem, DataSystem, GenericValue, Time};
    use std::ops::{Add, Div, Mul, Sub};

    // Wrapper type that implements all required traits for interpolation.
    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
    struct TestFloat(i64); // Store as fixed-point for Eq/Hash.

    impl TestFloat {
        fn new(v: f32) -> Self {
            Self((v * 1000.0) as i64)
        }

        fn value(&self) -> f32 {
            self.0 as f32 / 1000.0
        }
    }

    impl Add for TestFloat {
        type Output = Self;
        fn add(self, other: Self) -> Self {
            Self(self.0 + other.0)
        }
    }

    impl Sub for TestFloat {
        type Output = Self;
        fn sub(self, other: Self) -> Self {
            Self(self.0 - other.0)
        }
    }

    impl Mul<f32> for TestFloat {
        type Output = Self;
        fn mul(self, scalar: f32) -> Self {
            Self((self.0 as f32 * scalar) as i64)
        }
    }

    impl Mul<f64> for TestFloat {
        type Output = Self;
        fn mul(self, scalar: f64) -> Self {
            Self((self.0 as f64 * scalar) as i64)
        }
    }

    impl Div<f32> for TestFloat {
        type Output = Self;
        fn div(self, scalar: f32) -> Self {
            Self((self.0 as f32 / scalar) as i64)
        }
    }

    impl Div<f64> for TestFloat {
        type Output = Self;
        fn div(self, scalar: f64) -> Self {
            Self((self.0 as f64 / scalar) as i64)
        }
    }

    // Integer wrapper that supports interpolation via f32 multiplication.
    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
    struct TestInt(i64);

    impl Add for TestInt {
        type Output = Self;
        fn add(self, other: Self) -> Self {
            Self(self.0 + other.0)
        }
    }

    impl Sub for TestInt {
        type Output = Self;
        fn sub(self, other: Self) -> Self {
            Self(self.0 - other.0)
        }
    }

    impl Mul<f32> for TestInt {
        type Output = Self;
        fn mul(self, scalar: f32) -> Self {
            Self((self.0 as f32 * scalar) as i64)
        }
    }

    impl Mul<f64> for TestInt {
        type Output = Self;
        fn mul(self, scalar: f64) -> Self {
            Self((self.0 as f64 * scalar) as i64)
        }
    }

    impl Div<f32> for TestInt {
        type Output = Self;
        fn div(self, scalar: f32) -> Self {
            Self((self.0 as f32 / scalar) as i64)
        }
    }

    impl Div<f64> for TestInt {
        type Output = Self;
        fn div(self, scalar: f64) -> Self {
            Self((self.0 as f64 / scalar) as i64)
        }
    }

    // Define a simple custom data system for testing.
    define_data_types! {
        /// Test data types.
        #[derive(Clone, Debug, PartialEq)]
        pub TestData / TestAnimatedData / TestDataType {
            /// A float value.
            Float(TestFloat),
            /// An int value.
            Int(TestInt),
        }
    }

    #[test]
    fn test_discriminant() {
        let data = TestData::Float(TestFloat::new(42.0));
        assert_eq!(data.discriminant(), TestDataType::Float);
        assert_eq!(data.variant_name(), "Float");

        let data = TestData::Int(TestInt(42));
        assert_eq!(data.discriminant(), TestDataType::Int);
        assert_eq!(data.variant_name(), "Int");
    }

    #[test]
    fn test_from_conversion() {
        let data: TestData = TestFloat::new(42.0).into();
        assert!(matches!(data, TestData::Float(_)));

        let data: TestData = TestInt(42).into();
        assert!(matches!(data, TestData::Int(TestInt(42))));
    }

    #[test]
    fn test_generic_value_uniform() {
        let value = GenericValue::<TestData>::uniform(TestData::Float(TestFloat::new(42.0)));
        assert!(!value.is_animated());

        if let TestData::Float(f) = value.interpolate(Time::default()) {
            assert!((f.value() - 42.0).abs() < 0.01);
        } else {
            panic!("Expected Float variant");
        }
    }

    #[test]
    fn test_generic_value_animated() {
        let value = GenericValue::<TestData>::animated(vec![
            (Time::default(), TestData::Float(TestFloat::new(0.0))),
            (Time::from(10.0), TestData::Float(TestFloat::new(100.0))),
        ])
        .unwrap();

        assert!(value.is_animated());
        assert_eq!(value.sample_count(), 2);

        // Test interpolation at midpoint.
        let mid = value.interpolate(Time::from(5.0));
        if let TestData::Float(v) = mid {
            assert!((v.value() - 50.0).abs() < 1.0); // Fixed-point has some precision loss.
        } else {
            panic!("Expected Float variant");
        }
    }

    #[test]
    fn test_animated_data_system() {
        let animated =
            TestAnimatedData::from_single(Time::default(), TestData::Float(TestFloat::new(42.0)));
        assert_eq!(animated.keyframe_count(), 1);
        assert_eq!(animated.variant_name(), "Float");

        let sample = animated.sample_at(Time::default());
        assert!(matches!(sample, Some(TestData::Float(_))));
    }
}