dicom-toolkit-data 0.1.0

Core DICOM data structures, file I/O, and encoding/decoding
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
//! DICOM value types — the `Value` enum and its supporting types.
//!
//! Ports DCMTK's per-VR element classes into a single Rust enum, with dedicated
//! structs for the richer DICOM scalar types (dates, times, person names).

use crate::dataset::DataSet;
use dicom_toolkit_core::error::{DcmError, DcmResult};
use dicom_toolkit_dict::Tag;
use std::fmt;

// ── DicomDate ──────────────────────────────────────────────────────────────────

/// A DICOM DA (Date) value: YYYYMMDD, with optional month and day.
///
/// Partial dates are represented by leaving `month` and/or `day` as `0`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct DicomDate {
    pub year: u16,
    /// 0 when not specified (partial date contains year only).
    pub month: u8,
    /// 0 when not specified (partial date contains year+month only).
    pub day: u8,
}

impl DicomDate {
    /// Parse a DICOM DA string: YYYYMMDD, YYYYMM, or YYYY.
    pub fn parse(s: &str) -> DcmResult<Self> {
        let s = s.trim();
        match s.len() {
            4 => {
                let year = parse_u16_str(&s[0..4])?;
                Ok(Self {
                    year,
                    month: 0,
                    day: 0,
                })
            }
            6 => {
                let year = parse_u16_str(&s[0..4])?;
                let month = parse_u8_str(&s[4..6])?;
                Ok(Self {
                    year,
                    month,
                    day: 0,
                })
            }
            8 => {
                let year = parse_u16_str(&s[0..4])?;
                let month = parse_u8_str(&s[4..6])?;
                let day = parse_u8_str(&s[6..8])?;
                Ok(Self { year, month, day })
            }
            _ => Err(DcmError::Other(format!("invalid DICOM date: {:?}", s))),
        }
    }

    /// Parse a DICOM DA string, also accepting the legacy "YYYY.MM.DD" format.
    pub fn from_da_str(s: &str) -> DcmResult<Self> {
        let s = s.trim();
        if s.len() == 10 && s.as_bytes().get(4) == Some(&b'.') && s.as_bytes().get(7) == Some(&b'.')
        {
            let year = parse_u16_str(&s[0..4])?;
            let month = parse_u8_str(&s[5..7])?;
            let day = parse_u8_str(&s[8..10])?;
            return Ok(Self { year, month, day });
        }
        Self::parse(s)
    }
}

impl fmt::Display for DicomDate {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.month == 0 {
            write!(f, "{:04}", self.year)
        } else if self.day == 0 {
            write!(f, "{:04}{:02}", self.year, self.month)
        } else {
            write!(f, "{:04}{:02}{:02}", self.year, self.month, self.day)
        }
    }
}

// ── DicomTime ──────────────────────────────────────────────────────────────────

/// A DICOM TM (Time) value: HHMMSS.FFFFFF, with optional components.
///
/// Partial times are allowed: `HH`, `HHMM`, `HHMMSS`, `HHMMSS.F{1-6}`.
/// Missing components are stored as `0`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DicomTime {
    pub hour: u8,
    pub minute: u8,
    pub second: u8,
    /// Fractional seconds in microseconds (0–999999).
    pub fraction: u32,
}

