gdtf 0.2.0

Tools to read and inspect General Device Type Format (GDTF) files.
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
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
//! Describes all DMX modes of a device.

use crate::attribute::{Attribute, SubPhysicalUnit};
use crate::description::util::IterUtil;
use crate::fixture_type::FixtureType;
use crate::geometry::Geometry;
use crate::physical_descriptions::{ColorSpace, DmxProfile, Emitter, Filter, Gamut};
use crate::validation::{ValidationError, ValidationErrorType, ValidationObject, ValidationResult};
use crate::values::{non_empty_string, DmxValue, Name, Node, NodeExt};
use crate::wheel::{Wheel, WheelSlot};
use serde::de::value::StrDeserializer;
use serde::de::{Error, Unexpected, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt::Formatter;

/// Describes the logical control of the device in a specific mode.
///
/// If a device firmware revision changes a DMX footprint, then such revisions are specified as a
/// new DMX mode.
///
/// Corresponds to a `<DMXMode>` XML node.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DmxMode {
    /// The unique name of the DMX mode.
    ///
    /// Corresponds to the `Name` XML attribute.
    #[serde(rename = "@Name", skip_serializing_if = "Option::is_none")]
    pub name: Option<Name>,

    /// Description of the DMX mode.
    ///
    /// Corresponds to the `Description` XML attribute.
    #[serde(rename = "@Description", default)]
    pub description: String,

    /// Name of the first geometry in the device.
    ///
    /// Only top level geometries are allowed to be linked.
    ///
    /// Corresponds to the `Geometry` XML attribute.
    #[serde(rename = "@Geometry")]
    pub geometry: Option<Name>,

    /// Description of all DMX channels used in this mode.
    ///
    /// Corresponds to the `<DMXChannels>` XML child node.
    #[serde(
        rename = "DMXChannels",
        skip_serializing_if = "Vec::is_empty",
        serialize_with = "serialize_dmx_channels",
        deserialize_with = "deserialize_dmx_channels"
    )]
    pub dmx_channels: Vec<DmxChannel>,

    /// Description of relations between channels.
    ///
    /// Corresponds to the `<Relations>` XML child node.
    #[serde(
        rename = "Relations",
        skip_serializing_if = "Vec::is_empty",
        serialize_with = "serialize_relations",
        deserialize_with = "deserialize_relations",
        default
    )]
    pub relations: Vec<Relation>,

    /// Used to describe macros of the manufacturer.
    ///
    /// Corresponds to the `<FTMacros>` XML child node.
    #[serde(
        rename = "FTMacros",
        skip_serializing_if = "Vec::is_empty",
        serialize_with = "serialize_ft_macros",
        deserialize_with = "deserialize_ft_macros",
        default
    )]
    pub ft_macros: Vec<FtMacro>,
}

impl DmxMode {
    /// Looks up the [Geometry] linked by this DMX mode.
    pub fn geometry<'s>(&self, parent_fixture_type: &'s FixtureType) -> Option<&'s Geometry> {
        parent_fixture_type.root_geometry(self.geometry.as_ref()?)
    }

    /// Looks up a [DmxChannel] by [name](DmxChannel::name).
    pub fn dmx_channel(&self, name: &str) -> Option<&DmxChannel> {
        self.dmx_channels
            .iter()
            .find(|channel| channel.name().as_ref() == name)
    }

    /// Looks up a [Relation] by [name](Relation::name).
    pub fn relation(&self, name: &str) -> Option<&Relation> {
        self.relations
            .iter()
            .find(|relation| relation.name.as_ref().map(Name::as_ref) == Some(name))
    }

    /// Looks up an [FtMacro] by [name](FtMacro::name).
    pub fn ft_macro(&self, name: &str) -> Option<&FtMacro> {
        self.ft_macros
            .iter()
            .find(|m| m.name.as_ref().map(Name::as_ref) == Some(name))
    }

    /// Performs validation checks on the object.
    pub fn validate(&self, parent_fixture_type: &FixtureType, result: &mut ValidationResult) {
        if self.name.is_none() {
            result.errors.push(ValidationError::new(
                ValidationObject::DmxMode,
                None,
                ValidationErrorType::MissingName,
            ));
        }

        let name = self.name.as_ref();

        if let (Some(geometry), None) = (&self.geometry, self.geometry(parent_fixture_type)) {
            result.errors.push(ValidationError::new(
                ValidationObject::DmxMode,
                name.map(Name::to_string),
                ValidationErrorType::LinkNotFound(
                    ValidationObject::Geometry,
                    Node::new([geometry.clone()]),
                ),
            ));
        }

        let duplicate_channel_name = self
            .dmx_channels
            .iter()
            .map(|channel| channel.name())
            .find_duplicate();
        if let Some(name) = duplicate_channel_name {
            result.errors.push(ValidationError::new(
                ValidationObject::DmxChannel,
                name.to_string(),
                ValidationErrorType::DuplicateName,
            ));
        }

        let duplicate_relation_name = self
            .relations
            .iter()
            .filter_map(|relation| relation.name.as_ref())
            .find_duplicate();
        if let Some(name) = duplicate_relation_name {
            result.errors.push(ValidationError::new(
                ValidationObject::Relation,
                name.to_string(),
                ValidationErrorType::DuplicateName,
            ));
        }

        let duplicate_macro_name = self
            .ft_macros
            .iter()
            .filter_map(|ft_macro| ft_macro.name.as_ref())
            .find_duplicate();
        if let Some(name) = duplicate_macro_name {
            result.errors.push(ValidationError::new(
                ValidationObject::FtMacro,
                name.to_string(),
                ValidationErrorType::DuplicateName,
            ));
        }

        for dmx_channel in &self.dmx_channels {
            dmx_channel.validate(parent_fixture_type, self, result);
        }

        for relation in &self.relations {
            relation.validate(self, result);
        }

        for ft_macro in &self.ft_macros {
            ft_macro.validate(self, result);
        }
    }
}

define_collect_helper!("DMXChannel" (serialize_dmx_channels, deserialize_dmx_channels) -> DmxChannel);

