datamodel 0.10.0

A rust library to serialize and deserialize Valve's proprietary DMX file format
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
use std::mem::transmute;

use crate::element::{Element, ElementClass};
pub use uuid::Uuid as UUID;

/// A structure that holds raw binary data.
#[derive(Debug, Clone, Default)]
pub struct BinaryBlock(pub Vec<u8>);

/// A representation of time in tenths of a millisecond.
#[derive(Debug, Clone, Copy, Default)]
pub struct Time(pub i32);

impl Time {
    pub fn from_seconds(seconds: f32) -> Option<Self> {
        let tenths_of_milliseconds = (seconds * 10000.0 + 0.5).floor();
        if tenths_of_milliseconds > i32::MAX as f32 || tenths_of_milliseconds < i32::MIN as f32 {
            return None;
        }
        Some(Time(tenths_of_milliseconds as i32))
    }

    pub fn as_seconds(&self) -> f32 {
        self.0 as f32 / 10000.0
    }
}

/// A structure that 8 bit RGBA color.
#[derive(Debug, Clone, Copy, Default)]
pub struct Color {
    pub red: u8,
    pub green: u8,
    pub blue: u8,
    pub alpha: u8,
}

/// A mathematical 2 dimensional vector.
#[derive(Debug, Clone, Copy, Default)]
pub struct Vector2 {
    pub x: f32,
    pub y: f32,
}

#[cfg(feature = "mint")]
impl From<mint::Point2<f32>> for Vector2 {
    fn from(v: mint::Point2<f32>) -> Self {
        Self { x: v.x, y: v.y }
    }
}

#[cfg(feature = "mint")]
impl From<Vector2> for mint::Point2<f32> {
    fn from(v: Vector2) -> Self {
        Self { x: v.x, y: v.y }
    }
}

#[cfg(feature = "mint")]
impl From<mint::Vector2<f32>> for Vector2 {
    fn from(v: mint::Vector2<f32>) -> Self {
        Self { x: v.x, y: v.y }
    }
}

#[cfg(feature = "mint")]
impl From<Vector2> for mint::Vector2<f32> {
    fn from(v: Vector2) -> Self {
        Self { x: v.x, y: v.y }
    }
}

#[cfg(feature = "mint")]
impl mint::IntoMint for Vector2 {
    type MintType = mint::Vector2<f32>;
}

/// A mathematical 3 dimensional vector.
#[derive(Debug, Clone, Copy, Default)]
pub struct Vector3 {
    pub x: f32,
    pub y: f32,
    pub z: f32,
}

#[cfg(feature = "mint")]
impl From<mint::Point3<f32>> for Vector3 {
    fn from(v: mint::Point3<f32>) -> Self {
        Self { x: v.x, y: v.y, z: v.z }
    }
}

#[cfg(feature = "mint")]
impl From<Vector3> for mint::Point3<f32> {
    fn from(v: Vector3) -> Self {
        Self { x: v.x, y: v.y, z: v.z }
    }
}

#[cfg(feature = "mint")]
impl From<mint::Vector3<f32>> for Vector3 {
    fn from(v: mint::Vector3<f32>) -> Self {
        Self { x: v.x, y: v.y, z: v.z }
    }
}

#[cfg(feature = "mint")]
impl From<Vector3> for mint::Vector3<f32> {
    fn from(v: Vector3) -> Self {
        Self { x: v.x, y: v.y, z: v.z }
    }
}

#[cfg(feature = "mint")]
impl mint::IntoMint for Vector3 {
    type MintType = mint::Vector3<f32>;
}

/// A mathematical 4 dimensional vector.
#[derive(Debug, Clone, Copy, Default)]
pub struct Vector4 {
    pub x: f32,
    pub y: f32,
    pub z: f32,
    pub w: f32,
}

#[cfg(feature = "mint")]
impl From<mint::Vector4<f32>> for Vector4 {
    fn from(v: mint::Vector4<f32>) -> Self {
        Self {
            x: v.x,
            y: v.y,
            z: v.z,
            w: v.w,
        }
    }
}

#[cfg(feature = "mint")]
impl From<Vector4> for mint::Vector4<f32> {
    fn from(v: Vector4) -> Self {
        Self {
            x: v.x,
            y: v.y,
            z: v.z,
            w: v.w,
        }
    }
}

