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
//! This module includes a high level abstraction over a DICOM data element's value.

use crate::header::{EmptyObject, HasLength, Length, Tag};
use num_traits::NumCast;
use smallvec::SmallVec;
use std::{borrow::Cow, str::FromStr};

pub mod deserialize;
pub mod fragments;
pub mod partial;
pub mod person_name;
mod primitive;
pub mod range;
pub mod serialize;

pub use self::deserialize::Error as DeserializeError;
pub use self::partial::{DicomDate, DicomDateTime, DicomTime, PreciseDateTime};
pub use self::person_name::PersonName;
pub use self::range::{AsRange, DateRange, DateTimeRange, TimeRange};

pub use self::primitive::{
    CastValueError, ConvertValueError, InvalidValueReadError, ModifyValueError, PrimitiveValue,
    ValueType,
};

/// An aggregation of one or more elements in a value.
pub type C<T> = SmallVec<[T; 2]>;

/// Type alias for the in-memory pixel data fragment data.
pub type InMemFragment = Vec<u8>;

/// A trait for a value that maps to a DICOM element data value.
pub trait DicomValueType: HasLength {
    /// Retrieve the specific type of this value.
    fn value_type(&self) -> ValueType;

    /// Retrieve the number of elements contained in the DICOM value.
    ///
    /// In a sequence value, this is the number of items in the sequence.
    /// In an encapsulated pixel data sequence, the output is always 1.
    /// Otherwise, the output is the number of elements effectively encoded
    /// in the value.
    fn cardinality(&self) -> usize;
}

/// Representation of a full DICOM value, which may be either primitive or
/// another DICOM object.
///
/// `I` is the complex type for nest data set items, which should usually
/// implement [`HasLength`].
/// `P` is the encapsulated pixel data provider,
/// which should usually implement `AsRef<[u8]>`.
#[derive(Debug, Clone, PartialEq)]
pub enum Value<I = EmptyObject, P = InMemFragment> {
    /// Primitive value.
    Primitive(PrimitiveValue),
    /// A complex sequence of items.
    Sequence(DataSetSequence<I>),
    /// A sequence of encapsulated pixel data fragments.
    PixelSequence(PixelFragmentSequence<P>),
}

impl<P> Value<EmptyObject, P> {
    /// Construct an isolated DICOM pixel sequence sequence value
    /// from a basic offset table and a list of fragments.
    ///
    /// This function will define the data set sequence item type `I`
    /// to an empty object ([`EmptyObject`]),
    /// so that it can be used more easily in isolation.
    /// As a consequence, it cannot be directly combined with
    /// DICOM objects that may contain sequence values.
    /// To let the type parameter `I` be inferred from its context,
    /// create a [`PixelFragmentSequence`] and use `Value::from` instead.
    ///
    /// **Note:** This function does not validate the offset table
    /// against the fragments.
    pub fn new_pixel_sequence<T>(offset_table: C<u32>, fragments: T) -> Self
    where
        T: Into<C<P>>,
    {
        Value::from(PixelFragmentSequence::new(offset_table, fragments))
    }
}

impl<I> Value<I> {
    /// Construct an isolated DICOM data set sequence value
    /// from a list of items and length.
    ///
    /// This function will define the pixel data fragment type parameter `P`
    /// to the `Value` type's default ([`InMemFragment`]),
    /// so that it can be used more easily.
    /// If necessary,
    /// it is possible to let this type parameter be inferred from its context
    /// by creating a [`DataSetSequence`] and using `Value::from` instead.
    #[inline]
    pub fn new_sequence<T>(items: T, length: Length) -> Self
    where
        T: Into<C<I>>,
    {
        Self::from(DataSetSequence::new(items, length))
    }
}

impl Value {
    /// Construct a DICOM value from a primitive value.
    ///
    /// This is equivalent to `Value::from` in behavior,
    /// except that suitable type parameters are specified
    /// instead of inferred.
    ///
    /// This function will automatically define
    /// the sequence item parameter `I`
    /// to [`EmptyObject`]
    /// and the pixel data fragment type parameter `P`
    /// to the default fragment data type ([`InMemFragment`]),
    /// so that it can be used more easily in isolation.
    /// As a consequence, it cannot be directly combined with
    /// DICOM objects that may contain
    /// nested data sets or encapsulated pixel data.
    /// To let the type parameters `I` and `P` be inferred from their context,
    /// create a value of one of the types and use `Value::from` instead.
    ///
    /// - [`PrimitiveValue`]
    /// - [`PixelFragmentSequence`]
    /// - [`DataSetSequence`]
    #[inline]
    pub fn new(value: PrimitiveValue) -> Self {
        Self::from(value)
    }
}

impl<I, P> Value<I, P> {
    /// Obtain the number of individual values.
    /// In a primitive, this is the number of individual elements in the value.
    /// In a sequence item, this is the number of items.
    /// In a pixel sequence, this is currently set to 1
    /// regardless of the number of compressed fragments or frames.
    pub fn multiplicity(&self) -> u32 {
        match self {
            Value::Primitive(v) => v.multiplicity(),
            Value::Sequence(v) => v.multiplicity(),
            Value::PixelSequence(..) => 1,
        }
    }

    /// Gets a reference to the primitive value.
    pub fn primitive(&self) -> Option<&PrimitiveValue> {
        match self {
            Value::Primitive(v) => Some(v),
            _ => None,
        }
    }

    /// Gets a mutable reference to the primitive value.
    pub fn primitive_mut(&mut self) -> Option<&mut PrimitiveValue> {
        match self {
            Value::Primitive(v) => Some(v),
            _ => None,
        }
    }