/// Defines a DMX channel in the footprint of a device.
///
/// Corresponds to a `<DMXChannel>` XML node.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DmxChannel {
    /// Number of the DMX break.
    ///
    /// Breaks are used if a fixture needs more than one start address. For example, a scroller is
    /// added to an existing conventional fixture and the fixture is connected to a dimmer. This
    /// dimmer is patched in one universe and the scroller is connected to a PSU that, on the other
    /// hand, is patched in another universe. Both, the conventional fixture and scroller, are
    /// treated as one combined fixture.
    ///
    /// Corresponds to the `DMXBreak` XML attribute.
    #[serde(rename = "@DMXBreak", default)]
    pub dmx_break: DmxBreak,

    /// Relative addresses of the current DMX channel from highest to least significant.
    ///
    /// A value of None indicates the channel does not have any addresses.
    ///
    /// Corresponds to the `Offset` XML attribute.
    #[serde(
        rename = "@Offset",
        skip_serializing_if = "Option::is_none",
        serialize_with = "serialize_dmx_channel_offset",
        deserialize_with = "deserialize_dmx_channel_offset"
    )]
    pub offset: Option<Vec<i32>>,

    /// Link to the channel function that will be activated by default for this channel.
    ///
    /// Corresponds to the `InitialFunction` XML attribute.
    #[serde(rename = "@InitialFunction", skip_serializing_if = "Option::is_none")]
    pub initial_function: Option<Node>,

    /// Highlight value for the current channel.
    ///
    /// A value of None indicates the channel does not respond to highlight commands.
    ///
    /// Corresponds to the `Highlight` XML attribute.
    #[serde(
        rename = "@Highlight",
        skip_serializing_if = "Option::is_none",
        serialize_with = "serialize_dmx_channel_highlight",
        deserialize_with = "deserialize_dmx_channel_highlight"
    )]
    pub highlight: Option<DmxValue>,

    /// Name of the geometry the current channel controls.
    ///
    /// The Geometry should be the place in the tree of geometries where the function of the DMX
    /// channel (as defined by [ChannelFunction]) is located either physically or logically.
    /// If the DMX channel doesn't have a location, it will be in the top level geometry of the
    /// geometry tree. Attributes follow a trickle down principle, so they are inherited from top
    /// down.
    ///
    /// Corresponds to the `Geometry` XML attribute.
    #[serde(rename = "@Geometry")]
    pub geometry: Name,

    /// A list of mutually exclusive logical channels that make up the DMX channel.
    ///
    /// Corresponds to all `<LogicalChannel>` XML child nodes.
    #[serde(
        rename = "LogicalChannel",
        skip_serializing_if = "Vec::is_empty",
        default
    )]
    pub logical_channels: Vec<LogicalChannel>,
}

impl DmxChannel {
    /// Returns the unique name of the DMX channel.
    ///
    /// The name of a DMX channel is not user-defined and consists of the linked geometry name and
    /// the attribute name of the first logical channel, separated by a `_` character. For example
    /// a DMX channel linked to a geometry named `Yoke` with a logical channel connected to the
    /// `Pan` attribute, is named `Yoke_Pan`.
    ///
    /// This combination must be unique within the containing [DmxMode].
    pub fn name(&self) -> Name {
        let logical_channel_name = self
            .logical_channels
            .first()
            .map(|channel| channel.name().as_ref())
            .unwrap_or("");
        Name::new(format!("{}_{}", self.geometry, logical_channel_name)).unwrap()
    }

    /// Looks up the [ChannelFunction] that is activated by default for this channel.
    ///
    /// If an initial function is not specified by the channel, it defaults to the first channel
    /// function of the first logical function of this DMX channel.
    pub fn initial_function(&self) -> Option<(&LogicalChannel, &ChannelFunction)> {
        let Some(node) = &self.initial_function else {
            let first_channel = self.logical_channels.first()?;
            let first_function = first_channel.channel_functions.first()?;
            return Some((first_channel, first_function));
        };

        // first name in the node is the name of this DmxChannel (for some reason)
        let (channel_name, tail) = node.split_first()?;
        if channel_name != &self.name() {
            return None;
        }

        let (logical_channel_name, tail) = tail.split_first()?;
        let logical_channel = self.logical_channel(logical_channel_name)?;

        let function_name = tail.single()?;
        let function = logical_channel.channel_function(function_name)?;

        Some((logical_channel, function))
    }

    /// Looks up the [Geometry] linked by this DMX channel.
    pub fn geometry<'s>(&self, parent_fixture_type: &'s FixtureType) -> Option<&'s Geometry> {
        parent_fixture_type.nested_geometry(&self.geometry)
    }

    /// Looks up a [LogicalChannel] by [name](LogicalChannel::name).
    pub fn logical_channel(&self, name: &str) -> Option<&LogicalChannel> {
        self.logical_channels
            .iter()
            .find(|channel| channel.name().as_ref() == name)
    }

    /// Performs validation checks on the object.
    pub fn validate(
        &self,
        parent_fixture_type: &FixtureType,
        parent_dmx_mode: &DmxMode,
        result: &mut ValidationResult,
    ) {
        if let (Some(initial_function), None) = (&self.initial_function, self.initial_function()) {
            result.errors.push(ValidationError::new(
                ValidationObject::DmxChannel,
                self.name().to_string(),
                ValidationErrorType::LinkNotFound(
                    ValidationObject::ChannelFunction,
                    initial_function.clone(),
                ),
            ));
        }

        if self.geometry(parent_fixture_type).is_none() {
            result.errors.push(ValidationError::new(
                ValidationObject::DmxChannel,
                self.name().to_string(),
                ValidationErrorType::LinkNotFound(
                    ValidationObject::Geometry,
                    Node::new([self.geometry.clone()]),
                ),
            ))
        }

        let duplicate_logical_channel_name = self
            .logical_channels
            .iter()
            .map(|logical_channel| logical_channel.name())
            .find_duplicate();
        if let Some(name) = duplicate_logical_channel_name {
            result.errors.push(ValidationError::new(
                ValidationObject::LogicalChannel,
                name.to_string(),
                ValidationErrorType::DuplicateName,
            ));
        }

        for logical_channel in &self.logical_channels {
            logical_channel.validate(parent_fixture_type, parent_dmx_mode, result);
        }
    }
}

/// Number of the DMX break specified by a [DmxChannel], or the special "Overwrite" value.
///
/// Serialized format:
/// ```text
/// Normal value:  1
/// Special value: Overwrite
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DmxBreak {
    /// Defined DMX break.
    Value(i32),

    /// Indicates this number will be overwritten by a
    /// [ReferenceGeometry](super::geometry::ReferenceGeometry).
    Overwrite,
}

impl Serialize for DmxBreak {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match *self {
            DmxBreak::Value(value) => serializer.serialize_i32(value),
            DmxBreak::Overwrite => serializer.serialize_str("Overwrite"),
        }
    }
}