impl DicomTime {
    /// Parse a DICOM TM string.
    pub fn parse(s: &str) -> DcmResult<Self> {
        let s = s.trim();
        if s.is_empty() {
            return Err(DcmError::Other("empty DICOM time string".into()));
        }

        // Find the fractional part if present
        let (time_part, fraction) = if let Some(dot_pos) = s.find('.') {
            let frac_str = &s[dot_pos + 1..];
            // Pad or truncate to 6 digits
            let mut padded = String::from(frac_str);
            while padded.len() < 6 {
                padded.push('0');
            }
            let frac = parse_u32_str(&padded[..6])?;
            (&s[..dot_pos], frac)
        } else {
            (s, 0u32)
        };

        match time_part.len() {
            2 => {
                let hour = parse_u8_str(&time_part[0..2])?;
                if hour > 23 {
                    return Err(DcmError::Other(format!(
                        "invalid hour in DICOM time: {hour}"
                    )));
                }
                Ok(Self {
                    hour,
                    minute: 0,
                    second: 0,
                    fraction: 0,
                })
            }
            4 => {
                let hour = parse_u8_str(&time_part[0..2])?;
                let minute = parse_u8_str(&time_part[2..4])?;
                if hour > 23 {
                    return Err(DcmError::Other(format!(
                        "invalid hour in DICOM time: {hour}"
                    )));
                }
                if minute > 59 {
                    return Err(DcmError::Other(format!(
                        "invalid minute in DICOM time: {minute}"
                    )));
                }
                Ok(Self {
                    hour,
                    minute,
                    second: 0,
                    fraction: 0,
                })
            }
            6 => {
                let hour = parse_u8_str(&time_part[0..2])?;
                let minute = parse_u8_str(&time_part[2..4])?;
                let second = parse_u8_str(&time_part[4..6])?;
                if hour > 23 {
                    return Err(DcmError::Other(format!(
                        "invalid hour in DICOM time: {hour}"
                    )));
                }
                if minute > 59 {
                    return Err(DcmError::Other(format!(
                        "invalid minute in DICOM time: {minute}"
                    )));
                }
                if second > 59 {
                    return Err(DcmError::Other(format!(
                        "invalid second in DICOM time: {second}"
                    )));
                }
                Ok(Self {
                    hour,
                    minute,
                    second,
                    fraction,
                })
            }
            _ => Err(DcmError::Other(format!("invalid DICOM time: {:?}", s))),
        }
    }
}

impl fmt::Display for DicomTime {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:02}{:02}{:02}", self.hour, self.minute, self.second)?;
        if self.fraction > 0 {
            write!(f, ".{:06}", self.fraction)?;
        }
        Ok(())
    }
}

// ── DicomDateTime ─────────────────────────────────────────────────────────────

/// A DICOM DT (DateTime) value: YYYYMMDDHHMMSS.FFFFFF+ZZZZ.
#[derive(Debug, Clone, PartialEq)]
pub struct DicomDateTime {
    pub date: DicomDate,
    pub time: Option<DicomTime>,
    /// UTC offset in minutes, e.g. +0530 → 330, -0500 → -300.
    pub offset_minutes: Option<i16>,
}

impl DicomDateTime {
    /// Parse a DICOM DT string.
    pub fn parse(s: &str) -> DcmResult<Self> {
        let s = s.trim();
        if s.len() < 4 {
            return Err(DcmError::Other(format!("invalid DICOM datetime: {:?}", s)));
        }

        // Separate UTC offset: find trailing +/- that belong to timezone
        // Timezone is the last +HHMM or -HHMM
        let (dt_part, offset_minutes) = extract_tz_offset(s)?;

        // Date is always the first 8 chars (YYYYMMDD), but may be shorter
        let date_len = dt_part.len().min(8);
        let date_str = &dt_part[..date_len];
        // Pad date string to at least 4 chars
        let date = DicomDate::parse(date_str)?;

        let time = if dt_part.len() > 8 {
            Some(DicomTime::parse(&dt_part[8..])?)
        } else {
            None
        };

        Ok(Self {
            date,
            time,
            offset_minutes,
        })
    }
}

/// Extracts an optional trailing timezone offset (+HHMM or -HHMM) from a DT string.
fn extract_tz_offset(s: &str) -> DcmResult<(&str, Option<i16>)> {
    // Look for + or - that is not part of the date/time portion.
    // The date+time part is at most 21 chars (YYYYMMDDHHMMSS.FFFFFF),
    // so any sign after position 4 is a potential offset.
    let bytes = s.as_bytes();
    for i in (1..s.len()).rev() {
        if bytes[i] == b'+' || bytes[i] == b'-' {
            let tz_str = &s[i..];
            if tz_str.len() == 5 {
                let sign: i16 = if bytes[i] == b'+' { 1 } else { -1 };
                let hh = parse_u8_str(&tz_str[1..3])? as i16;
                let mm = parse_u8_str(&tz_str[3..5])? as i16;
                return Ok((&s[..i], Some(sign * (hh * 60 + mm))));
            }
        }
    }
    Ok((s, None))
}

impl fmt::Display for DicomDateTime {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.date)?;
        if let Some(ref t) = self.time {
            write!(f, "{}", t)?;
        }
        if let Some(offset) = self.offset_minutes {
            let sign = if offset >= 0 { '+' } else { '-' };
            let abs = offset.unsigned_abs();
            write!(f, "{}{:02}{:02}", sign, abs / 60, abs % 60)?;
        }
        Ok(())
    }
}