    /// Gets a reference to the items of a sequence.
    ///
    /// Returns `None` if the value is not a data set sequence.
    pub fn items(&self) -> Option<&[I]> {
        match self {
            Value::Sequence(v) => Some(v.items()),
            _ => None,
        }
    }

    /// Gets a mutable reference to the items of a sequence.
    ///
    /// Returns `None` if the value is not a data set sequence.
    pub fn items_mut(&mut self) -> Option<&mut C<I>> {
        match self {
            Value::Sequence(v) => Some(v.items_mut()),
            _ => None,
        }
    }

    /// Gets a reference to the fragments of a pixel data sequence.
    ///
    /// Returns `None` if the value is not a pixel data sequence.
    pub fn fragments(&self) -> Option<&[P]> {
        match self {
            Value::PixelSequence(v) => Some(v.fragments()),
            _ => None,
        }
    }

    /// Gets a mutable reference to the fragments of a pixel data sequence.
    ///
    /// Returns `None` if the value is not a pixel data sequence.
    pub fn fragments_mut(&mut self) -> Option<&mut C<P>> {
        match self {
            Value::PixelSequence(v) => Some(v.fragments_mut()),
            _ => None,
        }
    }

    /// Retrieves the primitive value.
    pub fn into_primitive(self) -> Option<PrimitiveValue> {
        match self {
            Value::Primitive(v) => Some(v),
            _ => None,
        }
    }

    /// Retrieves the data set items,
    /// discarding the recorded length information.
    ///
    /// Returns `None` if the value is not a data set sequence.
    pub fn into_items(self) -> Option<C<I>> {
        match self {
            Value::Sequence(v) => Some(v.into_items()),
            _ => None,
        }
    }

    /// Retrieves the pixel data fragments,
    /// discarding the rest of the information.
    pub fn into_fragments(self) -> Option<C<P>> {
        match self {
            Value::PixelSequence(v) => Some(v.into_fragments()),
            _ => None,
        }
    }

    /// Gets a reference to the encapsulated pixel data's offset table.
    ///
    /// Returns `None` if the value is not a pixel data sequence.
    pub fn offset_table(&self) -> Option<&[u32]> {
        match self {
            Value::PixelSequence(v) => Some(v.offset_table()),
            _ => None,
        }
    }

    /// Gets a mutable reference to the encapsulated pixel data's offset table.
    ///
    /// Returns `None` if the value is not a pixel data sequence.
    pub fn offset_table_mut(&mut self) -> Option<&mut C<u32>> {
        match self {
            Value::PixelSequence(v) => Some(v.offset_table_mut()),
            _ => None,
        }
    }

    /// Shorten this value by removing trailing elements
    /// to fit the given limit.
    ///
    /// On primitive values,
    /// elements are counted by the number of individual value items
    /// (note that bytes in a [`PrimitiveValue::U8`]
    /// are treated as individual items).
    /// On data set sequences and pixel data fragment sequences,
    /// this operation is applied to
    /// the data set items (or fragments) in the sequence.
    ///
    /// Nothing is done if the value's cardinality
    /// is already lower than or equal to the limit.
    pub fn truncate(&mut self, limit: usize) {
        match self {
            Value::Primitive(v) => v.truncate(limit),
            Value::Sequence(v) => v.truncate(limit),
            Value::PixelSequence(v) => v.truncate(limit),
        }
    }
}

impl<I, P> From<&str> for Value<I, P> {
    /// Converts a string into a primitive textual value.
    fn from(value: &str) -> Self {
        Value::Primitive(PrimitiveValue::from(value))
    }
}

impl<I, P> From<String> for Value<I, P> {
    /// Converts a string into a primitive textual value.
    fn from(value: String) -> Self {
        Value::Primitive(PrimitiveValue::from(value))
    }
}

impl<I, P> HasLength for Value<I, P> {
    fn length(&self) -> Length {
        match self {
            Value::Primitive(v) => v.length(),
            Value::Sequence(v) => v.length(),
            Value::PixelSequence(v) => v.length(),
        }
    }
}

impl<I, P> DicomValueType for Value<I, P> {
    fn value_type(&self) -> ValueType {
        match self {
            Value::Primitive(v) => v.value_type(),
            Value::Sequence(..) => ValueType::DataSetSequence,
            Value::PixelSequence(..) => ValueType::PixelSequence,
        }
    }

    fn cardinality(&self) -> usize {
        match self {
            Value::Primitive(v) => v.cardinality(),
            Value::Sequence(DataSetSequence { items, .. }) => items.len(),
            Value::PixelSequence { .. } => 1,
        }
    }
}