#[cfg(feature = "mint")]
impl mint::IntoMint for Vector4 {
    type MintType = mint::Vector4<f32>;
}

/// A Tait-Bryan 3 dimensional angle.
#[derive(Debug, Clone, Copy, Default)]
pub struct Angle {
    pub pitch: f32,
    pub yaw: f32,
    pub roll: f32,
}

#[cfg(feature = "mint")]
impl From<mint::EulerAngles<f32, mint::IntraXYZ>> for Angle {
    fn from(value: mint::EulerAngles<f32, mint::IntraXYZ>) -> Self {
        Self {
            pitch: value.b.to_degrees(),
            yaw: value.c.to_degrees(),
            roll: value.a.to_degrees(),
        }
    }
}

#[cfg(feature = "mint")]
impl From<Angle> for mint::EulerAngles<f32, mint::IntraXYZ> {
    fn from(value: Angle) -> Self {
        Self {
            a: value.roll.to_radians(),
            b: value.pitch.to_radians(),
            c: value.yaw.to_radians(),
            marker: std::marker::PhantomData,
        }
    }
}

#[cfg(feature = "mint")]
impl mint::IntoMint for Angle {
    type MintType = mint::EulerAngles<f32, mint::IntraXYZ>;
}

/// A mathematical Quaternion.
#[derive(Debug, Clone, Copy)]
pub struct Quaternion {
    pub x: f32,
    pub y: f32,
    pub z: f32,
    pub w: f32,
}

impl Default for Quaternion {
    fn default() -> Self {
        Self {
            x: 0.0,
            y: 0.0,
            z: 0.0,
            w: 1.0,
        }
    }
}

#[cfg(feature = "mint")]
impl From<mint::Quaternion<f32>> for Quaternion {
    fn from(v: mint::Quaternion<f32>) -> Self {
        Self {
            x: v.v.x,
            y: v.v.y,
            z: v.v.z,
            w: v.s,
        }
    }
}

#[cfg(feature = "mint")]
impl From<Quaternion> for mint::Quaternion<f32> {
    fn from(v: Quaternion) -> Self {
        Self {
            v: mint::Vector3 { x: v.x, y: v.y, z: v.z },
            s: v.w,
        }
    }
}

#[cfg(feature = "mint")]
impl mint::IntoMint for Quaternion {
    type MintType = mint::Quaternion<f32>;
}

/// A mathematical 4 by 4 matrix.
#[derive(Debug, Clone, Copy)]
pub struct Matrix(pub [[f32; 4]; 4]);

impl Default for Matrix {
    fn default() -> Self {
        Self([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]])
    }
}

#[cfg(feature = "mint")]
impl From<mint::RowMatrix4<f32>> for Matrix {
    fn from(value: mint::RowMatrix4<f32>) -> Self {
        Self([
            [value.x.x, value.x.y, value.x.z, value.x.w],
            [value.y.x, value.y.y, value.y.z, value.y.w],
            [value.z.x, value.z.y, value.z.z, value.z.w],
            [value.w.x, value.w.y, value.w.z, value.w.w],
        ])
    }
}

#[cfg(feature = "mint")]
impl From<Matrix> for mint::RowMatrix4<f32> {
    fn from(value: Matrix) -> Self {
        Self {
            x: mint::Vector4 {
                x: value.0[0][0],
                y: value.0[0][1],
                z: value.0[0][2],
                w: value.0[0][3],
            },
            y: mint::Vector4 {
                x: value.0[1][0],
                y: value.0[1][1],
                z: value.0[1][2],
                w: value.0[1][3],
            },
            z: mint::Vector4 {
                x: value.0[2][0],
                y: value.0[2][1],
                z: value.0[2][2],
                w: value.0[2][3],
            },
            w: mint::Vector4 {
                x: value.0[3][0],
                y: value.0[3][1],
                z: value.0[3][2],
                w: value.0[3][3],
            },
        }
    }
}

#[cfg(feature = "mint")]
impl From<mint::ColumnMatrix4<f32>> for Matrix {
    fn from(value: mint::ColumnMatrix4<f32>) -> Self {
        Self([
            [value.x.x, value.y.x, value.z.x, value.w.x],
            [value.x.y, value.y.y, value.z.y, value.w.y],
            [value.x.z, value.y.z, value.z.z, value.w.z],
            [value.x.w, value.y.w, value.z.w, value.w.w],
        ])
    }
}