impl<'de> Deserialize<'de> for DmxBreak {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct DmxBreakVisitor;

        impl<'de> Visitor<'de> for DmxBreakVisitor {
            type Value = DmxBreak;

            fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
                formatter.write_str("`Overwrite` or an integer")
            }

            fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E>
            where
                E: Error,
            {
                Ok(DmxBreak::Value(v))
            }

            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
            where
                E: Error,
            {
                if v == "Overwrite" {
                    return Ok(DmxBreak::Overwrite);
                }

                match v.parse::<i32>() {
                    Ok(value) => Ok(DmxBreak::Value(value)),
                    Err(_) => Err(E::invalid_value(Unexpected::Str(v), &self)),
                }
            }
        }

        deserializer.deserialize_str(DmxBreakVisitor)
    }
}

impl Default for DmxBreak {
    fn default() -> Self {
        DmxBreak::Value(1)
    }
}

fn serialize_dmx_channel_offset<S>(
    value: &Option<Vec<i32>>,
    serializer: S,
) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    match value {
        None => serializer.serialize_str("None"),
        Some(values) => serializer.serialize_str(&values.iter().join(",")),
    }
}

fn deserialize_dmx_channel_offset<'de, D>(deserializer: D) -> Result<Option<Vec<i32>>, D::Error>
where
    D: Deserializer<'de>,
{
    let s: String = Deserialize::deserialize(deserializer)?;
    let s = s.trim();
    if s == "None" || s.is_empty() {
        return Ok(None);
    }

    let values: Result<Vec<_>, _> = s
        .split(',')
        .map(|value| {
            value.parse::<i32>().map_err(|_| {
                D::Error::invalid_value(
                    Unexpected::Str(value),
                    &"an integer between -2^31 and 2^31",
                )
            })
        })
        .collect();
    Ok(Some(values?))
}

fn serialize_dmx_channel_highlight<S>(
    value: &Option<DmxValue>,
    serializer: S,
) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    match value {
        None => serializer.serialize_str("None"),
        Some(value) => value.serialize(serializer),
    }
}

fn deserialize_dmx_channel_highlight<'de, D>(deserializer: D) -> Result<Option<DmxValue>, D::Error>
where
    D: Deserializer<'de>,
{
    struct DmxHighlightVisitor;

    impl<'de> Visitor<'de> for DmxHighlightVisitor {
        type Value = Option<DmxValue>;

        fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
            formatter.write_str("`None` or a DMX value in the format uint/n or uint/ns")
        }

        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
        where
            E: Error,
        {
            if v == "None" {
                Ok(None)
            } else {
                Ok(Some(DmxValue::deserialize(StrDeserializer::new(v))?))
            }
        }
    }

    deserializer.deserialize_str(DmxHighlightVisitor)
}

/// Defines a logical, mutually exclusive part of a [DmxChannel].
///
/// Each [Attribute] is assigned to a logical channel and defines the function of the logical
/// channel.
///
/// All logical channels that are children of the same DMX channel are mutually exclusive.
///
/// In a DMX mode, only one logical channel with the same attribute can reference the same geometry
/// at a time.
///
/// Corresponds to a `<LogicalChannel>` XML node.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LogicalChannel {
    /// Link to the attribute.
    ///
    /// Corresponds to the `Attribute` XML attribute.
    #[serde(rename = "@Attribute")]
    pub attribute: Node,

    /// Controls if the logical channel fades between values.
    ///
    /// - With snap enabled, the channel will jump directly to the new value.
    /// - With snap disabled, the channel will fade between values.
    ///
    /// Corresponds to the `Snap` XML attribute.
    #[serde(
        rename = "@Snap",
        skip_serializing_if = "skip_serializing_logical_channel_snap",
        serialize_with = "serialize_logical_channel_snap",
        deserialize_with = "deserialize_logical_channel_snap",
        default
    )]
    pub snap: bool,

    /// Defines if all the subordinate channel functions react to a Group Control defined by the
    /// control system.
    ///
    /// Corresponds to the `Master` XML attribute.
    #[serde(rename = "@Master", default)]
    pub master: LogicalChannelMaster,

    /// Minimum fade time for moves in black action.
    ///
    /// `mib_fade` is defined for the complete DMX range.
    ///
    /// Corresponds to the `MibFade` XML attribute.
    #[serde(rename = "@MibFade", default)]
    pub mib_fade: f64,

    /// Minimum fade time for the subordinate channel functions to change DMX values by the control
    /// system.
    ///
    /// `dmx_change_time_limit` is defined for the complete DMX range.
    ///
    /// Corresponds to the `DMXChangeTimeLimit` XML attribute.
    #[serde(rename = "@DMXChangeTimeLimit", default)]
    pub dmx_change_time_limit: f64,

    /// A list of channel functions defining the function of each DMX range.
    ///
    /// Corresponds to all `<ChannelFunction>` XML child nodes.
    #[serde(
        rename = "ChannelFunction",
        skip_serializing_if = "Vec::is_empty",
        default
    )]
    pub channel_functions: Vec<ChannelFunction>,
}

impl LogicalChannel {
    /// Returns the unique name of the logical channel.
    ///
    /// The name of a logical channel is not user-defined and is equal to the linked attribute name.
    pub fn name(&self) -> &Name {
        self.attribute.first().unwrap()
    }

    /// Looks up the [Attribute] linked by this logical channel.
    pub fn attribute<'s>(&self, parent_fixture_type: &'s FixtureType) -> Option<&'s Attribute> {
        let name = self.attribute.single()?;
        parent_fixture_type.attribute_definitions.attribute(name)
    }

    /// Looks up a [ChannelFunction] by [name](ChannelFunction::name).
    pub fn channel_function(&self, name: &str) -> Option<&ChannelFunction> {
        self.channel_functions
            .iter()
            .find(|func| func.name.as_ref().map(Name::as_ref) == Some(name))
    }

    /// Performs validation checks on the object.
    pub fn validate(
        &self,
        parent_fixture_type: &FixtureType,
        parent_dmx_mode: &DmxMode,
        result: &mut ValidationResult,
    ) {
        if self.attribute(parent_fixture_type).is_none() {
            result.errors.push(ValidationError::new(
                ValidationObject::LogicalChannel,
                self.name().to_string(),
                ValidationErrorType::LinkNotFound(
                    ValidationObject::Attribute,
                    self.attribute.clone(),
                ),
            ));
        }

        let duplicate_channel_function_name = self
            .channel_functions
            .iter()
            .filter_map(|channel_function| channel_function.name.as_ref())
            .find_duplicate();
        if let Some(name) = duplicate_channel_function_name {
            result.errors.push(ValidationError::new(
                ValidationObject::ChannelFunction,
                name.to_string(),
                ValidationErrorType::DuplicateName,
            ));
        }

        for channel_function in &self.channel_functions {
            channel_function.validate(parent_fixture_type, parent_dmx_mode, result);
        }
    }
}