impl<I, P> Value<I, P>
where
    I: HasLength,
{
    /// Convert the full primitive value into a clean string.
    ///
    /// The value is converted into a strings
    /// as described in [`PrimitiveValue::to_str`].
    /// If the value contains multiple strings,
    /// they are trimmed at the end and concatenated
    /// (separated by the standard DICOM value delimiter `'\\'`)
    /// into an owned string.
    ///
    /// Returns an error if the value is not primitive.
    pub fn to_str(&self) -> Result<Cow<str>, ConvertValueError> {
        match self {
            Value::Primitive(prim) => Ok(prim.to_str()),
            _ => Err(ConvertValueError {
                requested: "string",
                original: self.value_type(),
                cause: None,
            }),
        }
    }

    /// Convert the full primitive value into a single raw string,
    /// with trailing whitespace kept.
    ///
    /// If the value contains multiple strings, they are concatenated
    /// (separated by the standard DICOM value delimiter `'\\'`)
    /// into an owned string.
    ///
    /// Returns an error if the value is not primitive.
    pub fn to_raw_str(&self) -> Result<Cow<str>, ConvertValueError> {
        match self {
            Value::Primitive(prim) => Ok(prim.to_raw_str()),
            _ => Err(ConvertValueError {
                requested: "string",
                original: self.value_type(),
                cause: None,
            }),
        }
    }

    /// Convert the full primitive value into a sequence of strings.
    ///
    /// If the value is a primitive, it will be converted into
    /// a vector of strings as described in [`PrimitiveValue::to_multi_str`].
    ///
    /// Returns an error if the value is not primitive.
    ///
    /// [`PrimitiveValue::to_multi_str`]: ../enum.PrimitiveValue.html#to_multi_str
    pub fn to_multi_str(&self) -> Result<Cow<[String]>, CastValueError> {
        match self {
            Value::Primitive(prim) => Ok(prim.to_multi_str()),
            _ => Err(CastValueError {
                requested: "string",
                got: self.value_type(),
            }),
        }
    }

    /// Convert the full primitive value into raw bytes.
    ///
    /// String values already encoded with the `Str` and `Strs` variants
    /// are provided in UTF-8.
    ///
    /// Returns an error if the value is not primitive.
    pub fn to_bytes(&self) -> Result<Cow<[u8]>, ConvertValueError> {
        match self {
            Value::Primitive(prim) => Ok(prim.to_bytes()),
            _ => Err(ConvertValueError {
                requested: "bytes",
                original: self.value_type(),
                cause: None,
            }),
        }
    }

    /// Retrieve and convert the primitive value into an integer.
    ///
    /// If the value is a primitive, it will be converted into
    /// an integer as described in [`PrimitiveValue::to_int`].
    ///
    /// [`PrimitiveValue::to_int`]: ../enum.PrimitiveValue.html#to_int
    pub fn to_int<T>(&self) -> Result<T, ConvertValueError>
    where
        T: Clone,
        T: NumCast,
        T: FromStr<Err = std::num::ParseIntError>,
    {
        match self {
            Value::Primitive(v) => v.to_int::<T>(),
            _ => Err(ConvertValueError {
                requested: "integer",
                original: self.value_type(),
                cause: None,
            }),
        }
    }

    /// Retrieve and convert the primitive value into a sequence of integers.
    ///
    /// If the value is a primitive, it will be converted into
    /// a vector of integers as described in [PrimitiveValue::to_multi_int].
    ///
    /// [PrimitiveValue::to_multi_int]: ../enum.PrimitiveValue.html#to_multi_int
    pub fn to_multi_int<T>(&self) -> Result<Vec<T>, ConvertValueError>
    where
        T: Clone,
        T: NumCast,
        T: FromStr<Err = std::num::ParseIntError>,
    {
        match self {
            Value::Primitive(v) => v.to_multi_int::<T>(),
            _ => Err(ConvertValueError {
                requested: "integer",
                original: self.value_type(),
                cause: None,
            }),
        }
    }

    /// Retrieve and convert the primitive value
    /// into a single-precision floating point number.
    ///
    /// If the value is a primitive, it will be converted into
    /// a number as described in [`PrimitiveValue::to_float32`].
    ///
    /// [`PrimitiveValue::to_float32`]: ../enum.PrimitiveValue.html#to_float32
    pub fn to_float32(&self) -> Result<f32, ConvertValueError> {
        match self {
            Value::Primitive(v) => v.to_float32(),
            _ => Err(ConvertValueError {
                requested: "float32",
                original: self.value_type(),
                cause: None,
            }),
        }
    }

    /// Retrieve and convert the primitive value
    /// into a sequence of single-precision floating point numbers.
    ///
    /// If the value is a primitive, it will be converted into
    /// a vector of numbers as described in [`PrimitiveValue::to_multi_float32`].
    ///
    /// [`PrimitiveValue::to_multi_float32`]: ../enum.PrimitiveValue.html#to_multi_float32
    pub fn to_multi_float32(&self) -> Result<Vec<f32>, ConvertValueError> {
        match self {
            Value::Primitive(v) => v.to_multi_float32(),
            _ => Err(ConvertValueError {
                requested: "float32",
                original: self.value_type(),
                cause: None,
            }),
        }
    }

    /// Retrieve and convert the primitive value
    /// into a double-precision floating point number.
    ///
    /// If the value is a primitive, it will be converted into
    /// a number as described in [`PrimitiveValue::to_float64`].
    ///
    /// [`PrimitiveValue::to_float64`]: ../enum.PrimitiveValue.html#to_float64
    pub fn to_float64(&self) -> Result<f64, ConvertValueError> {
        match self {
            Value::Primitive(v) => v.to_float64(),
            _ => Err(ConvertValueError {
                requested: "float64",
                original: self.value_type(),
                cause: None,
            }),
        }
    }

    /// Retrieve and convert the primitive value
    /// into a sequence of double-precision floating point numbers.
    ///
    /// If the value is a primitive, it will be converted into
    /// a vector of numbers as described in [`PrimitiveValue::to_multi_float64`].
    ///
    /// [`PrimitiveValue::to_multi_float64`]: ../enum.PrimitiveValue.html#to_multi_float64
    pub fn to_multi_float64(&self) -> Result<Vec<f64>, ConvertValueError> {
        match self {
            Value::Primitive(v) => v.to_multi_float64(),
            _ => Err(ConvertValueError {
                requested: "float64",
                original: self.value_type(),
                cause: None,
            }),
        }
    }

    /// Retrieve and convert the primitive value into a `DicomDate`.
    ///
    /// If the value is a primitive, it will be converted into
    /// a `DicomDate` as described in [`PrimitiveValue::to_date`].
    ///
    pub fn to_date(&self) -> Result<DicomDate, ConvertValueError> {
        match self {
            Value::Primitive(v) => v.to_date(),
            _ => Err(ConvertValueError {
                requested: "DicomDate",
                original: self.value_type(),
                cause: None,
            }),
        }
    }

    /// Retrieve and convert the primitive value into a sequence of `DicomDate`s.
    ///
    /// If the value is a primitive, it will be converted into
    /// a vector of `DicomDate` as described in [`PrimitiveValue::to_multi_date`].
    ///
    pub fn to_multi_date(&self) -> Result<Vec<DicomDate>, ConvertValueError> {
        match self {
            Value::Primitive(v) => v.to_multi_date(),
            _ => Err(ConvertValueError {
                requested: "DicomDate",
                original: self.value_type(),
                cause: None,
            }),
        }
    }

    /// Retrieve and convert the primitive value into a `DicomTime`.
    ///
    /// If the value is a primitive, it will be converted into
    /// a `DicomTime` as described in [`PrimitiveValue::to_time`].
    ///
    pub fn to_time(&self) -> Result<DicomTime, ConvertValueError> {
        match self {
            Value::Primitive(v) => v.to_time(),
            _ => Err(ConvertValueError {
                requested: "DicomTime",
                original: self.value_type(),
                cause: None,
            }),
        }
    }

    /// Retrieve and convert the primitive value into a sequence of `DicomTime`s.
    ///
    /// If the value is a primitive, it will be converted into
    /// a vector of `DicomTime` as described in [`PrimitiveValue::to_multi_time`].
    ///
    pub fn to_multi_time(&self) -> Result<Vec<DicomTime>, ConvertValueError> {
        match self {
            Value::Primitive(v) => v.to_multi_time(),
            _ => Err(ConvertValueError {
                requested: "DicomTime",
                original: self.value_type(),
                cause: None,
            }),
        }
    }

    /// Retrieve and convert the primitive value into a `DicomDateTime`.
    ///
    /// If the value is a primitive, it will be converted into
    /// a `DateTime` as described in [`PrimitiveValue::to_datetime`].
    ///
    pub fn to_datetime(&self) -> Result<DicomDateTime, ConvertValueError> {
        match self {
            Value::Primitive(v) => v.to_datetime(),
            _ => Err(ConvertValueError {
                requested: "DicomDateTime",
                original: self.value_type(),
                cause: None,
            }),
        }
    }

    /// Retrieve and convert the primitive value into a sequence of `DicomDateTime`s.
    ///
    /// If the value is a primitive, it will be converted into
    /// a vector of `DicomDateTime` as described in [`PrimitiveValue::to_multi_datetime`].
    ///
    pub fn to_multi_datetime(&self) -> Result<Vec<DicomDateTime>, ConvertValueError> {
        match self {
            Value::Primitive(v) => v.to_multi_datetime(),
            _ => Err(ConvertValueError {
                requested: "DicomDateTime",
                original: self.value_type(),
                cause: None,
            }),
        }
    }

    /// Retrieve and convert the primitive value into a `DateRange`.
    ///
    /// If the value is a primitive, it will be converted into
    /// a `DateRange` as described in [`PrimitiveValue::to_date_range`].
    ///
    pub fn to_date_range(&self) -> Result<DateRange, ConvertValueError> {
        match self {
            Value::Primitive(v) => v.to_date_range(),
            _ => Err(ConvertValueError {
                requested: "DateRange",
                original: self.value_type(),
                cause: None,
            }),
        }
    }

    /// Retrieve and convert the primitive value into a `TimeRange`.
    ///
    /// If the value is a primitive, it will be converted into
    /// a `TimeRange` as described in [`PrimitiveValue::to_time_range`].
    ///
    pub fn to_time_range(&self) -> Result<TimeRange, ConvertValueError> {
        match self {
            Value::Primitive(v) => v.to_time_range(),
            _ => Err(ConvertValueError {
                requested: "TimeRange",
                original: self.value_type(),
                cause: None,
            }),
        }
    }

    /// Retrieve and convert the primitive value into a `DateTimeRange`.
    ///
    /// If the value is a primitive, it will be converted into
    /// a `DateTimeRange` as described in [`PrimitiveValue::to_datetime_range`].
    ///
    pub fn to_datetime_range(&self) -> Result<DateTimeRange, ConvertValueError> {
        match self {
            Value::Primitive(v) => v.to_datetime_range(),
            _ => Err(ConvertValueError {
                requested: "DateTimeRange",
                original: self.value_type(),
                cause: None,
            }),
        }
    }

    /// Retrieves the primitive value as a DICOM tag.
    pub fn to_tag(&self) -> Result<Tag, CastValueError> {
        match self {
            Value::Primitive(PrimitiveValue::Tags(v)) => Ok(v[0]),
            _ => Err(CastValueError {
                requested: "tag",
                got: self.value_type(),
            }),
        }
    }

    /// Retrieves the primitive value as a [`PersonName`].
    pub fn to_person_name(&self) -> Result<PersonName<'_>, ConvertValueError> {
        match self {
            Value::Primitive(v) => v.to_person_name(),
            _ => Err(ConvertValueError {
                requested: "PersonName",
                original: self.value_type(),
                cause: None,
            }),
        }
    }
}

