dcm4rs 0.2.0

Pure-Rust DICOM data model, data dictionary and file I/O
Documentation
//! DICOM data dictionary (PS3.6 Chapter 6, Registry of DICOM Data Elements).
//!
//! The tables are generated by `dictgen` in the `cli` package from the official DocBook source;
//! the generated file is committed, so building needs neither network access nor XML parsing.

use crate::{Tag, Vr};

/// The VR declared by the dictionary; a few elements are ambiguous and need context (neighbouring
/// attributes) to decide.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum VrDef {
    /// A single, definite VR.
    Fixed(Vr),
    /// A set of possible VRs, e.g. `US or SS`.
    Ambiguous(&'static [Vr]),
}

impl VrDef {
    /// Whether the given VR is possible.
    pub fn contains(self, vr: Vr) -> bool {
        match self {
            Self::Fixed(fixed) => fixed == vr,
            Self::Ambiguous(set) => set.contains(&vr),
        }
    }

    /// The VR when unique; `None` when ambiguous.
    pub fn fixed(self) -> Option<Vr> {
        match self {
            Self::Fixed(vr) => Some(vr),
            Self::Ambiguous(_) => None,
        }
    }

    /// Whether the VR is ambiguous.
    pub fn is_ambiguous(self) -> bool {
        matches!(self, Self::Ambiguous(_))
    }
}

/// A standard data element entry.
#[derive(Clone, Copy, Debug)]
pub struct Entry {
    /// The exact tag. For repeater entries (see [`Repeater`]) this is the pattern's base value
    /// with the `x` nibbles as 0; the actual tag follows the query.
    pub tag: Tag,
    /// Keyword, e.g. `PatientName`.
    pub keyword: &'static str,
    /// Standard name, e.g. `Patient's Name`.
    pub name: &'static str,
    /// The VR.
    pub vr: VrDef,
    /// Value multiplicity, e.g. `1`, `1-n`, `2-n`.
    pub vm: &'static str,
    /// Whether the element is retired.
    pub retired: bool,
}

/// A repeating-group entry, e.g. `(60xx,0010)` Overlay Rows.
#[derive(Clone, Copy, Debug)]
pub struct Repeater {
    /// Mask: bitwise AND with the tag yields `value`.
    pub mask: u32,
    /// Expected value after masking.
    pub value: u32,
    /// The entry (its `tag` is the pattern base value).
    pub entry: Entry,
}

mod generated;

pub use generated::tags;

/// All standard data elements, in ascending tag order.
pub fn entries() -> &'static [Entry] {
    generated::ENTRIES
}

/// All repeating-group entries.
pub fn repeaters() -> &'static [Repeater] {
    generated::REPEATERS
}

/// Look up an entry by tag: exact entries first, then repeaters by mask.
pub fn lookup(tag: Tag) -> Option<&'static Entry> {
    if let Ok(index) = generated::ENTRIES.binary_search_by(|entry| entry.tag.cmp(&tag)) {
        return Some(&generated::ENTRIES[index]);
    }
    let raw = tag.as_u32();
    generated::REPEATERS
        .iter()
        .find(|repeater| raw & repeater.mask == repeater.value)
        .map(|repeater| &repeater.entry)
}

/// Look up the VR by tag.
pub fn vr(tag: Tag) -> Option<VrDef> {
    lookup(tag).map(|entry| entry.vr)
}

/// Look up an entry by keyword (case insensitive).
pub fn lookup_keyword(keyword: &str) -> Option<&'static Entry> {
    generated::ENTRIES
        .iter()
        .find(|entry| entry.keyword.eq_ignore_ascii_case(keyword))
}