#[cfg(feature = "mint")]
impl From<Matrix> for mint::ColumnMatrix4<f32> {
    fn from(value: Matrix) -> Self {
        Self {
            x: mint::Vector4 {
                x: value.0[0][0],
                y: value.0[1][0],
                z: value.0[2][0],
                w: value.0[3][0],
            },
            y: mint::Vector4 {
                x: value.0[0][1],
                y: value.0[1][1],
                z: value.0[2][1],
                w: value.0[3][1],
            },
            z: mint::Vector4 {
                x: value.0[0][2],
                y: value.0[1][2],
                z: value.0[2][2],
                w: value.0[3][2],
            },
            w: mint::Vector4 {
                x: value.0[0][3],
                y: value.0[1][3],
                z: value.0[2][3],
                w: value.0[3][3],
            },
        }
    }
}

#[cfg(feature = "mint")]
impl mint::IntoMint for Matrix {
    type MintType = mint::RowMatrix4<f32>;
}

macro_rules! declare_attributes {
    ($($name:ident : $value:ty),* $(,)?) => {
        paste::paste! {
            /// A value to specify what type the attribute is.
            #[derive(Clone, Copy, Debug, Eq, PartialEq)]
            pub enum AttributeType {
                $($name,)*
                $([<$name Array>],)*
            }

            /// Possible values which the attribute will store.
            #[derive(Clone, Debug)]
            pub enum AttributeValue {
                $($name($value),)*
                $([<$name Array>](Vec<$value>),)*
            }

            impl AttributeValue {
                pub fn attribute_type(&self) -> AttributeType {
                    match self {
                        $(AttributeValue::$name(_) => AttributeType::$name,)*
                        $(AttributeValue::[<$name Array>](_) => AttributeType::[<$name Array>],)*
                    }
                }
            }
        }
    };
}

declare_attributes! {
    Element: Option<Element>,
    Integer: i32,
    Float: f32,
    Boolean: bool,
    String: String,
    Binary: BinaryBlock,
    ObjectId: UUID,
    Time: Time,
    Color: Color,
    Vector2: Vector2,
    Vector3: Vector3,
    Vector4: Vector4,
    Angle: Angle,
    Quaternion: Quaternion,
    Matrix: Matrix,
    ULong: u64,
    UByte: u8,
}

#[derive(Clone, Debug)]
pub struct Attribute(AttributeValue);

impl Attribute {
    pub fn new(value: AttributeValue) -> Self {
        Self(value)
    }

    pub fn get_type(&self) -> AttributeType {
        self.0.attribute_type()
    }

    pub fn get_inner_value(&self) -> &AttributeValue {
        &self.0
    }

    pub fn get_inner_value_mut(&mut self) -> &mut AttributeValue {
        &mut self.0
    }
}

/// A trait to implement a type that stores as a attribute value.
pub trait AttributeInfo: Clone + Default {
    /// Returns the attribute type the value stores.
    fn attribute_type() -> AttributeType;
    /// Converts the value into a attribute value.
    fn into_attribute_type(self) -> AttributeValue;
    /// Converts the value into an attribute.
    fn into_attribute(self) -> Attribute {
        Attribute::new(self.into_attribute_type())
    }
    /// Gets the inner values of a attribute if its the correct attribute type.
    fn get_attribute_value(attribute: &AttributeValue) -> Option<&Self>;
    /// Gets the inner values as mutably of a attribute if its the correct attribute type.
    fn get_attribute_value_mut(attribute: &mut AttributeValue) -> Option<&mut Self>;
}

impl<Class: ElementClass> AttributeInfo for Element<Class> {
    fn attribute_type() -> AttributeType {
        AttributeType::Element
    }

    fn into_attribute_type(self) -> AttributeValue {
        AttributeValue::Element(Some(self.convert()))
    }

    fn get_attribute_value(attribute: &AttributeValue) -> Option<&Self> {
        match attribute {
            AttributeValue::Element(element) => {
                if let Some(element) = element {
                    Some(element.cast())
                } else {
                    None
                }
            }
            _ => None,
        }
    }