/// Macro for implementing getters to single and multi-values,
/// by delegating to `PrimitiveValue`.
///
/// Should be placed inside `Value`'s impl block.
macro_rules! impl_primitive_getters {
    ($name_single: ident, $name_multi: ident, $variant: ident, $ret: ty) => {
        /// Get a single value of the requested type.
        ///
        /// If it contains multiple values,
        /// only the first one is returned.
        /// An error is returned if the variant is not compatible.
        pub fn $name_single(&self) -> Result<$ret, CastValueError> {
            match self {
                Value::Primitive(v) => v.$name_single(),
                value => Err(CastValueError {
                    requested: stringify!($name_single),
                    got: value.value_type(),
                }),
            }
        }

        /// Get a sequence of values of the requested type without copying.
        ///
        /// An error is returned if the variant is not compatible.
        pub fn $name_multi(&self) -> Result<&[$ret], CastValueError> {
            match self {
                Value::Primitive(v) => v.$name_multi(),
                value => Err(CastValueError {
                    requested: stringify!($name_multi),
                    got: value.value_type(),
                }),
            }
        }
    };
}

impl<I, P> Value<I, P> {
    /// Get a single string value.
    ///
    /// If it contains multiple strings,
    /// only the first one is returned.
    ///
    /// An error is returned if the variant is not compatible.
    ///
    /// To enable conversions of other variants to a textual representation,
    /// see [`to_str()`] instead.
    ///
    /// [`to_str()`]: #method.to_str
    pub fn string(&self) -> Result<&str, CastValueError> {
        match self {
            Value::Primitive(v) => v.string(),
            _ => Err(CastValueError {
                requested: "string",
                got: self.value_type(),
            }),
        }
    }

