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
use std::fmt;
use std::str::FromStr;
use crate::error::{Error, Result};
/// DICOM Value Representation: the 34 VRs defined by PS3.5.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[repr(u8)]
pub enum Vr {
/// AE — Application Entity
Ae,
/// AS — Age String
As,
/// AT — Attribute Tag
At,
/// CS — Code String
Cs,
/// DA — Date
Da,
/// DS — Decimal String
Ds,
/// DT — Date Time
Dt,
/// FD — Floating Point Double (64-bit float)
Fd,
/// FL — Floating Point Single (32-bit float)
Fl,
/// IS — Integer String
Is,
/// LO — Long String
Lo,
/// LT — Long Text
Lt,
/// OB — Other Byte
Ob,
/// OD — Other Double (64-bit float)
Od,
/// OF — Other Float (32-bit float)
Of,
/// OL — Other Long (32-bit integer)
Ol,
/// OV — Other 64-bit Very Long (64-bit integer)
Ov,
/// OW — Other Word (16-bit word)
Ow,
/// PN — Person Name
Pn,
/// SH — Short String
Sh,
/// SL — Signed Long (32-bit signed integer)
Sl,
/// SQ — Sequence of Items
Sq,
/// SS — Signed Short (16-bit signed integer)
Ss,
/// ST — Short Text
St,
/// SV — Signed 64-bit Very Long (64-bit signed integer)
Sv,
/// TM — Time
Tm,
/// UC — Unlimited Characters
Uc,
/// UI — Unique Identifier
Ui,
/// UL — Unsigned Long (32-bit unsigned integer)
Ul,
/// UN — Unknown
Un,
/// UR — Universal Resource Identifier
Ur,
/// US — Unsigned Short (16-bit unsigned integer)
Us,
/// UT — Unlimited Text
Ut,
/// UV — Unsigned 64-bit Very Long (64-bit unsigned integer)
Uv,
}
impl Vr {
/// All 34 VRs, in standard order.
pub const ALL: [Self; 34] = [
Self::Ae,
Self::As,
Self::At,
Self::Cs,
Self::Da,
Self::Ds,
Self::Dt,
Self::Fd,
Self::Fl,
Self::Is,
Self::Lo,
Self::Lt,
Self::Ob,
Self::Od,
Self::Of,
Self::Ol,
Self::Ov,
Self::Ow,
Self::Pn,
Self::Sh,
Self::Sl,
Self::Sq,
Self::Ss,
Self::St,
Self::Sv,
Self::Tm,
Self::Uc,
Self::Ui,
Self::Ul,
Self::Un,
Self::Ur,
Self::Us,
Self::Ut,
Self::Uv,
];
/// Two-character spelling used by the standard, e.g. `"OB"`.
pub const fn as_str(self) -> &'static str {
match self {
Self::Ae => "AE",
Self::As => "AS",
Self::At => "AT",
Self::Cs => "CS",
Self::Da => "DA",
Self::Ds => "DS",
Self::Dt => "DT",
Self::Fd => "FD",
Self::Fl => "FL",
Self::Is => "IS",
Self::Lo => "LO",
Self::Lt => "LT",
Self::Ob => "OB",
Self::Od => "OD",
Self::Of => "OF",
Self::Ol => "OL",
Self::Ov => "OV",
Self::Ow => "OW",
Self::Pn => "PN",
Self::Sh => "SH",
Self::Sl => "SL",
Self::Sq => "SQ",
Self::Ss => "SS",
Self::St => "ST",
Self::Sv => "SV",
Self::Tm => "TM",
Self::Uc => "UC",
Self::Ui => "UI",
Self::Ul => "UL",
Self::Un => "UN",
Self::Ur => "UR",
Self::Us => "US",
Self::Ut => "UT",
Self::Uv => "UV",
}
}
/// Whether this VR is textual (value stored as text, possibly multi-valued).
pub const fn is_string(self) -> bool {
matches!(
self,
Self::Ae
| Self::As
| Self::Cs
| Self::Da
| Self::Ds
| Self::Dt
| Self::Is
| Self::Lo
| Self::Lt
| Self::Pn
| Self::Sh
| Self::St
| Self::Tm
| Self::Uc
| Self::Ui
| Self::Ur
| Self::Ut
)
}
/// Whether its text is governed by `Specific Character Set` (PS3.3 C.12.1.1.2).
pub const fn uses_specific_character_set(self) -> bool {
matches!(
self,
Self::Lo | Self::Lt | Self::Pn | Self::Sh | Self::St | Self::Uc | Self::Ut | Self::Ur
)
}
/// Whether explicit-VR encoding uses the "2 reserved bytes + 4-byte length" form (PS3.5 6.2).
pub const fn uses_long_length(self) -> bool {
matches!(
self,
Self::Ob
| Self::Od
| Self::Of
| Self::Ol
| Self::Ov
| Self::Ow
| Self::Sq
| Self::Uc
| Self::Un
| Self::Ur
| Self::Ut
)
}
/// Whether multiple values are separated by backslashes. In `LT`, `ST`, `UT` and `UR` a
/// backslash is an ordinary character (PS3.5 6.2).
pub const fn splits_on_backslash(self) -> bool {
self.is_string() && !matches!(self, Self::Lt | Self::St | Self::Ut | Self::Ur)
}
/// Pad byte used to reach even length: NUL for `UI`, a space for other textual VRs, NUL for
/// binary VRs.
pub const fn padding(self) -> u8 {
if matches!(self, Self::Ui) {
0
} else if self.is_string() {
b' '
} else {
0
}
}
/// Whether this VR is a sequence.
pub const fn is_sequence(self) -> bool {
matches!(self, Self::Sq)
}
}
impl fmt::Display for Vr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for Vr {
type Err = Error;
fn from_str(text: &str) -> Result<Self> {
let upper = text.trim().to_ascii_uppercase();
Self::ALL
.iter()
.copied()
.find(|vr| vr.as_str() == upper)
.ok_or_else(|| Error::UnknownVr(text.to_owned()))
}
}