fn skip_serializing_logical_channel_snap(value: &bool) -> bool {
    // `false` is the default, no need to serialize
    !*value
}

fn serialize_logical_channel_snap<S>(value: &bool, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    serializer.serialize_str(match value {
        true => "Yes",
        false => "No",
    })
}

fn deserialize_logical_channel_snap<'de, D>(deserializer: D) -> Result<bool, D::Error>
where
    D: Deserializer<'de>,
{
    struct SnapVisitor;

    impl<'de> Visitor<'de> for SnapVisitor {
        type Value = bool;

        fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
            formatter.write_str("`Yes`, `No`, `On` or `Off`")
        }

        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
        where
            E: Error,
        {
            match v {
                "Yes" | "On" => Ok(true),
                "No" | "Off" => Ok(false),
                _ => Err(E::invalid_value(Unexpected::Str(v), &self)),
            }
        }
    }

    deserializer.deserialize_str(SnapVisitor)
}

/// Defines if all the channel functions in a [LogicalChannel] react to a Group Control defined by
/// the control system.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
pub enum LogicalChannelMaster {
    /// Logical channel reacts to a grand master.
    Grand,

    /// Logical channel reacts to a group master.
    Group,

    /// Logical channel does not react to a master.
    #[default]
    #[serde(other)]
    None,
}

/// Defines the function of a DMX range in a [LogicalChannel].
///
/// Corresponds to a `<ChannelFunction>` XML node.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ChannelFunction {
    /// The unique name of the channel function.
    ///
    /// When a name is not supplied, it is assumed to be equal to the name of the attribute and the
    /// number of the channel function.
    ///
    /// Corresponds to the `Name` XML attribute.
    #[serde(rename = "@Name", skip_serializing_if = "Option::is_none")]
    pub name: Option<Name>,

    /// Link to the attribute that is controlled by this channel function.
    ///
    /// Corresponds to the `Attribute` XML attribute.
    #[serde(rename = "@Attribute", default = "default_channel_function_attribute")]
    pub attribute: Node,

    /// The manufacturer's original name of the attribute.
    ///
    /// Corresponds to the `OriginalAttribute` XML attribute.
    #[serde(rename = "@OriginalAttribute", default)]
    pub original_attribute: String,

    /// Start DMX value.
    ///
    /// The end DMX value is calculated as the `dmx_from` of the next channel function - 1, or the
    /// maximum value of the DMX channel.
    ///
    /// Corresponds to the `DMXFrom` XML attribute.
    #[serde(rename = "@DMXFrom", default)]
    pub dmx_from: DmxValue,

    /// Default DMX value of the channel function when activated by the control system.
    ///
    /// Corresponds to the `Default` XML attribute.
    #[serde(rename = "@Default", default)]
    pub default: DmxValue,

    /// Physical start value.
    ///
    /// Corresponds to the `PhysicalFrom` XML attribute.
    #[serde(
        rename = "@PhysicalFrom",
        default = "default_channel_function_physical_from"
    )]
    pub physical_from: f64,

    /// Physical end value.
    ///
    /// Corresponds to the `PhysicalTo` XML attribute.
    #[serde(
        rename = "@PhysicalTo",
        default = "default_channel_function_physical_to"
    )]
    pub physical_to: f64,

    /// Time in seconds to move from min to max of the channel function.
    ///
    /// Corresponds to the `RealFade` XML attribute.
    #[serde(rename = "@RealFade", default)]
    pub real_fade: f64,

    /// Time in seconds to accelerate from stop to maximum velocity.
    ///
    /// Corresponds to the `RealAcceleration` XML attribute.
    #[serde(rename = "@RealAcceleration", default)]
    pub real_acceleration: f64,

    /// Optional link to a wheel.
    ///
    /// Corresponds to the `Wheel` XML attribute.
    #[serde(rename = "@Wheel", skip_serializing_if = "Option::is_none")]
    pub wheel: Option<Node>,

    /// Optional link to an emitter in the
    /// [PhysicalDescriptions](super::physical_descriptions::PhysicalDescriptions).
    ///
    /// Corresponds to the `Emitter` XML attribute.
    #[serde(rename = "@Emitter", skip_serializing_if = "Option::is_none")]
    pub emitter: Option<Node>,

    /// Optional link to a filter in the
    /// [PhysicalDescriptions](super::physical_descriptions::PhysicalDescriptions).
    ///
    /// Corresponds to the `Filter` XML attribute.
    #[serde(rename = "@Filter", skip_serializing_if = "Option::is_none")]
    pub filter: Option<Node>,

    /// Optional link to a color space in the
    /// [PhysicalDescriptions](super::physical_descriptions::PhysicalDescriptions).
    ///
    /// Corresponds to the `ColorSpace` XML attribute.
    #[serde(rename = "@ColorSpace", skip_serializing_if = "Option::is_none")]
    pub color_space: Option<Node>,

    /// Optional link to a gamut in the
    /// [PhysicalDescriptions](super::physical_descriptions::PhysicalDescriptions).
    ///
    /// Corresponds to the `Gamut` XML attribute.
    #[serde(rename = "@Gamut", skip_serializing_if = "Option::is_none")]
    pub gamut: Option<Node>,

    /// Optional link to a [DmxChannel] or a [ChannelFunction].
    ///
    /// Corresponds to the `ModeMaster` XML attribute.
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub mode_master: Option<ModeMasterNode>,

    /// Optional link to a DMX profile.
    ///
    /// Corresponds to the `DMXProfile` XML attribute.
    #[serde(rename = "@DMXProfile", skip_serializing_if = "Option::is_none")]
    pub dmx_profile: Option<Node>,

    /// Minimum physical value that will be used for the DMX profile.
    ///
    /// When omitted the value of `physical_from` should be used.
    ///
    /// Corresponds to the `Min` XML attribute.
    #[serde(rename = "@Min", skip_serializing_if = "Option::is_none")]
    pub min: Option<f64>,

    /// Maximum physical value that will be used for the DMX profile.
    ///
    /// When omitted the value of `physical_to` should be used.
    ///
    /// Corresponds to the `Max` XML attribute.
    #[serde(rename = "@Max", skip_serializing_if = "Option::is_none")]
    pub max: Option<f64>,

    /// Custom name that can be used to address this channel function with other command-based
    /// protocols like OSC.
    ///
    /// When omitted the name is calculated as the full node name of the channel function, for
    /// example `Head_Dimmer.Dimmer.Dimmer`.
    ///
    /// Corresponds to the `CustomName` XML attribute.
    #[serde(
        rename = "@CustomName",
        skip_serializing_if = "Option::is_none",
        deserialize_with = "non_empty_string",
        default
    )]
    pub custom_name: Option<String>,

    /// The list of channel sets in the channel function.
    ///
    /// Corresponds to all `<ChannelSet>` XML child nodes.
    #[serde(rename = "ChannelSet", skip_serializing_if = "Vec::is_empty", default)]
    pub channel_sets: Vec<ChannelSet>,

    /// The list of sub channel sets in the channel function.
    ///
    /// Corresponds to all `<SubChannelSet>` XML child nodes.
    #[serde(
        rename = "SubChannelSet",
        skip_serializing_if = "Vec::is_empty",
        default
    )]
    pub sub_channel_sets: Vec<SubChannelSet>,
}