    /// Get the inner sequence of string values
    /// if the variant is either `Str` or `Strs`.
    ///
    /// An error is returned if the variant is not compatible.
    ///
    /// To enable conversions of other variants to a textual representation,
    /// see [`to_str()`] instead.
    ///
    /// [`to_str()`]: #method.to_str
    pub fn strings(&self) -> Result<&[String], CastValueError> {
        match self {
            Value::Primitive(v) => v.strings(),
            _ => Err(CastValueError {
                requested: "strings",
                got: self.value_type(),
            }),
        }
    }

    impl_primitive_getters!(tag, tags, Tags, Tag);
    impl_primitive_getters!(date, dates, Date, DicomDate);
    impl_primitive_getters!(time, times, Time, DicomTime);
    impl_primitive_getters!(datetime, datetimes, DateTime, DicomDateTime);
    impl_primitive_getters!(uint8, uint8_slice, U8, u8);
    impl_primitive_getters!(uint16, uint16_slice, U16, u16);
    impl_primitive_getters!(int16, int16_slice, I16, i16);
    impl_primitive_getters!(uint32, uint32_slice, U32, u32);
    impl_primitive_getters!(int32, int32_slice, I32, i32);
    impl_primitive_getters!(int64, int64_slice, I64, i64);
    impl_primitive_getters!(uint64, uint64_slice, U64, u64);
    impl_primitive_getters!(float32, float32_slice, F32, f32);
    impl_primitive_getters!(float64, float64_slice, F64, f64);
}

impl<I, P> From<PrimitiveValue> for Value<I, P> {
    fn from(v: PrimitiveValue) -> Self {
        Value::Primitive(v)
    }
}

/// A sequence of complex data set items of type `I`.
#[derive(Debug, Clone)]
pub struct DataSetSequence<I> {
    /// The item sequence.
    items: C<I>,
    /// The sequence length in bytes.
    ///
    /// The value may be [`UNDEFINED`](Length::UNDEFINED)
    /// if the length is implicitly defined,
    /// otherwise it should match the full byte length of all items.
    length: Length,
}

impl<I> DataSetSequence<I> {
    /// Construct a DICOM data sequence
    /// using a sequence of items and a length.
    ///
    /// **Note:** This function does not validate the `length`
    /// against the items.
    /// When not sure,
    /// `length` can be set to [`UNDEFINED`](Length::UNDEFINED)
    /// to leave it as implicitly defined.
    #[inline]
    pub fn new(items: impl Into<C<I>>, length: Length) -> Self {
        DataSetSequence {
            items: items.into(),
            length,
        }
    }

    /// Construct an empty DICOM data sequence,
    /// with the length explicitly defined to zero.
    #[inline]
    pub fn empty() -> Self {
        DataSetSequence {
            items: Default::default(),
            length: Length(0),
        }
    }

    /// Gets a reference to the items of a sequence.
    #[inline]
    pub fn items(&self) -> &[I] {
        &self.items
    }

    /// Gets a mutable reference to the items of a sequence.
    #[inline]
    pub fn items_mut(&mut self) -> &mut C<I> {
        &mut self.items
    }

    /// Obtain the number of items in the sequence.
    #[inline]
    pub fn multiplicity(&self) -> u32 {
        self.items.len() as u32
    }

    /// Retrieve the sequence of items,
    /// discarding the recorded length information.
    #[inline]
    pub fn into_items(self) -> C<I> {
        self.items
    }

    /// Get the value data's length
    /// as specified by the sequence's data element,
    /// in bytes.
    ///
    /// This is equivalent to [`HasLength::length`].
    #[inline]
    pub fn length(&self) -> Length {
        HasLength::length(self)
    }

    /// Shorten this sequence by removing trailing data set items
    /// to fit the given limit.
    #[inline]
    pub fn truncate(&mut self, limit: usize) {
        self.items.truncate(limit);
    }
}