    fn get_attribute_value_mut(attribute: &mut AttributeValue) -> Option<&mut Self> {
        match attribute {
            AttributeValue::Element(element) => {
                if let Some(element) = element {
                    // SAFETY: The size of the Element does not change.
                    Some(unsafe { std::mem::transmute::<&mut Element, &mut Element<Class>>(element) })
                } else {
                    None
                }
            }
            _ => None,
        }
    }
}

impl<Class: ElementClass> AttributeInfo for Option<Element<Class>> {
    fn attribute_type() -> AttributeType {
        AttributeType::Element
    }

    fn into_attribute_type(self) -> AttributeValue {
        AttributeValue::Element(self.map(|element| element.convert()))
    }

    fn get_attribute_value(attribute: &AttributeValue) -> Option<&Self> {
        match attribute {
            // SAFETY: The size of the Element does not change.
            AttributeValue::Element(element) => Some(unsafe { transmute::<&Option<Element>, &Option<Element<Class>>>(element) }),
            _ => None,
        }
    }

    fn get_attribute_value_mut(attribute: &mut AttributeValue) -> Option<&mut Self> {
        match attribute {
            // SAFETY: The size of the Element does not change.
            AttributeValue::Element(element) => Some(unsafe { transmute::<&mut Option<Element>, &mut Option<Element<Class>>>(element) }),
            _ => None,
        }
    }
}

impl<Class: ElementClass> AttributeInfo for Vec<Option<Element<Class>>> {
    fn attribute_type() -> AttributeType {
        AttributeType::ElementArray
    }

    fn into_attribute_type(self) -> AttributeValue {
        AttributeValue::ElementArray(unsafe { transmute::<Vec<Option<Element<Class>>>, Vec<Option<Element>>>(self) })
    }

    fn get_attribute_value(attribute: &AttributeValue) -> Option<&Self> {
        match attribute {
            // SAFETY: The size of the Element does not change.
            AttributeValue::ElementArray(element) => Some(unsafe { transmute::<&Vec<Option<Element>>, &Vec<Option<Element<Class>>>>(element) }),
            _ => None,
        }
    }

    fn get_attribute_value_mut(attribute: &mut AttributeValue) -> Option<&mut Self> {
        match attribute {
            // SAFETY: The size of the Element does not change.
            AttributeValue::ElementArray(element) => Some(unsafe { transmute::<&mut Vec<Option<Element>>, &mut Vec<Option<Element<Class>>>>(element) }),
            _ => None,
        }
    }
}

macro_rules! define_attributes_info {
    ($($name:ident : $value:ty),* $(,)?) => {
        paste::paste! {
            $(
                impl AttributeInfo for $value {
                    fn attribute_type() -> AttributeType {
                        AttributeType::$name
                    }
                    fn into_attribute_type(self) -> AttributeValue {
                        AttributeValue::$name(self)
                    }
                    fn get_attribute_value(attribute: &AttributeValue) -> Option<&Self> {
                        match attribute {
                            AttributeValue::$name(inner_value) => Some(inner_value),
                            _ => None
                        }
                    }
                    fn get_attribute_value_mut(attribute: &mut AttributeValue) -> Option<&mut Self> {
                        match attribute {
                            AttributeValue::$name(inner_value) => Some(inner_value),
                            _ => None
                        }
                    }
                }
                impl AttributeInfo for Vec<$value> {
                    fn attribute_type() -> AttributeType {
                        AttributeType::[<$name Array>]
                    }
                    fn into_attribute_type(self) -> AttributeValue {
                        AttributeValue::[<$name Array>](self)
                    }
                    fn get_attribute_value(attribute: &AttributeValue) -> Option<&Self> {
                        match attribute {
                            AttributeValue::[<$name Array>](inner_value) => Some(inner_value),
                            _ => None
                        }
                    }
                    fn get_attribute_value_mut(attribute: &mut AttributeValue) -> Option<&mut Self> {
                        match attribute {
                            AttributeValue::[<$name Array>](inner_value) => Some(inner_value),
                            _ => None
                        }
                    }
                }
            )*
        }
    };
}

define_attributes_info! {
    Integer: i32,
    Float: f32,
    Boolean: bool,
    String: String,
    Binary: BinaryBlock,
    ObjectId: UUID,
    Time: Time,
    Color: Color,
    Vector2: Vector2,
    Vector3: Vector3,
    Vector4: Vector4,
    Angle: Angle,
    Quaternion: Quaternion,
    Matrix: Matrix,
    ULong: u64,
    UByte: u8,
}