impl ChannelFunction {
    /// Looks up the [Attribute] linked by this channel function.
    pub fn attribute<'s>(&self, parent_fixture_type: &'s FixtureType) -> Option<&'s Attribute> {
        let name = self.attribute.single()?;
        parent_fixture_type.attribute_definitions.attribute(name)
    }

    /// Looks up the [Wheel] linked by this channel function.
    pub fn wheel<'s>(&self, parent_fixture_type: &'s FixtureType) -> Option<&'s Wheel> {
        let name = self.wheel.as_ref()?.single()?;
        parent_fixture_type.wheel(name)
    }

    /// Looks up the [Emitter] linked by this channel function.
    pub fn emitter<'s>(&self, parent_fixture_type: &'s FixtureType) -> Option<&'s Emitter> {
        let name = self.emitter.as_ref()?.single()?;
        parent_fixture_type.physical_descriptions.emitter(name)
    }

    /// Looks up the [Filter] linked by this channel function.
    pub fn filter<'s>(&self, parent_fixture_type: &'s FixtureType) -> Option<&'s Filter> {
        let name = self.filter.as_ref()?.single()?;
        parent_fixture_type.physical_descriptions.filter(name)
    }

    /// Looks up the [ColorSpace] linked by this channel function.
    pub fn color_space<'s>(&self, parent_fixture_type: &'s FixtureType) -> Option<&'s ColorSpace> {
        let name = self.color_space.as_ref()?.single()?;
        parent_fixture_type.physical_descriptions.color_space(name)
    }

    /// Looks up the [Gamut] linked by this channel function.
    pub fn gamut<'s>(&self, parent_fixture_type: &'s FixtureType) -> Option<&'s Gamut> {
        let name = self.gamut.as_ref()?.single()?;
        parent_fixture_type.physical_descriptions.gamut(name)
    }

    /// Looks up the [DmxProfile] linked by this channel function.
    pub fn dmx_profile<'s>(&self, parent_fixture_type: &'s FixtureType) -> Option<&'s DmxProfile> {
        let name = self.dmx_profile.as_ref()?.single()?;
        parent_fixture_type.physical_descriptions.dmx_profile(name)
    }

    /// Returns the minimum physical value that will be used for the DMX profile, or the physical
    /// start value if a minimum is not set.
    pub fn min(&self) -> f64 {
        self.min.unwrap_or(self.physical_from)
    }

    /// Returns the maximum physical value that will be used for the DMX profile, or the physical
    /// end value if a maximum is not set.
    pub fn max(&self) -> f64 {
        self.max.unwrap_or(self.physical_to)
    }

    /// Looks up a [ChannelSet] by [name](ChannelSet::name).
    pub fn channel_set(&self, name: &str) -> Option<&ChannelSet> {
        self.channel_sets
            .iter()
            .find(|channel_set| channel_set.name.as_ref().map(Name::as_ref) == Some(name))
    }

    /// Looks up a [SubChannelSet] by [name](SubChannelSet::name).
    pub fn sub_channel_set(&self, name: &str) -> Option<&SubChannelSet> {
        self.sub_channel_sets
            .iter()
            .find(|sub| sub.name.as_ref().map(Name::as_ref) == Some(name))
    }

    /// Performs validation checks on the object.
    pub fn validate(
        &self,
        parent_fixture_type: &FixtureType,
        parent_dmx_mode: &DmxMode,
        result: &mut ValidationResult,
    ) {
        if self.name.is_none() {
            result.errors.push(ValidationError::new(
                ValidationObject::ChannelFunction,
                None,
                ValidationErrorType::MissingName,
            ));
        }

        let name = self.name.as_ref();

        if self.attribute(parent_fixture_type).is_none() {
            result.errors.push(ValidationError::new(
                ValidationObject::ChannelFunction,
                name.map(Name::to_string),
                ValidationErrorType::LinkNotFound(
                    ValidationObject::Attribute,
                    self.attribute.clone(),
                ),
            ));
        }

        if let (Some(wheel), None) = (&self.wheel, self.wheel(parent_fixture_type)) {
            result.errors.push(ValidationError::new(
                ValidationObject::ChannelFunction,
                name.map(Name::to_string),
                ValidationErrorType::LinkNotFound(ValidationObject::Wheel, wheel.clone()),
            ));
        }

        if let (Some(emitter), None) = (&self.emitter, self.emitter(parent_fixture_type)) {
            result.errors.push(ValidationError::new(
                ValidationObject::ChannelFunction,
                name.map(Name::to_string),
                ValidationErrorType::LinkNotFound(ValidationObject::Emitter, emitter.clone()),
            ));
        }

        if let (Some(filter), None) = (&self.filter, self.filter(parent_fixture_type)) {
            result.errors.push(ValidationError::new(
                ValidationObject::ChannelFunction,
                name.map(Name::to_string),
                ValidationErrorType::LinkNotFound(ValidationObject::Filter, filter.clone()),
            ));
        }

        if let (Some(color_space), None) =
            (&self.color_space, self.color_space(parent_fixture_type))
        {
            result.errors.push(ValidationError::new(
                ValidationObject::ChannelFunction,
                name.map(Name::to_string),
                ValidationErrorType::LinkNotFound(
                    ValidationObject::ColorSpace,
                    color_space.clone(),
                ),
            ));
        }

        if let (Some(gamut), None) = (&self.gamut, self.gamut(parent_fixture_type)) {
            result.errors.push(ValidationError::new(
                ValidationObject::ChannelFunction,
                name.map(Name::to_string),
                ValidationErrorType::LinkNotFound(ValidationObject::Gamut, gamut.clone()),
            ));
        }

        if let Some(mode_master) = &self.mode_master {
            if mode_master.mode_master(parent_dmx_mode).is_none() {
                result.errors.push(ValidationError::new(
                    ValidationObject::ChannelFunction,
                    name.map(Name::to_string),
                    ValidationErrorType::LinkNotFound(
                        ValidationObject::ModeMaster,
                        mode_master.node.clone(),
                    ),
                ));
            }
        }

        if let (Some(dmx_profile), None) =
            (&self.dmx_profile, self.dmx_profile(parent_fixture_type))
        {
            result.errors.push(ValidationError::new(
                ValidationObject::ChannelFunction,
                name.map(Name::to_string),
                ValidationErrorType::LinkNotFound(
                    ValidationObject::DmxProfile,
                    dmx_profile.clone(),
                ),
            ));
        }

        for channel_set in &self.channel_sets {
            channel_set.validate(parent_fixture_type, self, result);
        }

        for sub_channel_set in &self.sub_channel_sets {
            sub_channel_set.validate(parent_fixture_type, result);
        }
    }
}