impl<I> HasLength for DataSetSequence<I> {
    #[inline]
    fn length(&self) -> Length {
        self.length
    }
}

impl<I> DicomValueType for DataSetSequence<I> {
    #[inline]
    fn value_type(&self) -> ValueType {
        ValueType::DataSetSequence
    }

    #[inline]
    fn cardinality(&self) -> usize {
        self.items.len()
    }
}

impl<I> From<Vec<I>> for DataSetSequence<I> {
    /// Converts a vector of items
    /// into a data set sequence with an undefined length.
    #[inline]
    fn from(items: Vec<I>) -> Self {
        DataSetSequence {
            items: items.into(),
            length: Length::UNDEFINED,
        }
    }
}

impl<A, I> From<SmallVec<A>> for DataSetSequence<I>
where
    A: smallvec::Array<Item = I>,
    C<I>: From<SmallVec<A>>,
{
    /// Converts a smallvec of items
    /// into a data set sequence with an undefined length.
    #[inline]
    fn from(items: SmallVec<A>) -> Self {
        DataSetSequence {
            items: items.into(),
            length: Length::UNDEFINED,
        }
    }
}

impl<I> From<[I; 1]> for DataSetSequence<I> {
    /// Constructs a data set sequence with a single item
    /// and an undefined length.
    #[inline]
    fn from([item]: [I; 1]) -> Self {
        DataSetSequence {
            items: smallvec::smallvec![item],
            length: Length::UNDEFINED,
        }
    }
}

impl<I, P> From<DataSetSequence<I>> for Value<I, P> {
    #[inline]
    fn from(value: DataSetSequence<I>) -> Self {
        Value::Sequence(value)
    }
}

impl<I> PartialEq<DataSetSequence<I>> for DataSetSequence<I>
where
    I: PartialEq,
{
    /// This method tests for `self` and `other` values to be equal,
    /// and is used by `==`.
    ///
    /// This implementation only checks for item equality,
    /// disregarding the byte length.
    #[inline]
    fn eq(&self, other: &DataSetSequence<I>) -> bool {
        self.items() == other.items()
    }
}

/// A sequence of pixel data fragments.
///
/// Each fragment (of data type `P`) is
/// an even-lengthed sequence of bytes
/// representing the encoded pixel data.
/// The first item of the sequence is interpreted as a basic offset table,
/// which is defined separately.
#[derive(Debug, Clone, PartialEq)]
pub struct PixelFragmentSequence<P> {
    /// The value contents of the basic offset table.
    offset_table: C<u32>,
    /// The sequence of pixel data fragments.
    fragments: C<P>,
}

impl<P> PixelFragmentSequence<P> {
    /// Construct a DICOM pixel sequence sequence value
    /// from a basic offset table and a list of fragments.
    ///
    /// **Note:** This function does not validate the offset table
    /// against the given fragments.
    #[inline]
    pub fn new(offset_table: impl Into<C<u32>>, fragments: impl Into<C<P>>) -> Self {
        PixelFragmentSequence {
            offset_table: offset_table.into(),
            fragments: fragments.into(),
        }
    }

    /// Construct a DICOM pixel sequence sequence value
    /// from a list of fragments,
    /// with an empty basic offset table.
    #[inline]
    pub fn new_fragments(fragments: impl Into<C<P>>) -> Self {
        PixelFragmentSequence {
            offset_table: Default::default(),
            fragments: fragments.into(),
        }
    }

    /// Gets a reference to the pixel data fragments.
    ///
    /// This sequence does not include the offset table.
    #[inline]
    pub fn fragments(&self) -> &[P] {
        &self.fragments
    }

    /// Gets a mutable reference to the pixel data fragments.
    ///
    /// This sequence does not include the offset table.
    #[inline]
    pub fn fragments_mut(&mut self) -> &mut C<P> {
        &mut self.fragments
    }

    /// Retrieve the pixel data fragments,
    /// discarding the rest of the information.
    ///
    /// This sequence does not include the offset table.
    #[inline]
    pub fn into_fragments(self) -> C<P> {
        self.fragments
    }

    /// Decompose the sequence into its constituent parts:
    /// the basic offset table and the pixel data fragments.
    pub fn into_parts(self) -> (C<u32>, C<P>) {
        (self.offset_table, self.fragments)
    }

    /// Gets a reference to the encapsulated pixel data's offset table.
    pub fn offset_table(&self) -> &[u32] {
        &self.offset_table
    }

    /// Gets a mutable reference to the encapsulated pixel data's offset table.
    pub fn offset_table_mut(&mut self) -> &mut C<u32> {
        &mut self.offset_table
    }

    /// Get the value data's length
    /// as specified by the sequence's data element,
    /// in bytes.
    ///
    /// This is equivalent to [`HasLength::length`].
    #[inline]
    pub fn length(&self) -> Length {
        HasLength::length(self)
    }

    /// Shorten this sequence by removing trailing fragments
    /// to fit the given limit.
    ///
    /// Note that this operations does not affect the basic offset table.
    #[inline]
    pub fn truncate(&mut self, limit: usize) {
        self.fragments.truncate(limit);
    }
}

impl<T, F, P> From<(T, F)> for PixelFragmentSequence<P>
where
    T: Into<C<u32>>,
    F: Into<C<P>>,
{
    /// Construct a pixel data fragment sequence,
    /// interpreting the first tuple element as a basic offset table
    /// and the second element as the vector of fragments.
    ///
    /// **Note:** This function does not validate the offset table
    /// against the given fragments.
    fn from((offset_table, fragments): (T, F)) -> Self {
        PixelFragmentSequence::new(offset_table, fragments)
    }
}