// ── PersonName ────────────────────────────────────────────────────────────────

/// A DICOM PN (Person Name) value.
///
/// Each name consists of up to three component groups (alphabetic, ideographic,
/// phonetic) separated by `=`. Within each group, the five name components
/// (family, given, middle, prefix, suffix) are separated by `^`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PersonName {
    pub alphabetic: String,
    pub ideographic: String,
    pub phonetic: String,
}

impl PersonName {
    /// Parse a DICOM PN string.
    pub fn parse(s: &str) -> Self {
        let mut parts = s.splitn(3, '=');
        PersonName {
            alphabetic: parts.next().unwrap_or("").to_string(),
            ideographic: parts.next().unwrap_or("").to_string(),
            phonetic: parts.next().unwrap_or("").to_string(),
        }
    }

    fn component(group: &str, index: usize) -> &str {
        group.split('^').nth(index).unwrap_or("")
    }

    pub fn last_name(&self) -> &str {
        Self::component(&self.alphabetic, 0)
    }

    pub fn first_name(&self) -> &str {
        Self::component(&self.alphabetic, 1)
    }

    pub fn middle_name(&self) -> &str {
        Self::component(&self.alphabetic, 2)
    }

    pub fn prefix(&self) -> &str {
        Self::component(&self.alphabetic, 3)
    }

    pub fn suffix(&self) -> &str {
        Self::component(&self.alphabetic, 4)
    }
}

impl fmt::Display for PersonName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Emit only as many groups as are non-empty, trimming trailing empty groups.
        if !self.phonetic.is_empty() {
            write!(
                f,
                "{}={}={}",
                self.alphabetic, self.ideographic, self.phonetic
            )
        } else if !self.ideographic.is_empty() {
            write!(f, "{}={}", self.alphabetic, self.ideographic)
        } else {
            write!(f, "{}", self.alphabetic)
        }
    }
}

// ── PixelData ─────────────────────────────────────────────────────────────────

/// Pixel data stored either as native (uncompressed) bytes or encapsulated
/// (compressed) fragments.
#[derive(Debug, Clone, PartialEq)]
pub enum PixelData {
    /// Uncompressed pixel data.
    Native { bytes: Vec<u8> },
    /// Encapsulated (compressed) pixel data with optional offset table.
    Encapsulated {
        offset_table: Vec<u32>,
        fragments: Vec<Vec<u8>>,
    },
}

// ── Value ─────────────────────────────────────────────────────────────────────

/// The value held by a DICOM data element.
///
/// Each variant corresponds to one or more DICOM VRs. Numeric string VRs
/// (DS, IS) are stored already decoded as `f64`/`i64`.
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
    /// No value (zero-length element).
    Empty,
    /// AE, CS, LO, LT, SH, ST, UC, UR, UT — multi-valued via backslash.
    Strings(Vec<String>),
    /// PN — person name with up to three component groups.
    PersonNames(Vec<PersonName>),
    /// UI — UID string.
    Uid(String),
    /// DA — date values.
    Date(Vec<DicomDate>),
    /// TM — time values.
    Time(Vec<DicomTime>),
    /// DT — datetime values.
    DateTime(Vec<DicomDateTime>),
    /// IS — integer string, decoded.
    Ints(Vec<i64>),
    /// DS — decimal string, decoded.
    Decimals(Vec<f64>),
    /// OB, UN — raw bytes.
    U8(Vec<u8>),
    /// US, OW — raw 16-bit words (interpret by VR).
    U16(Vec<u16>),
    /// SS — signed 16-bit integers.
    I16(Vec<i16>),
    /// UL, OL — 32-bit unsigned integers.
    U32(Vec<u32>),
    /// SL — 32-bit signed integers.
    I32(Vec<i32>),
    /// UV, OV — 64-bit unsigned integers.
    U64(Vec<u64>),
    /// SV — 64-bit signed integers.
    I64(Vec<i64>),
    /// FL, OF — 32-bit floats.
    F32(Vec<f32>),
    /// FD, OD — 64-bit floats.
    F64(Vec<f64>),
    /// AT — attribute tag pairs.
    Tags(Vec<Tag>),
    /// SQ — sequence of items (datasets).
    Sequence(Vec<DataSet>),
    /// Pixel data — (7FE0,0010).
    PixelData(PixelData),
}

