Skip to main content

dcmnorm_encoding/
text.rs

1//! This module contains reusable components for encoding and decoding text in DICOM
2//! data structures, including support for character repertoires.
3//!
4//! At the moment the following character sets are supported:
5//!
6//! | Character Set                 | decoding support | encoding support |
7//! |-------------------------------|------------------|------------------|
8//! | ISO-IR 6 (default)            | ✓ | ✓ |
9//! | ISO-IR 13 (WINDOWS_31J): The JIS X 0201-1976 character set (Japanese single-byte) | ✓ | ✓ |
10//! | ISO-IR 87 (ISO_2022_JP): The JIS X 0208-1990 character set (Japanese multi-byte) | ✓ | ✓ |
11//! | ISO-IR 100 (ISO-8859-1): Right-hand part of the Latin alphabet no. 1, the Western Europe character set | ✓ | ✓ |
12//! | ISO-IR 101 (ISO-8859-2): Right-hand part of the Latin alphabet no. 2, the Central/Eastern Europe character set | ✓ | ✓ |
13//! | ISO-IR 109 (ISO-8859-3): Right-hand part of the Latin alphabet no. 3, the South Europe character set | ✓ | ✓ |
14//! | ISO-IR 110 (ISO-8859-4): Right-hand part of the Latin alphabet no. 4, the North Europe character set | ✓ | ✓ |
15//! | ISO-IR 126 (ISO-8859-7): The Latin/Greek character set | ✓ | ✓ |
16//! | ISO-IR 127 (ISO-8859-6): The Latin/Arabic character set | ✓ | ✓ |
17//! | ISO-IR 138 (ISO-8859-8): The Latin/Hebrew character set | ✓ | ✓ |
18//! | ISO-IR 144 (ISO-8859-5): The Latin/Cyrillic character set | ✓ | ✓ |
19//! | ISO-IR 148 (ISO-8859-9): Latin no. 5, the Turkish character set  | x | x |
20//! | ISO-IR 149 (WINDOWS_949): The KS X 1001 character set (Korean) | ✓ | ✓ |
21//! | ISO-IR 159: The JIS X 0212-1990 character set (supplementary Japanese characters) | x | x |
22//! | ISO-IR 166 (WINDOWS_874): The TIS 620-2533 character set (Thai) | ✓ | ✓ |
23//! | ISO-IR 192: The Unicode character set based on the UTF-8 encoding | ✓ | ✓ |
24//! | GB18030: The Simplified Chinese character set | ✓ | ✓ |
25//! | GB2312: Simplified Chinese character set | ✓ | ✓ |
26//! | GBK: Simplified Chinese character set | ✓ | ✓ |
27//! These capabilities are available through [`SpecificCharacterSet`].
28
29use encoding::all::{
30    GB18030, GBK, ISO_2022_JP, ISO_8859_1, ISO_8859_2, ISO_8859_3, ISO_8859_4, ISO_8859_5,
31    ISO_8859_6, ISO_8859_7, ISO_8859_8, UTF_8, WINDOWS_31J, WINDOWS_874, WINDOWS_949,
32};
33use encoding::{DecoderTrap, EncoderTrap, Encoding, RawDecoder, StringWriter};
34use snafu::{Backtrace, Snafu};
35use std::borrow::Cow;
36use std::fmt::Debug;
37
38/// An error type for text encoding issues.
39#[derive(Debug, Snafu)]
40#[non_exhaustive]
41pub enum EncodeTextError {
42    /// A custom error message,
43    /// for when the underlying error type does not encode error semantics
44    /// into type variants.
45    #[snafu(display("{}", message))]
46    EncodeCustom {
47        /// The error message in plain text.
48        message: Cow<'static, str>,
49        /// The generated backtrace, if available.
50        backtrace: Backtrace,
51    },
52}
53
54/// An error type for text decoding issues.
55#[derive(Debug, Snafu)]
56#[non_exhaustive]
57pub enum DecodeTextError {
58    /// A custom error message,
59    /// for when the underlying error type does not encode error semantics
60    /// into type variants.
61    #[snafu(display("{}", message))]
62    DecodeCustom {
63        /// The error message in plain text.
64        message: Cow<'static, str>,
65        /// The generated backtrace, if available.
66        backtrace: Backtrace,
67    },
68}
69
70type EncodeResult<T> = Result<T, EncodeTextError>;
71type DecodeResult<T> = Result<T, DecodeTextError>;
72
73/// A holder of encoding and decoding mechanisms for text in DICOM content,
74/// which according to the standard, depends on the specific character set.
75pub trait TextCodec {
76    /// Obtain the defined term (unique name) of the text encoding,
77    /// which may be used as the value of a
78    /// Specific Character Set (0008, 0005) element to refer to this codec.
79    ///
80    /// Should contain no leading or trailing spaces.
81    /// This method may be useful for testing purposes, considering that
82    /// `TextCodec` is often used as a trait object.
83    fn name(&self) -> Cow<'static, str>;
84
85    /// Decode the given byte buffer as a single string. The resulting string
86    /// _may_ contain backslash characters ('\') to delimit individual values,
87    /// and should be split later on if required.
88    fn decode(&self, text: &[u8]) -> DecodeResult<String>;
89
90    /// Encode a text value into a byte vector. The input string can
91    /// feature multiple text values by using the backslash character ('\')
92    /// as the value delimiter.
93    fn encode(&self, text: &str) -> EncodeResult<Vec<u8>>;
94}
95
96impl<T: ?Sized> TextCodec for Box<T>
97where
98    T: TextCodec,
99{
100    fn name(&self) -> Cow<'static, str> {
101        self.as_ref().name()
102    }
103
104    fn decode(&self, text: &[u8]) -> DecodeResult<String> {
105        self.as_ref().decode(text)
106    }
107
108    fn encode(&self, text: &str) -> EncodeResult<Vec<u8>> {
109        self.as_ref().encode(text)
110    }
111}
112
113impl<T: ?Sized> TextCodec for &'_ T
114where
115    T: TextCodec,
116{
117    fn name(&self) -> Cow<'static, str> {
118        (**self).name()
119    }
120
121    fn decode(&self, text: &[u8]) -> DecodeResult<String> {
122        (**self).decode(text)
123    }
124
125    fn encode(&self, text: &str) -> EncodeResult<Vec<u8>> {
126        (**self).encode(text)
127    }
128}
129
130/// A descriptor for a specific character set,
131/// taking part in text encoding and decoding
132/// as per [PS3.5 ch 6 6.1](https://dicom.nema.org/medical/dicom/2023e/output/chtml/part05/chapter_6.html#sect_6.1).
133///
134/// # Example
135///
136/// Use [`from_code`](SpecificCharacterSet::from_code)
137/// or one of the associated constants to create a character set.
138/// From there, use the [`TextCodec`] trait to encode and decode text.
139///
140/// ```
141/// use dcmnorm_encoding::text::{SpecificCharacterSet, TextCodec};
142///
143/// let character_set = SpecificCharacterSet::from_code("ISO_IR 100").unwrap();
144/// assert_eq!(character_set, SpecificCharacterSet::ISO_IR_100);
145/// ```
146#[derive(Debug, Default, Clone, PartialEq)]
147pub struct SpecificCharacterSet(CharsetImpl);
148
149impl SpecificCharacterSet {
150    /// ISO IR 6: The default character set, as defined by the DICOM standard.
151    pub const ISO_IR_6: SpecificCharacterSet = SpecificCharacterSet(CharsetImpl::Default);
152
153    // ISO IR 100: ISO 8859-1, the Western Europe character set
154    pub const ISO_IR_100: SpecificCharacterSet = SpecificCharacterSet(CharsetImpl::IsoIr100);
155
156    /// ISO IR 192: UTF-8 encoding
157    pub const ISO_IR_192: SpecificCharacterSet = SpecificCharacterSet(CharsetImpl::IsoIr192);
158
159    /// Obtain the specific character set identified by the given code string.
160    ///
161    /// Supported code strings include the possible values
162    /// in the respective DICOM element (0008, 0005).
163    ///
164    /// # Example
165    ///
166    /// ```
167    /// use dcmnorm_encoding::text::{SpecificCharacterSet, TextCodec};
168    ///
169    /// let character_set = SpecificCharacterSet::from_code("ISO_IR 100").unwrap();
170    /// assert_eq!(character_set.name(), "ISO_IR 100");
171    /// ```
172    pub fn from_code(code: &str) -> Option<Self> {
173        CharsetImpl::from_code(code).map(SpecificCharacterSet)
174    }
175
176    /// Obtain the specific character set for all the values of a Specific Character Set
177    /// (0008,0005) element. A single value is the same as [`from_code`](Self::from_code); more
178    /// than one (`\ISO 2022 IR 87`, `ISO 2022 IR 6\ISO 2022 IR 149`) selects ISO 2022 code
179    /// extension, where text switches between the declared character sets with escape
180    /// sequences (PS3.5 6.1.2.5). An empty first value is the default repertoire.
181    /// Unsupported values are left out; `None` when no value is supported.
182    ///
183    /// ```
184    /// use dcmnorm_encoding::text::{SpecificCharacterSet, TextCodec};
185    ///
186    /// let charset = SpecificCharacterSet::from_codes(&["", "ISO 2022 IR 87"]).unwrap();
187    /// assert_eq!(charset.decode(b"\x1b$B;n83\x1b(B").unwrap(), "試験");
188    /// ```
189    pub fn from_codes<S: AsRef<str>>(codes: &[S]) -> Option<Self> {
190        match codes {
191            [] => None,
192            [code] => Self::from_code(code.as_ref()).or_else(|| {
193                code.as_ref()
194                    .trim()
195                    .is_empty()
196                    .then(SpecificCharacterSet::default)
197            }),
198            codes => crate::iso2022::Iso2022::from_codes(codes)
199                .map(|charset| SpecificCharacterSet(CharsetImpl::Iso2022(charset))),
200        }
201    }
202}
203
204impl TextCodec for SpecificCharacterSet {
205    fn name(&self) -> Cow<'static, str> {
206        self.0.name()
207    }
208
209    fn decode(&self, text: &[u8]) -> DecodeResult<String> {
210        self.0.decode(text)
211    }
212
213    fn encode(&self, text: &str) -> EncodeResult<Vec<u8>> {
214        self.0.encode(text)
215    }
216}
217
218/// An enum type for individual supported character sets.
219#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
220#[non_exhaustive]
221enum CharsetImpl {
222    /// **ISO-IR 6**: the default character set.
223    #[default]
224    Default,
225    /// **ISO-IR 13**: The Simplified Japanese single byte character set.
226    IsoIr13,
227    /// **ISO-IR 87**: The Simplified Japanese multi byte character set.
228    IsoIr87,
229    /// **ISO-IR 100** (ISO-8859-1): Right-hand part of the Latin alphabet no. 1,
230    /// the Western Europe character set.
231    IsoIr100,
232    /// **ISO-IR 101** (ISO-8859-2): Right-hand part of the Latin alphabet no. 2,
233    /// the Central/Eastern Europe character set.
234    IsoIr101,
235    /// **ISO-IR 109** (ISO-8859-3): Right-hand part of the Latin alphabet no. 3,
236    /// the South Europe character set.
237    IsoIr109,
238    /// **ISO-IR 110** (ISO-8859-4): Right-hand part of the Latin alphabet no. 4,
239    /// the North Europe character set.
240    IsoIr110,
241    /// **ISO-IR 126** (ISO-8859-7): The Greek character set.
242    IsoIr126,
243    /// **ISO-IR 127** (ISO-8859-6): The Arabic character set.
244    IsoIr127,
245    /// **ISO-IR 138** (ISO-8859-8): The Hebrew character set.
246    IsoIr138,
247    /// **ISO-IR 144** (ISO-8859-5): The Latin/Cyrillic character set.
248    IsoIr144,
249    /// **ISO-IR 149**: The Korean character set.
250    IsoIr149,
251    /// **ISO-IR 166**: The Thai character set.
252    IsoIr166,
253    /// **ISO-IR 192**: The Unicode character set based on the UTF-8 encoding.
254    IsoIr192,
255    /// **GB18030**: The Simplified Chinese character set.
256    Gb18030,
257    /// **Gbk**: The Simplified Chinese character set.
258    Gbk,
259    /// A multi-valued Specific Character Set using ISO 2022 code extension.
260    Iso2022(crate::iso2022::Iso2022),
261    // Support for more text encodings is tracked in issue #40.
262}
263
264impl CharsetImpl {
265    /// Obtain the specific character set identified by the given code string.
266    ///
267    /// Supported code strings include the possible values
268    /// in the respective DICOM element (0008, 0005).
269    pub fn from_code(uid: &str) -> Option<Self> {
270        use self::CharsetImpl::*;
271        match uid.trim_end() {
272            "Default" | "ISO_IR_6" | "ISO_IR 6" | "ISO 2022 IR 6" => Some(Default),
273            "ISO_IR_13" | "ISO_IR 13" | "ISO 2022 IR 13" => Some(IsoIr13),
274            "ISO_IR_87" | "ISO_IR 87" | "ISO 2022 IR 87" => Some(IsoIr87),
275            "ISO_IR_100" | "ISO_IR 100" | "ISO 2022 IR 100" => Some(IsoIr100),
276            "ISO_IR_101" | "ISO_IR 101" | "ISO 2022 IR 101" => Some(IsoIr101),
277            "ISO_IR_109" | "ISO_IR 109" | "ISO 2022 IR 109" => Some(IsoIr109),
278            "ISO_IR_110" | "ISO_IR 110" | "ISO 2022 IR 110" => Some(IsoIr110),
279            "ISO_IR_126" | "ISO_IR 126" | "ISO 2022 IR 126" => Some(IsoIr126),
280            "ISO_IR_127" | "ISO_IR 127" | "ISO 2022 IR 127" => Some(IsoIr127),
281            "ISO_IR_138" | "ISO_IR 138" | "ISO 2022 IR 138" => Some(IsoIr138),
282            "ISO_IR_144" | "ISO_IR 144" | "ISO 2022 IR 144" => Some(IsoIr144),
283            "ISO_IR_149" | "ISO_IR 149" | "ISO 2022 IR 149" => Some(IsoIr149),
284            "ISO_IR_166" | "ISO_IR 166" | "ISO 2022 IR 166" => Some(IsoIr166),
285            "ISO_IR_192" | "ISO_IR 192" => Some(IsoIr192),
286            "GB18030" => Some(Gb18030),
287            "GBK" | "GB2312" | "ISO 2022 IR 58" => Some(Gbk),
288            _ => None,
289        }
290    }
291}
292
293impl TextCodec for CharsetImpl {
294    fn name(&self) -> Cow<'static, str> {
295        if let CharsetImpl::Iso2022(charset) = self {
296            return Cow::Owned(charset.name());
297        }
298        Cow::Borrowed(match self {
299            CharsetImpl::Iso2022(_) => unreachable!(),
300            CharsetImpl::Default => "ISO_IR 6",
301            CharsetImpl::IsoIr13 => "ISO_IR 13",
302            CharsetImpl::IsoIr87 => "ISO_IR 87",
303            CharsetImpl::IsoIr100 => "ISO_IR 100",
304            CharsetImpl::IsoIr101 => "ISO_IR 101",
305            CharsetImpl::IsoIr109 => "ISO_IR 109",
306            CharsetImpl::IsoIr110 => "ISO_IR 110",
307            CharsetImpl::IsoIr126 => "ISO_IR 126",
308            CharsetImpl::IsoIr127 => "ISO_IR 127",
309            CharsetImpl::IsoIr138 => "ISO_IR 138",
310            CharsetImpl::IsoIr144 => "ISO_IR 144",
311            CharsetImpl::IsoIr149 => "ISO_IR 149",
312            CharsetImpl::IsoIr166 => "ISO_IR 166",
313            CharsetImpl::IsoIr192 => "ISO_IR 192",
314            CharsetImpl::Gb18030 => "GB18030",
315            CharsetImpl::Gbk => "GBK",
316        })
317    }
318
319    fn decode(&self, text: &[u8]) -> DecodeResult<String> {
320        match self {
321            CharsetImpl::Default => DefaultCharacterSetCodec.decode(text),
322            CharsetImpl::IsoIr13 => IsoIr13CharacterSetCodec.decode(text),
323            CharsetImpl::IsoIr87 => IsoIr87CharacterSetCodec.decode(text),
324            CharsetImpl::IsoIr100 => IsoIr100CharacterSetCodec.decode(text),
325            CharsetImpl::IsoIr101 => IsoIr101CharacterSetCodec.decode(text),
326            CharsetImpl::IsoIr109 => IsoIr109CharacterSetCodec.decode(text),
327            CharsetImpl::IsoIr110 => IsoIr110CharacterSetCodec.decode(text),
328            CharsetImpl::IsoIr126 => IsoIr126CharacterSetCodec.decode(text),
329            CharsetImpl::IsoIr127 => IsoIr127CharacterSetCodec.decode(text),
330            CharsetImpl::IsoIr138 => IsoIr138CharacterSetCodec.decode(text),
331            CharsetImpl::IsoIr144 => IsoIr144CharacterSetCodec.decode(text),
332            CharsetImpl::IsoIr149 => IsoIr149CharacterSetCodec.decode(text),
333            CharsetImpl::IsoIr166 => IsoIr166CharacterSetCodec.decode(text),
334            CharsetImpl::IsoIr192 => Utf8CharacterSetCodec.decode(text),
335            CharsetImpl::Gb18030 => Gb18030CharacterSetCodec.decode(text),
336            CharsetImpl::Gbk => GBKCharacterSetCodec.decode(text),
337            CharsetImpl::Iso2022(charset) => Ok(charset.decode(text)),
338        }
339    }
340
341    fn encode(&self, text: &str) -> EncodeResult<Vec<u8>> {
342        match self {
343            CharsetImpl::Default => DefaultCharacterSetCodec.encode(text),
344            CharsetImpl::IsoIr13 => IsoIr13CharacterSetCodec.encode(text),
345            CharsetImpl::IsoIr87 => IsoIr87CharacterSetCodec.encode(text),
346            CharsetImpl::IsoIr100 => IsoIr100CharacterSetCodec.encode(text),
347            CharsetImpl::IsoIr101 => IsoIr101CharacterSetCodec.encode(text),
348            CharsetImpl::IsoIr109 => IsoIr109CharacterSetCodec.encode(text),
349            CharsetImpl::IsoIr110 => IsoIr110CharacterSetCodec.encode(text),
350            CharsetImpl::IsoIr126 => IsoIr126CharacterSetCodec.encode(text),
351            CharsetImpl::IsoIr127 => IsoIr127CharacterSetCodec.encode(text),
352            CharsetImpl::IsoIr138 => IsoIr138CharacterSetCodec.encode(text),
353            CharsetImpl::IsoIr144 => IsoIr144CharacterSetCodec.encode(text),
354            CharsetImpl::IsoIr149 => IsoIr149CharacterSetCodec.encode(text),
355            CharsetImpl::IsoIr166 => IsoIr166CharacterSetCodec.encode(text),
356            CharsetImpl::IsoIr192 => Utf8CharacterSetCodec.encode(text),
357            CharsetImpl::Gb18030 => Gb18030CharacterSetCodec.encode(text),
358            CharsetImpl::Gbk => GBKCharacterSetCodec.encode(text),
359            CharsetImpl::Iso2022(charset) => charset
360                .encode(text)
361                .map_err(|message| EncodeCustomSnafu { message }.build()),
362        }
363    }
364}
365
366fn decode_text_trap(
367    _decoder: &mut dyn RawDecoder,
368    input: &[u8],
369    output: &mut dyn StringWriter,
370) -> bool {
371    let c = input[0];
372    let o0 = c & 7;
373    let o1 = (c & 56) >> 3;
374    let o2 = (c & 192) >> 6;
375    output.write_char('\\');
376    output.write_char((o2 + b'0') as char);
377    output.write_char((o1 + b'0') as char);
378    output.write_char((o0 + b'0') as char);
379    true
380}
381
382/// Create and implement a character set type using the `encoding` crate.
383macro_rules! decl_character_set {
384    ($typ: ident, $term: literal, $val: expr) => {
385        #[derive(Debug, Default, Copy, Clone, Eq, Hash, PartialEq)]
386        #[doc = "Data type for the "]
387        #[doc = $term]
388        #[doc = "character set encoding."]
389        pub struct $typ;
390
391        impl TextCodec for $typ {
392            fn name(&self) -> Cow<'static, str> {
393                Cow::Borrowed($term)
394            }
395
396            fn decode(&self, text: &[u8]) -> DecodeResult<String> {
397                $val.decode(text, DecoderTrap::Call(decode_text_trap))
398                    .map_err(|message| DecodeCustomSnafu { message }.build())
399            }
400
401            fn encode(&self, text: &str) -> EncodeResult<Vec<u8>> {
402                $val.encode(text, EncoderTrap::Strict)
403                    .map_err(|message| EncodeCustomSnafu { message }.build())
404            }
405        }
406    };
407}
408
409/// Data type representing the default character set.
410#[derive(Debug, Default, Copy, Clone, Eq, Hash, PartialEq)]
411pub struct DefaultCharacterSetCodec;
412
413impl TextCodec for DefaultCharacterSetCodec {
414    fn name(&self) -> Cow<'static, str> {
415        Cow::Borrowed("ISO_IR 6")
416    }
417
418    fn decode(&self, text: &[u8]) -> DecodeResult<String> {
419        // Using 8859-1 because it is a superset. Reiterations of this impl
420        // should check for invalid character codes (#40).
421        ISO_8859_1
422            .decode(text, DecoderTrap::Call(decode_text_trap))
423            .map_err(|message| DecodeCustomSnafu { message }.build())
424    }
425
426    fn encode(&self, text: &str) -> EncodeResult<Vec<u8>> {
427        ISO_8859_1
428            .encode(text, EncoderTrap::Strict)
429            .map_err(|message| EncodeCustomSnafu { message }.build())
430    }
431}
432
433decl_character_set!(IsoIr13CharacterSetCodec, "ISO_IR 13", WINDOWS_31J);
434decl_character_set!(IsoIr87CharacterSetCodec, "ISO_IR 87", ISO_2022_JP);
435decl_character_set!(IsoIr100CharacterSetCodec, "ISO_IR 100", ISO_8859_1);
436decl_character_set!(IsoIr101CharacterSetCodec, "ISO_IR 101", ISO_8859_2);
437decl_character_set!(IsoIr109CharacterSetCodec, "ISO_IR 109", ISO_8859_3);
438decl_character_set!(IsoIr110CharacterSetCodec, "ISO_IR 110", ISO_8859_4);
439decl_character_set!(IsoIr126CharacterSetCodec, "ISO_IR 126", ISO_8859_7);
440decl_character_set!(IsoIr127CharacterSetCodec, "ISO_IR 127", ISO_8859_6);
441decl_character_set!(IsoIr138CharacterSetCodec, "ISO_IR 138", ISO_8859_8);
442decl_character_set!(IsoIr144CharacterSetCodec, "ISO_IR 144", ISO_8859_5);
443decl_character_set!(IsoIr149CharacterSetCodec, "ISO_IR 149", WINDOWS_949);
444decl_character_set!(IsoIr166CharacterSetCodec, "ISO_IR 166", WINDOWS_874);
445decl_character_set!(Utf8CharacterSetCodec, "ISO_IR 192", UTF_8);
446decl_character_set!(Gb18030CharacterSetCodec, "GB18030", GB18030);
447decl_character_set!(GBKCharacterSetCodec, "GBK", GBK);
448
449/// The result of a text validation procedure (please see [`validate_iso_8859`]).
450#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
451pub enum TextValidationOutcome {
452    /// The text is fully valid and can be safely decoded.
453    Ok,
454    /// Some characters may have to be replaced, other than that the text can be safely decoded.
455    BadCharacters,
456    /// The text cannot be decoded.
457    NotOk,
458}
459
460/// Check whether the given byte slice contains valid text from the default character repertoire.
461pub fn validate_iso_8859(text: &[u8]) -> TextValidationOutcome {
462    if ISO_8859_1.decode(text, DecoderTrap::Strict).is_err() {
463        match ISO_8859_1.decode(text, DecoderTrap::Call(decode_text_trap)) {
464            Ok(_) => TextValidationOutcome::BadCharacters,
465            Err(_) => TextValidationOutcome::NotOk,
466        }
467    } else {
468        TextValidationOutcome::Ok
469    }
470}
471
472/// Check whether the given byte slice contains only valid characters for a
473/// Date value representation.
474pub fn validate_da(text: &[u8]) -> TextValidationOutcome {
475    if text.iter().cloned().all(|c| c.is_ascii_digit()) {
476        TextValidationOutcome::Ok
477    } else {
478        TextValidationOutcome::NotOk
479    }
480}
481
482/// Check whether the given byte slice contains only valid characters for a
483/// Time value representation.
484pub fn validate_tm(text: &[u8]) -> TextValidationOutcome {
485    if text.iter().cloned().all(|c| match c {
486        b'\\' | b'.' | b'-' | b' ' => true,
487        c => c.is_ascii_digit(),
488    }) {
489        TextValidationOutcome::Ok
490    } else {
491        TextValidationOutcome::NotOk
492    }
493}
494
495/// Check whether the given byte slice contains only valid characters for a
496/// Date Time value representation.
497pub fn validate_dt(text: &[u8]) -> TextValidationOutcome {
498    if text.iter().cloned().all(|c| match c {
499        b'.' | b'-' | b'+' | b' ' | b'\\' => true,
500        c => c.is_ascii_digit(),
501    }) {
502        TextValidationOutcome::Ok
503    } else {
504        TextValidationOutcome::NotOk
505    }
506}
507
508/// Check whether the given byte slice contains only valid characters for a
509/// Code String value representation.
510pub fn validate_cs(text: &[u8]) -> TextValidationOutcome {
511    if text.iter().cloned().all(|c| match c {
512        b' ' | b'_' => true,
513        c => c.is_ascii_digit() || c.is_ascii_uppercase(),
514    }) {
515        TextValidationOutcome::Ok
516    } else {
517        TextValidationOutcome::NotOk
518    }
519}
520
521#[cfg(test)]
522mod tests {
523    use super::*;
524
525    fn test_codec<T>(codec: T, string: &str, bytes: &[u8])
526    where
527        T: TextCodec,
528    {
529        assert_eq!(codec.encode(string).expect("encoding"), bytes);
530        assert_eq!(codec.decode(bytes).expect("decoding"), string);
531    }
532
533    #[test]
534    fn iso_ir_6_baseline() {
535        let codec = SpecificCharacterSet::default();
536        test_codec(codec, "Smith^John", b"Smith^John");
537    }
538
539    #[test]
540    fn iso_ir_13_baseline() {
541        let codec = SpecificCharacterSet(CharsetImpl::IsoIr13);
542        test_codec(codec, "ヤマダ^タロウ", b"\xd4\xcf\xc0\xde^\xc0\xdb\xb3");
543    }
544
545    #[test]
546    fn iso_ir_87_baseline() {
547        let codec = SpecificCharacterSet(CharsetImpl::IsoIr87);
548        test_codec(&codec, "山田^太郎", b"\x1b$B;3ED\x1b(B^\x1b$BB@O:");
549        test_codec(&codec, "やまだ^たろう", b"\x1b$B$d$^$@\x1b(B^\x1b$B$?$m$&");
550    }
551
552    #[test]
553    fn iso_ir_192_baseline() {
554        let codec = SpecificCharacterSet::ISO_IR_192;
555        test_codec(&codec, "Simões^John", "Simões^John".as_bytes());
556        test_codec(codec, "Иванков^Андрей", "Иванков^Андрей".as_bytes());
557    }
558
559    #[test]
560    fn iso_ir_100_baseline() {
561        let codec = SpecificCharacterSet(CharsetImpl::IsoIr100);
562        test_codec(&codec, "Simões^João", b"Sim\xF5es^Jo\xE3o");
563        test_codec(codec, "Günther^Hans", b"G\xfcnther^Hans");
564    }
565
566    #[test]
567    fn iso_ir_101_baseline() {
568        let codec = SpecificCharacterSet(CharsetImpl::IsoIr101);
569        test_codec(codec, "Günther^Hans", b"G\xfcnther^Hans");
570    }
571
572    #[test]
573    fn iso_ir_110_baseline() {
574        let codec = SpecificCharacterSet(CharsetImpl::IsoIr110);
575        test_codec(codec, "ĄĸŖĨϧŠĒĢŦŽĀÁÂÃÄÅÆĮČÉ^ĘËĖÍÎĪĐŅŌĶÔÕÖרŲÚÛÜŨŪß", b"\xA1\xA2\xA3\xA5\xA6\xA7\xA9\xAA\xAB\xAC\xAE\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9^\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF");
576    }
577
578    #[test]
579    fn iso_ir_126_baseline() {
580        let codec = SpecificCharacterSet(CharsetImpl::IsoIr126);
581        test_codec(codec, "Διονυσιος", b"\xC4\xE9\xEF\xED\xF5\xF3\xE9\xEF\xF2");
582    }
583
584    #[test]
585    fn iso_ir_127_baseline() {
586        let codec = SpecificCharacterSet(CharsetImpl::IsoIr127);
587        test_codec(
588            codec,
589            "قباني^لنزار",
590            b"\xE2\xC8\xC7\xE6\xEA^\xE4\xE6\xD2\xC7\xD1",
591        );
592    }
593
594    #[test]
595    fn iso_ir_138_baseline() {
596        let codec = SpecificCharacterSet(CharsetImpl::IsoIr138);
597        test_codec(
598            &codec,
599            "מקור השם עברית",
600            b"\xEE\xF7\xE5\xF8\x20\xE4\xF9\xED\x20\xF2\xE1\xF8\xE9\xFA",
601        );
602        test_codec(
603            codec,
604            "שרון^דבורה",
605            b"\xF9\xF8\xE5\xEF^\xE3\xE1\xE5\xF8\xE4",
606        );
607    }
608
609    #[test]
610    fn iso_ir_144_baseline() {
611        let codec = SpecificCharacterSet(CharsetImpl::IsoIr144);
612        test_codec(
613            &codec,
614            "Иванков^Андрей",
615            b"\xb8\xd2\xd0\xdd\xda\xde\xd2^\xb0\xdd\xd4\xe0\xd5\xd9",
616        );
617        test_codec(
618            &codec,
619            "Гол. мозг стандарт",
620            b"\xB3\xDE\xDB.\x20\xDC\xDE\xD7\xD3\x20\xE1\xE2\xD0\xDD\xD4\xD0\xE0\xE2",
621        );
622        test_codec(&codec, "мозг 2мм", b"\xDC\xDE\xD7\xD3\x202\xDC\xDC");
623    }
624
625    #[test]
626    fn iso_ir_149_baseline() {
627        let codec = SpecificCharacterSet(CharsetImpl::IsoIr149);
628        test_codec(&codec, "김희중", b"\xB1\xE8\xC8\xF1\xC1\xDF");
629        test_codec(
630            codec,
631            "Hong^Gildong=洪^吉洞=홍^길동",
632            b"Hong^Gildong=\xFB\xF3^\xD1\xCE\xD4\xD7=\xC8\xAB^\xB1\xE6\xB5\xBF",
633        );
634    }
635
636    #[test]
637    fn iso_ir_166_baseline() {
638        let codec = SpecificCharacterSet(CharsetImpl::IsoIr166);
639        test_codec(&codec, "ประเทศไทย", b"\xBB\xC3\xD0\xE0\xB7\xC8\xE4\xB7\xC2");
640        test_codec(codec, "รหัสสำหรับอักขระไทยที่ใช้กับคอมพิวเตอร์", b"\xC3\xCB\xD1\xCA\xCA\xD3\xCB\xC3\xD1\xBA\xCD\xD1\xA1\xA2\xC3\xD0\xE4\xB7\xC2\xB7\xD5\xE8\xE3\xAA\xE9\xA1\xD1\xBA\xA4\xCD\xC1\xBE\xD4\xC7\xE0\xB5\xCD\xC3\xEC");
641    }
642
643    #[test]
644    fn gb_18030_baseline() {
645        let codec = SpecificCharacterSet(CharsetImpl::Gb18030);
646        test_codec(
647            &codec,
648            "Wang^XiaoDong=王^小东",
649            b"Wang^XiaoDong=\xCD\xF5^\xD0\xA1\xB6\xAB",
650        );
651    }
652    #[test]
653    fn gb_gbk_baseline() {
654        let codec = SpecificCharacterSet(CharsetImpl::Gbk);
655
656        let iso2022_ir58_bytes = vec![
657            0xB0, 0xB2, 0xBB, 0xD5, 0xD0, 0xC7, 0xC1, 0xE9, 0xD0, 0xC5, 0xCF, 0xA2, 0xBF, 0xC6,
658            0xBC, 0xBC, 0xD3, 0xD0, 0xCF, 0xDE, 0xB9, 0xAB, 0xCB, 0xBE,
659        ];
660        let rw = codec.decode(&iso2022_ir58_bytes).expect("decoding");
661
662        assert_eq!(rw, "安徽星灵信息科技有限公司");
663
664        let gb2312_bytes = vec![
665            0xCA, 0xB9, 0xC6, 0xE4, 0xD3, 0xEB, 0xD4, 0xAD, 0xCA, 0xBC, 0xB2, 0xD6, 0xBF, 0xE2,
666            0xB1, 0xA3, 0xB3, 0xD6, 0xD2, 0xBB, 0xD6, 0xC2,
667        ];
668        let rw2 = codec.decode(&gb2312_bytes).expect("decoding");
669
670        assert_eq!(rw2, "使其与原始仓库保持一致");
671    }
672}