fn default_channel_function_attribute() -> Node {
    Node::new(vec![Name::new("NoFeature").unwrap()])
}
fn default_channel_function_physical_from() -> f64 {
    0.
}
fn default_channel_function_physical_to() -> f64 {
    1.
}

/// A link to a [DmxChannel] or [ChannelFunction] defining the mode master of a [ChannelFunction].
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ModeMasterNode {
    /// Link to a [DmxChannel] or a [ChannelFunction].
    ///
    /// Corresponds to the `ModeMaster` XML attribute.
    #[serde(rename = "@ModeMaster")]
    pub node: Node,

    /// The lowest DMX value of the linked DMX channel or channel function to activate the current
    /// channel function.
    ///
    /// Corresponds to the `ModeFrom` XML attribute.
    #[serde(rename = "@ModeFrom", default)]
    pub from: DmxValue,

    /// The highest DMX value of the linked DMX channel or channel function to activate the current
    /// function.
    ///
    /// Corresponds to the `ModeTo` XML attribute.
    #[serde(rename = "@ModeTo", default)]
    pub to: DmxValue,
}

impl ModeMasterNode {
    /// Looks up the [DmxChannel] or [ChannelFunction] linked by this channel function.
    pub fn mode_master<'s>(&self, parent_dmx_mode: &'s DmxMode) -> Option<ModeMaster<'s>> {
        let (channel_name, tail) = self.node.split_first()?;
        let channel = parent_dmx_mode.dmx_channel(channel_name)?;

        let Some((logical_channel_name, tail)) = tail.split_first() else {
            return Some(ModeMaster::DmxChannel(channel));
        };

        let logical_channel = channel.logical_channel(logical_channel_name)?;
        let function_name = tail.single()?;
        let function = logical_channel.channel_function(function_name)?;

        Some(ModeMaster::ChannelFunction(
            channel,
            logical_channel,
            function,
        ))
    }
}

/// Represents the [DmxChannel] or [ChannelFunction] linked as a mode master with a [ModeMasterNode].
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ModeMaster<'s> {
    /// Mode master is a DMX channel.
    DmxChannel(&'s DmxChannel),

    /// Mode master is a channel function.
    ChannelFunction(&'s DmxChannel, &'s LogicalChannel, &'s ChannelFunction),
}

/// Defines a channel set of a [ChannelFunction].
///
/// Corresponds to a `<ChannelSet>` XML node.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ChannelSet {
    /// The name of the channel set.
    ///
    /// Corresponds to the `Name` XML attribute.
    #[serde(rename = "@Name", skip_serializing_if = "Option::is_none")]
    pub name: Option<Name>,

    /// Start DMX value.
    ///
    /// The end DMX value is calculated as the `dmx_from` of the next channel set - 1, or the
    /// maximum value of the current channel function.
    ///
    /// Corresponds to the `DMXFrom` XML attribute.
    #[serde(rename = "@DMXFrom")]
    pub dmx_from: DmxValue,

    /// Physical start value.
    ///
    /// When omitted the value of the parent channel function's `physical_from` should be used.
    ///
    /// Corresponds to the `PhysicalFrom` XML attribute.
    #[serde(rename = "@PhysicalFrom", skip_serializing_if = "Option::is_none")]
    pub physical_from: Option<f64>,

    /// Physical end value.
    ///
    /// When omitted the value of the parent channel function's `physical_to` should be used.
    ///
    /// Corresponds to the `PhysicalTo` XML attribute.
    #[serde(rename = "@PhysicalTo", skip_serializing_if = "Option::is_none")]
    pub physical_to: Option<f64>,

    /// If the channel function has a link to a wheel, specifies the corresponding slot index.
    ///
    /// The wheel slot index results from the order of slots of the wheel which is linked in the
    /// channel function.
    ///
    /// Corresponds to the `WheelSlotIndex` XML attribute.
    #[serde(
        rename = "@WheelSlotIndex",
        skip_serializing_if = "Option::is_none",
        serialize_with = "serialize_wheel_slot_index",
        deserialize_with = "deserialize_wheel_slot_index",
        default
    )]
    pub wheel_slot_index: Option<i32>,
}

impl ChannelSet {
    /// Returns the physical start value of the channel set, or the physical start value of the
    /// parent channel function if no value is provided.
    pub fn physical_from(&self, parent_channel_function: &ChannelFunction) -> f64 {
        self.physical_from
            .unwrap_or(parent_channel_function.physical_from)
    }

    /// Returns the physical end value of the channel set, or the physical end value of the parent
    /// channel function if no value is provided.
    pub fn physical_to(&self, parent_channel_function: &ChannelFunction) -> f64 {
        self.physical_to
            .unwrap_or(parent_channel_function.physical_to)
    }