impl Value {
    /// Returns the number of values (VM).
    pub fn multiplicity(&self) -> usize {
        match self {
            Value::Empty => 0,
            Value::Strings(v) => v.len(),
            Value::PersonNames(v) => v.len(),
            Value::Uid(_) => 1,
            Value::Date(v) => v.len(),
            Value::Time(v) => v.len(),
            Value::DateTime(v) => v.len(),
            Value::Ints(v) => v.len(),
            Value::Decimals(v) => v.len(),
            Value::U8(v) => v.len(),
            Value::U16(v) => v.len(),
            Value::I16(v) => v.len(),
            Value::U32(v) => v.len(),
            Value::I32(v) => v.len(),
            Value::U64(v) => v.len(),
            Value::I64(v) => v.len(),
            Value::F32(v) => v.len(),
            Value::F64(v) => v.len(),
            Value::Tags(v) => v.len(),
            Value::Sequence(v) => v.len(),
            Value::PixelData(_) => 1,
        }
    }

    pub fn is_empty(&self) -> bool {
        self.multiplicity() == 0
    }

    /// Returns the first string value, if this is a `Strings` or `Uid` variant.
    pub fn as_string(&self) -> Option<&str> {
        match self {
            Value::Strings(v) => v.first().map(|s| s.as_str()),
            Value::Uid(s) => Some(s.as_str()),
            Value::PersonNames(v) => v.first().map(|p| p.alphabetic.as_str()),
            _ => None,
        }
    }

    pub fn as_strings(&self) -> Option<&[String]> {
        match self {
            Value::Strings(v) => Some(v.as_slice()),
            _ => None,
        }
    }

    pub fn as_u16(&self) -> Option<u16> {
        match self {
            Value::U16(v) => v.first().copied(),
            _ => None,
        }
    }

    pub fn as_u32(&self) -> Option<u32> {
        match self {
            Value::U32(v) => v.first().copied(),
            _ => None,
        }
    }

    pub fn as_i32(&self) -> Option<i32> {
        match self {
            Value::I32(v) => v.first().copied(),
            _ => None,
        }
    }

    pub fn as_f64(&self) -> Option<f64> {
        match self {
            Value::F64(v) => v.first().copied(),
            Value::Decimals(v) => v.first().copied(),
            _ => None,
        }
    }

    pub fn as_bytes(&self) -> Option<&[u8]> {
        match self {
            Value::U8(v) => Some(v.as_slice()),
            Value::PixelData(PixelData::Native { bytes }) => Some(bytes.as_slice()),
            _ => None,
        }
    }

    /// Returns a human-readable string representation (like dcmdump output).
    pub fn to_display_string(&self) -> String {
        match self {
            Value::Empty => String::new(),
            Value::Strings(v) => v.join("\\"),
            Value::PersonNames(v) => v
                .iter()
                .map(|p| p.to_string())
                .collect::<Vec<_>>()
                .join("\\"),
            Value::Uid(s) => s.clone(),
            Value::Date(v) => v
                .iter()
                .map(|d| d.to_string())
                .collect::<Vec<_>>()
                .join("\\"),
            Value::Time(v) => v
                .iter()
                .map(|t| t.to_string())
                .collect::<Vec<_>>()
                .join("\\"),
            Value::DateTime(v) => v
                .iter()
                .map(|dt| dt.to_string())
                .collect::<Vec<_>>()
                .join("\\"),
            Value::Ints(v) => v
                .iter()
                .map(|n| n.to_string())
                .collect::<Vec<_>>()
                .join("\\"),
            Value::Decimals(v) => v
                .iter()
                .map(|n| format_f64(*n))
                .collect::<Vec<_>>()
                .join("\\"),
            Value::U8(v) => format!("({} bytes)", v.len()),
            Value::U16(v) => v
                .iter()
                .map(|n| n.to_string())
                .collect::<Vec<_>>()
                .join("\\"),
            Value::I16(v) => v
                .iter()
                .map(|n| n.to_string())
                .collect::<Vec<_>>()
                .join("\\"),
            Value::U32(v) => v
                .iter()
                .map(|n| n.to_string())
                .collect::<Vec<_>>()
                .join("\\"),
            Value::I32(v) => v
                .iter()
                .map(|n| n.to_string())
                .collect::<Vec<_>>()
                .join("\\"),
            Value::U64(v) => v
                .iter()
                .map(|n| n.to_string())
                .collect::<Vec<_>>()
                .join("\\"),
            Value::I64(v) => v
                .iter()
                .map(|n| n.to_string())
                .collect::<Vec<_>>()
                .join("\\"),
            Value::F32(v) => v
                .iter()
                .map(|n| format!("{}", n))
                .collect::<Vec<_>>()
                .join("\\"),
            Value::F64(v) => v
                .iter()
                .map(|n| format_f64(*n))
                .collect::<Vec<_>>()
                .join("\\"),
            Value::Tags(v) => v
                .iter()
                .map(|t| format!("({:04X},{:04X})", t.group, t.element))
                .collect::<Vec<_>>()
                .join("\\"),
            Value::Sequence(v) => format!("(Sequence with {} item(s))", v.len()),
            Value::PixelData(PixelData::Native { bytes }) => {
                format!("(PixelData, {} bytes)", bytes.len())
            }
            Value::PixelData(PixelData::Encapsulated { fragments, .. }) => {
                format!("(PixelData, {} fragment(s))", fragments.len())
            }
        }
    }