impl<I, P> From<PixelFragmentSequence<P>> for Value<I, P> {
    #[inline]
    fn from(value: PixelFragmentSequence<P>) -> Self {
        Value::PixelSequence(value)
    }
}

impl<P> HasLength for PixelFragmentSequence<P> {
    /// In standard DICOM,
    /// encapsulated pixel data is always defined by
    /// a pixel data element with an undefined length.
    #[inline]
    fn length(&self) -> Length {
        Length::UNDEFINED
    }
}

impl<P> DicomValueType for PixelFragmentSequence<P> {
    #[inline]
    fn value_type(&self) -> ValueType {
        ValueType::PixelSequence
    }

    #[inline]
    fn cardinality(&self) -> usize {
        1
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::dicom_value;
    use smallvec::smallvec;

    #[test]
    fn to_int() {
        let value = Value::new(dicom_value!(I32, [1, 2, 5]));
        assert_eq!(value.to_int::<u32>().unwrap(), 1);
        assert_eq!(value.to_int::<i32>().unwrap(), 1);
        assert_eq!(value.to_int::<u16>().unwrap(), 1);
        assert_eq!(value.to_int::<i16>().unwrap(), 1);
        assert_eq!(value.to_int::<u64>().unwrap(), 1);
        assert_eq!(value.to_int::<i64>().unwrap(), 1);

        assert_eq!(value.to_multi_int::<i32>().unwrap(), vec![1, 2, 5]);
        assert_eq!(value.to_multi_int::<u32>().unwrap(), vec![1, 2, 5]);

        // sequence values can't be turned to an int
        let value = Value::<EmptyObject, _>::new_sequence(smallvec![], Length::UNDEFINED);

        assert!(matches!(
            value.to_int::<u32>(),
            Err(ConvertValueError {
                requested: "integer",
                original: ValueType::DataSetSequence,
                ..
            })
        ));
    }

    #[test]
    fn to_float() {
        let value = Value::new(dicom_value!(F64, [1., 2., 5.]));
        assert_eq!(value.to_float32().unwrap(), 1.);
        assert_eq!(value.to_float64().unwrap(), 1.);

        assert_eq!(value.to_multi_float32().unwrap(), vec![1., 2., 5.]);
        assert_eq!(value.to_multi_float64().unwrap(), vec![1., 2., 5.]);

        // sequence values can't be turned to a number
        let value = Value::<EmptyObject, _>::new_sequence(smallvec![], Length::UNDEFINED);

        assert!(matches!(
            value.to_float32(),
            Err(ConvertValueError {
                requested: "float32",
                original: ValueType::DataSetSequence,
                ..
            })
        ));
    }

    #[test]
    fn to_date() {
        let expected_dates = [
            DicomDate::from_ymd(2021, 2, 3).unwrap(),
            DicomDate::from_ymd(2022, 3, 4).unwrap(),
            DicomDate::from_ymd(2023, 4, 5).unwrap(),
        ];

        let value = Value::new(dicom_value!(Strs, ["20210203", "20220304", "20230405"]));
        assert_eq!(value.to_date().unwrap(), expected_dates[0],);
        assert_eq!(value.to_multi_date().unwrap(), &expected_dates[..]);

        let value_pair = Value::new(dicom_value!(
            Date,
            [
                DicomDate::from_ymd(2021, 2, 3).unwrap(),
                DicomDate::from_ymd(2022, 3, 4).unwrap(),
            ]
        ));

        assert_eq!(value_pair.to_date().unwrap(), expected_dates[0]);
        assert_eq!(value_pair.to_multi_date().unwrap(), &expected_dates[0..2]);

        // cannot turn to integers
        assert!(matches!(
            value_pair.to_multi_int::<i64>(),
            Err(ConvertValueError {
                requested: "integer",
                original: ValueType::Date,
                ..
            })
        ));

        let range_value = Value::new(dicom_value!(Str, "20210203-20220304"));

        // can turn to range
        assert_eq!(
            range_value.to_date_range().unwrap(),
            DateRange::from_start_to_end(
                expected_dates[0].to_naive_date().unwrap(),
                expected_dates[1].to_naive_date().unwrap()
            )
            .unwrap()
        );
    }

    #[test]
    fn getters() {
        assert_eq!(
            Value::new(dicom_value!(Strs, ["Smith^John"]))
                .string()
                .unwrap(),
            "Smith^John"
        );

        assert_eq!(
            Value::new(dicom_value!(Strs, ["Smith^John"]))
                .strings()
                .unwrap(),
            &["Smith^John"]
        );

        assert_eq!(Value::new(dicom_value!(I32, [1, 2, 5])).int32().unwrap(), 1,);

        assert_eq!(
            Value::new(dicom_value!(I32, [1, 2, 5]))
                .int32_slice()
                .unwrap(),
            &[1, 2, 5],
        );

        assert!(matches!(
            Value::new(dicom_value!(I32, [1, 2, 5])).uint32(),
            Err(CastValueError {
                requested: "uint32",
                got: ValueType::I32,
                ..
            })
        ));

        assert!(matches!(
            Value::new(dicom_value!(I32, [1, 2, 5])).strings(),
            Err(CastValueError {
                requested: "strings",
                got: ValueType::I32,
                ..
            })
        ));

        assert_eq!(
            Value::new(PrimitiveValue::Date(smallvec![DicomDate::from_ymd(
                2014, 10, 12
            )
            .unwrap()]))
            .date()
            .ok(),
            Some(DicomDate::from_ymd(2014, 10, 12).unwrap()),
        );

        assert_eq!(
            Value::new(PrimitiveValue::Date(
                smallvec![DicomDate::from_ymd(2014, 10, 12).unwrap(); 5]
            ))
            .dates()
            .unwrap(),
            &[DicomDate::from_ymd(2014, 10, 12).unwrap(); 5]
        );

        assert!(matches!(
            Value::new(PrimitiveValue::Date(smallvec![DicomDate::from_ymd(
                2014, 10, 12
            )
            .unwrap()]))
            .time(),
            Err(CastValueError {
                requested: "time",
                got: ValueType::Date,
                ..
            })
        ));
    }

    #[derive(Debug, Clone, Copy, PartialEq)]
    struct DummyItem(u32);

    impl HasLength for DummyItem {
        fn length(&self) -> Length {
            Length::defined(8)
        }
    }

    #[test]
    fn value_eq() {
        // the following declarations are equivalent
        let v1 = Value::<_, _>::from(PixelFragmentSequence::new(
            smallvec![],
            smallvec![vec![1, 2, 5]],
        ));
        let v2 = Value::new_pixel_sequence(smallvec![], smallvec![vec![1, 2, 5]]);
        assert_eq!(v1, v2);
        assert_eq!(v2, v1);

        // redeclare with different type parameters
        let v1 = Value::<DummyItem, _>::from(PixelFragmentSequence::new(
            smallvec![],
            smallvec![vec![1, 2, 5]],
        ));

        // declarations are equivalent
        let v3 = Value::from(PrimitiveValue::from("Something"));
        let v4 = Value::new(dicom_value!(Str, "Something"));
        let v3_2: Value = "Something".into();
        assert_eq!(v3, v4);
        assert_eq!(v3, v3_2);

        // redeclare with different type parameters
        let v3: Value<DummyItem, _> = PrimitiveValue::from("Something").into();

        let v5 = Value::from(DataSetSequence::new(
            vec![DummyItem(0), DummyItem(1), DummyItem(2)],
            Length::defined(1000),
        ));
        let v6 = Value::from(DataSetSequence::new(
            vec![DummyItem(0), DummyItem(1), DummyItem(2)],
            Length::UNDEFINED,
        ));
        assert_eq!(v5, v6);

        assert_ne!(v1, v3);
        assert_ne!(v3, v1);
        assert_ne!(v1, v6);
        assert_ne!(v6, v1);
        assert_ne!(v3, v6);
        assert_ne!(v6, v3);
    }

    #[test]
    fn data_set_sequences() {
        let v = DataSetSequence::new(
            vec![DummyItem(1), DummyItem(2), DummyItem(5)],
            Length::defined(24),
        );

        assert_eq!(v.cardinality(), 3);
        assert_eq!(v.value_type(), ValueType::DataSetSequence);
        assert_eq!(v.items(), &[DummyItem(1), DummyItem(2), DummyItem(5)]);
        assert_eq!(v.length(), Length(24));

        let v = Value::<_, [u8; 0]>::from(v);
        assert_eq!(v.value_type(), ValueType::DataSetSequence);
        assert_eq!(v.cardinality(), 3);
        assert_eq!(
            v.items(),
            Some(&[DummyItem(1), DummyItem(2), DummyItem(5)][..])
        );
        assert_eq!(v.primitive(), None);
        assert_eq!(v.fragments(), None);
        assert_eq!(v.offset_table(), None);
        assert_eq!(v.length(), Length(24));

        // can't turn sequence to string
        assert!(matches!(
            v.to_str(),
            Err(ConvertValueError {
                original: ValueType::DataSetSequence,
                ..
            })
        ));
        // can't turn sequence to bytes
        assert!(matches!(
            v.to_bytes(),
            Err(ConvertValueError {
                requested: "bytes",
                original: ValueType::DataSetSequence,
                ..
            })
        ));

        // can turn into items
        let items = v.into_items().unwrap();
        assert_eq!(&items[..], &[DummyItem(1), DummyItem(2), DummyItem(5)][..]);
    }

    #[test]
    fn pixel_fragment_sequences() {
        let v = PixelFragmentSequence::new(vec![], vec![vec![0x55; 128]]);

        assert_eq!(v.cardinality(), 1);
        assert_eq!(v.value_type(), ValueType::PixelSequence);
        assert_eq!(v.fragments(), &[vec![0x55; 128]]);
        assert!(HasLength::length(&v).is_undefined());

        let v = Value::<EmptyObject, _>::from(v);
        assert_eq!(v.cardinality(), 1);
        assert_eq!(v.value_type(), ValueType::PixelSequence);
        assert_eq!(v.items(), None);
        assert_eq!(v.primitive(), None);
        assert_eq!(v.fragments(), Some(&[vec![0x55; 128]][..]));
        assert_eq!(v.offset_table(), Some(&[][..]));
        assert!(HasLength::length(&v).is_undefined());

        // can't turn sequence to string
        assert!(matches!(
            v.to_str(),
            Err(ConvertValueError {
                requested: "string",
                original: ValueType::PixelSequence,
                ..
            })
        ));

        // can't turn sequence to bytes
        assert!(matches!(
            v.to_bytes(),
            Err(ConvertValueError {
                requested: "bytes",
                original: ValueType::PixelSequence,
                ..
            })
        ));

        // can turn into fragments
        let fragments = v.into_fragments().unwrap();
        assert_eq!(&fragments[..], &[vec![0x55; 128]]);
    }
}