    /// Looks up the [WheelSlot] linked by this channel slot, in the [Wheel] linked by the parent
    /// channel function.
    pub fn wheel_slot<'s>(&self, parent_wheel: &'s Wheel) -> Option<&'s WheelSlot> {
        parent_wheel.slots.get(self.wheel_slot_index? as usize)
    }

    /// Performs validation checks on the object.
    pub fn validate(
        &self,
        parent_fixture_type: &FixtureType,
        parent_channel_function: &ChannelFunction,
        result: &mut ValidationResult,
    ) {
        let name = self.name.as_ref();

        if let (Some(wheel_slot_index), Some(wheel)) = (
            self.wheel_slot_index,
            parent_channel_function.wheel(parent_fixture_type),
        ) {
            if self.wheel_slot(wheel).is_none() {
                result.errors.push(ValidationError::new(
                    ValidationObject::ChannelSet,
                    name.map(Name::to_string),
                    ValidationErrorType::LinkNotFound(
                        ValidationObject::WheelSlot,
                        Node::new([Name::new_lossy(wheel_slot_index.to_string())]),
                    ),
                ));
            }
        }
    }
}

fn serialize_wheel_slot_index<S>(value: &Option<i32>, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    let serialize_value = value.map_or(0, |val| val.max(0) + 1);
    serialize_value.serialize(serializer)
}

fn deserialize_wheel_slot_index<'de, D>(deserializer: D) -> Result<Option<i32>, D::Error>
where
    D: Deserializer<'de>,
{
    let serialize_value = i32::deserialize(deserializer)?;
    if serialize_value <= 0 {
        Ok(None)
    } else {
        Ok(Some(serialize_value - 1))
    }
}

/// Defines a sub channel set of a [ChannelFunction].
///
/// Corresponds to a `<SubChannelSet>` XML node.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SubChannelSet {
    /// The name of the sub channel set.
    ///
    /// Corresponds to the `Name` XML attribute.
    #[serde(rename = "@Name", skip_serializing_if = "Option::is_none")]
    pub name: Option<Name>,

    /// Physical start value.
    ///
    /// Corresponds to the `PhysicalFrom` XML attribute.
    #[serde(rename = "@PhysicalFrom")]
    pub physical_from: f64,

    /// Physical end value.
    ///
    /// Corresponds to the `PhysicalTo` XML attribute.
    #[serde(rename = "@PhysicalTo")]
    pub physical_to: f64,

    /// Link to the sub physical unit.
    ///
    /// Corresponds to the `SubPhysicalUnit` XML attribute.
    #[serde(rename = "@SubPhysicalUnit")]
    pub sub_physical_unit: Node,

    /// Optional link to the DMX profile.
    ///
    /// Corresponds to the `DMXProfile` XML attribute.
    #[serde(rename = "@DMXProfile", skip_serializing_if = "Option::is_none")]
    pub dmx_profile: Option<Node>,
}

impl SubChannelSet {
    /// Looks up the [SubPhysicalUnit] linked by this sub channel set.
    pub fn sub_physical_unit<'s>(
        &self,
        parent_fixture_type: &'s FixtureType,
    ) -> Option<&'s SubPhysicalUnit> {
        let (attribute_name, tail) = self.sub_physical_unit.split_first()?;
        let attribute = parent_fixture_type
            .attribute_definitions
            .attribute(attribute_name)?;

        let sub_name = tail.single()?;
        attribute
            .subphysical_units
            .iter()
            .find(|unit| unit.type_.as_str() == sub_name.as_ref())
    }

    /// Looks up the [DmxProfile] linked by this sub channel set.
    pub fn dmx_profile<'s>(&self, parent_fixture_type: &'s FixtureType) -> Option<&'s DmxProfile> {
        let name = self.dmx_profile.as_ref()?.single()?;
        parent_fixture_type.physical_descriptions.dmx_profile(name)
    }

    /// Performs validation checks on the object.
    pub fn validate(&self, parent_fixture_type: &FixtureType, result: &mut ValidationResult) {
        if self.name.is_none() {
            result.errors.push(ValidationError::new(
                ValidationObject::SubChannelSet,
                None,
                ValidationErrorType::MissingName,
            ));
        }

        let name = self.name.as_ref();

        if self.sub_physical_unit(parent_fixture_type).is_none() {
            result.errors.push(ValidationError::new(
                ValidationObject::SubChannelSet,
                name.map(Name::to_string),
                ValidationErrorType::LinkNotFound(
                    ValidationObject::SubPhysicalUnit,
                    self.sub_physical_unit.clone(),
                ),
            ));
        }

        if let (Some(dmx_profile), None) =
            (&self.dmx_profile, self.dmx_profile(parent_fixture_type))
        {
            result.errors.push(ValidationError::new(
                ValidationObject::SubChannelSet,
                name.map(Name::to_string),
                ValidationErrorType::LinkNotFound(
                    ValidationObject::DmxProfile,
                    dmx_profile.clone(),
                ),
            ));
        }
    }
}

define_collect_helper!("Relation" (serialize_relations, deserialize_relations) -> Relation);

/// Describes a dependency between a DMX channel and a channel function.
///
/// Corresponds to a `<Relation>` XML node.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Relation {
    /// The unique name of the relation.
    ///
    /// Corresponds to the `Name` XML attribute.
    #[serde(rename = "@Name", skip_serializing_if = "Option::is_none")]
    pub name: Option<Name>,

    /// Link to the master DMX channel.
    ///
    /// Corresponds to the `Master` XML attribute.
    #[serde(rename = "@Master")]
    pub master: Node,

    /// Link to the following channel function.
    ///
    /// Corresponds to the `Follower` XML attribute.
    #[serde(rename = "@Follower")]
    pub follower: Node,

    /// Type of the relation.
    ///
    /// Corresponds to the `Type` XML attribute.
    #[serde(rename = "@Type")]
    pub type_: RelationType,
}