    /// Approximate encoded byte length (for dcmdump `# length` field).
    pub(crate) fn encoded_len(&self) -> usize {
        match self {
            Value::Empty => 0,
            Value::Strings(v) => {
                let total: usize = v.iter().map(|s| s.len()).sum();
                total + v.len().saturating_sub(1)
            }
            Value::PersonNames(v) => {
                let total: usize = v.iter().map(|p| p.to_string().len()).sum();
                total + v.len().saturating_sub(1)
            }
            Value::Uid(s) => s.len(),
            Value::Date(v) => v.len() * 8,
            Value::Time(v) => v.len() * 14,
            Value::DateTime(v) => v.len() * 26,
            Value::Ints(v) => {
                v.iter().map(|n| n.to_string().len()).sum::<usize>() + v.len().saturating_sub(1)
            }
            Value::Decimals(v) => {
                v.iter().map(|n| format_f64(*n).len()).sum::<usize>() + v.len().saturating_sub(1)
            }
            Value::U8(v) => v.len(),
            Value::U16(v) => v.len() * 2,
            Value::I16(v) => v.len() * 2,
            Value::U32(v) => v.len() * 4,
            Value::I32(v) => v.len() * 4,
            Value::U64(v) => v.len() * 8,
            Value::I64(v) => v.len() * 8,
            Value::F32(v) => v.len() * 4,
            Value::F64(v) => v.len() * 8,
            Value::Tags(v) => v.len() * 4,
            Value::Sequence(_) => 0,
            Value::PixelData(PixelData::Native { bytes }) => bytes.len(),
            Value::PixelData(PixelData::Encapsulated { fragments, .. }) => {
                fragments.iter().map(|f| f.len()).sum()
            }
        }
    }
}

// ── Helpers ───────────────────────────────────────────────────────────────────

fn parse_u8_str(s: &str) -> DcmResult<u8> {
    s.parse::<u8>()
        .map_err(|_| DcmError::Other(format!("expected u8, got {:?}", s)))
}

fn parse_u16_str(s: &str) -> DcmResult<u16> {
    s.parse::<u16>()
        .map_err(|_| DcmError::Other(format!("expected u16, got {:?}", s)))
}

fn parse_u32_str(s: &str) -> DcmResult<u32> {
    s.parse::<u32>()
        .map_err(|_| DcmError::Other(format!("expected u32, got {:?}", s)))
}