impl Relation {
    /// Looks up the master [DmxChannel] linked by this relation.
    pub fn master<'s>(&self, parent_dmx_mode: &'s DmxMode) -> Option<&'s DmxChannel> {
        let name = self.master.single()?;
        parent_dmx_mode.dmx_channel(name)
    }

    /// Looks up the follower [ChannelFunction] linked by this relation.
    pub fn follower<'s>(
        &self,
        parent_dmx_mode: &'s DmxMode,
    ) -> Option<(&'s DmxChannel, &'s LogicalChannel, &'s ChannelFunction)> {
        let (channel_name, tail) = self.follower.split_first()?;
        let channel = parent_dmx_mode.dmx_channel(channel_name)?;

        let (logical_name, tail) = tail.split_first()?;
        let logical = channel.logical_channel(logical_name)?;

        let func_name = tail.single()?;
        let func = logical.channel_function(func_name)?;

        Some((channel, logical, func))
    }

    /// Performs validation checks on the object.
    pub fn validate(&self, parent_dmx_mode: &DmxMode, result: &mut ValidationResult) {
        if self.name.is_none() {
            result.errors.push(ValidationError::new(
                ValidationObject::Relation,
                None,
                ValidationErrorType::MissingName,
            ));
        }

        let name = self.name.as_ref();

        if self.master(parent_dmx_mode).is_none() {
            result.errors.push(ValidationError::new(
                ValidationObject::Relation,
                name.map(Name::to_string),
                ValidationErrorType::LinkNotFound(
                    ValidationObject::DmxChannel,
                    self.master.clone(),
                ),
            ));
        }

        if self.follower(parent_dmx_mode).is_none() {
            result.errors.push(ValidationError::new(
                ValidationObject::Relation,
                name.map(Name::to_string),
                ValidationErrorType::LinkNotFound(
                    ValidationObject::ChannelFunction,
                    self.follower.clone(),
                ),
            ));
        }
    }
}

/// Type of relation described in a [Relation].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum RelationType {
    /// Master multiplies the value of the follower.
    Multiply,

    /// Master overrides the value of the follower.
    Override,
}

define_collect_helper!("FTMacro" (serialize_ft_macros, deserialize_ft_macros) -> FtMacro);

/// Describes a macro to be executed by the control system.
///
/// A macro is made up of several sequences of DMX values.
///
/// Corresponds to an `<FTMacro>` XML node.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FtMacro {
    /// The unique name of the macro.
    ///
    /// Corresponds to the `Name` XML attribute.
    #[serde(rename = "@Name", skip_serializing_if = "Option::is_none")]
    pub name: Option<Name>,

    /// Optional link to a channel function.
    ///
    /// Corresponds to the `ChannelFunction` XML attribute.
    #[serde(rename = "@ChannelFunction", skip_serializing_if = "Option::is_none")]
    pub channel_function: Option<Node>,

    /// A list of DMX sequences.
    ///
    /// Corresponds to all `<MacroDMX>` XML child nodes.
    #[serde(rename = "MacroDMX", skip_serializing_if = "Vec::is_empty", default)]
    pub dmx: Vec<MacroDmx>,
}

impl FtMacro {
    /// Looks up the [ChannelFunction] linked by this macro.
    pub fn channel_function<'s>(
        &self,
        parent_dmx_mode: &'s DmxMode,
    ) -> Option<(&'s DmxChannel, &'s LogicalChannel, &'s ChannelFunction)> {
        let (channel_name, tail) = self.channel_function.as_ref()?.split_first()?;
        let channel = parent_dmx_mode.dmx_channel(channel_name)?;

        let (logical_name, tail) = tail.split_first()?;
        let logical = channel.logical_channel(logical_name)?;

        let function_name = tail.single()?;
        let function = logical.channel_function(function_name)?;

        Some((channel, logical, function))
    }

    /// Performs validation checks on the object.
    pub fn validate(&self, parent_dmx_mode: &DmxMode, result: &mut ValidationResult) {
        if self.name.is_none() {
            result.errors.push(ValidationError::new(
                ValidationObject::FtMacro,
                None,
                ValidationErrorType::MissingName,
            ));
        }

        let name = self.name.as_ref();

        if let (Some(channel_function), None) = (
            &self.channel_function,
            self.channel_function(parent_dmx_mode),
        ) {
            result.errors.push(ValidationError::new(
                ValidationObject::FtMacro,
                name.map(Name::to_string),
                ValidationErrorType::LinkNotFound(
                    ValidationObject::ChannelFunction,
                    channel_function.clone(),
                ),
            ));
        }

        let dmx_values = self
            .dmx
            .iter()
            .flat_map(|dmx| dmx.steps.iter())
            .flat_map(|step| step.values.iter());
        for dmx_value in dmx_values {
            if dmx_value.dmx_channel(parent_dmx_mode).is_none() {
                result.errors.push(ValidationError::new(
                    ValidationObject::FtMacro,
                    name.map(Name::to_string),
                    ValidationErrorType::LinkNotFound(
                        ValidationObject::DmxChannel,
                        dmx_value.dmx_channel.clone(),
                    ),
                ));
            }
        }
    }
}

/// Describes an individual sequence of DMX values in an [FtMacro].
///
/// Corresponds to a `<MacroDMX>` XML node.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MacroDmx {
    /// The steps in the sequence.
    ///
    /// Corresponds to all `<MacroDMXStep>` XML child nodes.
    #[serde(
        rename = "MacroDMXStep",
        skip_serializing_if = "Vec::is_empty",
        default
    )]
    pub steps: Vec<MacroDmxStep>,
}

/// Describes a step in a [MacroDmx] sequence.
///
/// Corresponds to a `<MacroDMXStep>` XML node.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MacroDmxStep {
    /// Duration of the step in seconds.
    ///
    /// Corresponds to the `Duration` XML attribute.
    #[serde(rename = "@Duration", default = "default_macro_dmx_step_duration")]
    pub duration: f64,

    /// Values to apply in this step.
    ///
    /// Corresponds to all `<MacroDMXValue>` XML child nodes.
    #[serde(
        rename = "MacroDMXValue",
        skip_serializing_if = "Vec::is_empty",
        default
    )]
    pub values: Vec<MacroDmxValue>,
}

fn default_macro_dmx_step_duration() -> f64 {
    1.
}

/// Describes the value for a DMX channel in a [MacroDmxStep].
///
/// Corresponds to a `<MacroDMXValue>` XML node.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MacroDmxValue {
    /// Value of the DMX channel.
    ///
    /// Corresponds to the `Value` XML attribute.
    #[serde(rename = "@Value")]
    pub value: DmxValue,

    /// Link to the DMX channel.
    ///
    /// Corresponds to the `DMXChannel` XML attribute.
    #[serde(rename = "@DMXChannel")]
    pub dmx_channel: Node,
}

impl MacroDmxValue {
    /// Looks up the [DmxChannel] linked by this macro.
    pub fn dmx_channel<'s>(&self, parent_dmx_mode: &'s DmxMode) -> Option<&'s DmxChannel> {
        let name = self.dmx_channel.single()?;
        parent_dmx_mode.dmx_channel(name)
    }
}