/// Format an f64 without trailing zeros but with at least one decimal place.
fn format_f64(v: f64) -> String {
    if v.fract() == 0.0 && v.abs() < 1e15 {
        format!("{:.1}", v)
    } else {
        format!("{}", v)
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

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

    // ── DicomDate ───────────────────────────────────────────────────────

    #[test]
    fn date_full_parse() {
        let d = DicomDate::parse("20231215").unwrap();
        assert_eq!(d.year, 2023);
        assert_eq!(d.month, 12);
        assert_eq!(d.day, 15);
    }

    #[test]
    fn date_year_only() {
        let d = DicomDate::parse("2023").unwrap();
        assert_eq!(d.year, 2023);
        assert_eq!(d.month, 0);
        assert_eq!(d.day, 0);
    }

    #[test]
    fn date_year_month() {
        let d = DicomDate::parse("202312").unwrap();
        assert_eq!(d.year, 2023);
        assert_eq!(d.month, 12);
        assert_eq!(d.day, 0);
    }

    #[test]
    fn date_display_full() {
        let d = DicomDate {
            year: 2023,
            month: 12,
            day: 15,
        };
        assert_eq!(d.to_string(), "20231215");
    }

    #[test]
    fn date_display_partial_year() {
        let d = DicomDate {
            year: 2023,
            month: 0,
            day: 0,
        };
        assert_eq!(d.to_string(), "2023");
    }

    #[test]
    fn date_display_partial_year_month() {
        let d = DicomDate {
            year: 2023,
            month: 12,
            day: 0,
        };
        assert_eq!(d.to_string(), "202312");
    }

    #[test]
    fn date_legacy_format() {
        let d = DicomDate::from_da_str("2023.12.15").unwrap();
        assert_eq!(d.year, 2023);
        assert_eq!(d.month, 12);
        assert_eq!(d.day, 15);
    }

    #[test]
    fn date_invalid() {
        assert!(DicomDate::parse("20231").is_err());
        assert!(DicomDate::parse("2023121").is_err());
        assert!(DicomDate::parse("abcdefgh").is_err());
    }

    // ── DicomTime ───────────────────────────────────────────────────────

    #[test]
    fn time_full_parse() {
        let t = DicomTime::parse("143022.500000").unwrap();
        assert_eq!(t.hour, 14);
        assert_eq!(t.minute, 30);
        assert_eq!(t.second, 22);
        assert_eq!(t.fraction, 500000);
    }

    #[test]
    fn time_partial_hour() {
        let t = DicomTime::parse("14").unwrap();
        assert_eq!(t.hour, 14);
        assert_eq!(t.minute, 0);
        assert_eq!(t.second, 0);
        assert_eq!(t.fraction, 0);
    }

    #[test]
    fn time_partial_hour_minute() {
        let t = DicomTime::parse("1430").unwrap();
        assert_eq!(t.hour, 14);
        assert_eq!(t.minute, 30);
        assert_eq!(t.second, 0);
    }

    #[test]
    fn time_partial_no_fraction() {
        let t = DicomTime::parse("143022").unwrap();
        assert_eq!(t.hour, 14);
        assert_eq!(t.minute, 30);
        assert_eq!(t.second, 22);
        assert_eq!(t.fraction, 0);
    }

    #[test]
    fn time_fraction_short() {
        // Short fraction is zero-padded on the right
        let t = DicomTime::parse("143022.5").unwrap();
        assert_eq!(t.fraction, 500000);
    }

    #[test]
    fn time_display() {
        let t = DicomTime {
            hour: 14,
            minute: 30,
            second: 22,
            fraction: 500000,
        };
        assert_eq!(t.to_string(), "143022.500000");
    }

    #[test]
    fn time_display_no_fraction() {
        let t = DicomTime {
            hour: 14,
            minute: 30,
            second: 22,
            fraction: 0,
        };
        assert_eq!(t.to_string(), "143022");
    }

    // ── DicomDateTime ───────────────────────────────────────────────────

    #[test]
    fn datetime_full_parse() {
        let dt = DicomDateTime::parse("20231215143022.000000+0530").unwrap();
        assert_eq!(dt.date.year, 2023);
        assert_eq!(dt.date.month, 12);
        assert_eq!(dt.date.day, 15);
        let t = dt.time.unwrap();
        assert_eq!(t.hour, 14);
        assert_eq!(t.minute, 30);
        assert_eq!(t.second, 22);
        assert_eq!(dt.offset_minutes, Some(330)); // +05:30 = 5*60+30 = 330
    }

    #[test]
    fn datetime_negative_offset() {
        let dt = DicomDateTime::parse("20231215143022.000000-0500").unwrap();
        assert_eq!(dt.offset_minutes, Some(-300));
    }

    #[test]
    fn datetime_no_time() {
        let dt = DicomDateTime::parse("20231215").unwrap();
        assert_eq!(dt.date.year, 2023);
        assert!(dt.time.is_none());
        assert!(dt.offset_minutes.is_none());
    }

    #[test]
    fn datetime_display_roundtrip() {
        // Use non-zero fraction so Display includes it, enabling exact round-trip.
        let s = "20231215143022.500000+0530";
        let dt = DicomDateTime::parse(s).unwrap();
        assert_eq!(dt.to_string(), s);
    }

    #[test]
    fn datetime_display_roundtrip_no_fraction() {
        // Without a fractional second the display omits the decimal.
        let s = "20231215143022+0530";
        let dt = DicomDateTime::parse(s).unwrap();
        assert_eq!(dt.to_string(), s);
    }

    // ── PersonName ──────────────────────────────────────────────────────

    #[test]
    fn pn_simple() {
        let pn = PersonName::parse("Eichelberg^Marco^^Dr.");
        assert_eq!(pn.last_name(), "Eichelberg");
        assert_eq!(pn.first_name(), "Marco");
        assert_eq!(pn.middle_name(), "");
        assert_eq!(pn.prefix(), "Dr.");
        assert_eq!(pn.suffix(), "");
    }

    #[test]
    fn pn_multi_component() {
        let pn = PersonName::parse("Smith^John=\u{5C71}\u{7530}^\u{592A}\u{90CE}=\u{3084}\u{307E}\u{3060}^\u{305F}\u{308D}\u{3046}");
        assert_eq!(pn.last_name(), "Smith");
        assert_eq!(pn.first_name(), "John");
        assert!(!pn.ideographic.is_empty());
        assert!(!pn.phonetic.is_empty());
    }

    #[test]
    fn pn_display_single_group() {
        let pn = PersonName::parse("Smith^John");
        assert_eq!(pn.to_string(), "Smith^John");
    }

    #[test]
    fn pn_display_two_groups() {
        let pn = PersonName::parse("Smith^John=SJ");
        assert_eq!(pn.to_string(), "Smith^John=SJ");
    }

    // ── Value ───────────────────────────────────────────────────────────

    #[test]
    fn value_multiplicity() {
        assert_eq!(Value::Empty.multiplicity(), 0);
        assert_eq!(
            Value::Strings(vec!["a".into(), "b".into()]).multiplicity(),
            2
        );
        assert_eq!(Value::U16(vec![1, 2, 3]).multiplicity(), 3);
        assert_eq!(Value::Uid("1.2.3".into()).multiplicity(), 1);
        assert_eq!(Value::Sequence(vec![]).multiplicity(), 0);
    }

    #[test]
    fn value_is_empty() {
        assert!(Value::Empty.is_empty());
        assert!(Value::Strings(vec![]).is_empty());
        assert!(!Value::Strings(vec!["x".into()]).is_empty());
    }

    #[test]
    fn value_as_string() {
        let v = Value::Strings(vec!["hello".into(), "world".into()]);
        assert_eq!(v.as_string(), Some("hello"));
        assert_eq!(v.as_strings().unwrap().len(), 2);
    }

    #[test]
    fn value_as_uid() {
        let v = Value::Uid("1.2.840.10008.1.1".into());
        assert_eq!(v.as_string(), Some("1.2.840.10008.1.1"));
    }

    #[test]
    fn value_as_numeric() {
        let v = Value::U16(vec![512]);
        assert_eq!(v.as_u16(), Some(512));

        let v = Value::U32(vec![65536]);
        assert_eq!(v.as_u32(), Some(65536));

        let v = Value::I32(vec![-1]);
        assert_eq!(v.as_i32(), Some(-1));

        let v = Value::F64(vec![2.78]);
        assert_eq!(v.as_f64(), Some(2.78));
    }

    #[test]
    fn value_to_display_string_strings() {
        let v = Value::Strings(vec!["foo".into(), "bar".into()]);
        assert_eq!(v.to_display_string(), "foo\\bar");
    }

    #[test]
    fn value_to_display_string_u16() {
        let v = Value::U16(vec![512, 256]);
        assert_eq!(v.to_display_string(), "512\\256");
    }

    #[test]
    fn value_to_display_string_sequence() {
        let v = Value::Sequence(vec![]);
        assert_eq!(v.to_display_string(), "(Sequence with 0 item(s))");
    }

    #[test]
    fn value_as_bytes() {
        let v = Value::U8(vec![1, 2, 3]);
        assert_eq!(v.as_bytes(), Some(&[1u8, 2, 3][..]));
    }
}