mod generated;
use std::fmt;
use std::ops::Deref;
use std::str::FromStr;
use crate::error::{Error, Result};
pub use generated::Entry;
const NATIVE: &[&str] = &[
"1.2.840.10008.1.2", "1.2.840.10008.1.2.1", "1.2.840.10008.1.2.2", "1.2.840.10008.1.2.1.99", ];
const DEFLATED: &[&str] = &[
"1.2.840.10008.1.2.1.99", "1.2.840.10008.1.2.4.95", "1.2.840.10008.1.2.4.205", ];
const IMPLICIT_VR_LITTLE_ENDIAN: &str = "1.2.840.10008.1.2";
const EXPLICIT_VR_BIG_ENDIAN: &str = "1.2.840.10008.1.2.2";
const DICOM_ROOT: &str = "1.2.840.10008.";
pub fn is_valid(value: &str) -> bool {
if value.is_empty() || value.len() > 64 {
return false;
}
value.split('.').all(|component| {
!component.is_empty()
&& component.bytes().all(|byte| byte.is_ascii_digit())
&& (component == "0" || !component.starts_with('0'))
})
}
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Uid {
value: String,
private_encoding: Option<(bool, bool)>,
}
impl Uid {
pub fn new(value: impl Into<String>) -> Result<Self> {
let value: String = value.into();
let trimmed = value.trim().to_owned();
if !is_valid(&trimmed) {
return Err(Error::InvalidUid(trimmed));
}
Ok(Self {
value: trimmed,
private_encoding: None,
})
}
pub fn as_str(&self) -> &str {
&self.value
}
pub fn entry(&self) -> Option<&'static Entry> {
registry(&self.value)
}
pub fn name(&self) -> &str {
self.entry().map_or(&self.value, |entry| entry.name)
}
pub fn keyword(&self) -> &str {
self.entry().map_or("", |entry| entry.keyword)
}
pub fn kind(&self) -> &str {
self.entry().map_or("", |entry| entry.kind)
}
pub fn info(&self) -> &str {
self.entry().map_or("", |entry| entry.info)
}
pub fn is_retired(&self) -> bool {
self.entry().is_some_and(|entry| entry.retired)
}
pub fn is_private(&self) -> bool {
!self.value.starts_with(DICOM_ROOT)
}
pub fn is_valid(&self) -> bool {
is_valid(&self.value)
}
pub fn is_transfer_syntax(&self) -> bool {
match self.entry() {
Some(entry) => entry.kind == "Transfer Syntax",
None => self.private_encoding.is_some(),
}
}
pub fn is_implicit_vr(&self) -> Option<bool> {
if let Some((implicit, _)) = self.private_encoding {
return Some(implicit);
}
if !self.is_transfer_syntax() {
return None;
}
Some(self.value == IMPLICIT_VR_LITTLE_ENDIAN)
}
pub fn is_little_endian(&self) -> Option<bool> {
if let Some((_, little_endian)) = self.private_encoding {
return Some(little_endian);
}
if !self.is_transfer_syntax() {
return None;
}
Some(self.value != EXPLICIT_VR_BIG_ENDIAN)
}
pub fn is_deflated(&self) -> Option<bool> {
if !self.is_transfer_syntax() {
return None;
}
Some(DEFLATED.contains(&self.value.as_str()))
}
pub fn is_compressed(&self) -> Option<bool> {
if !self.is_transfer_syntax() {
return None;
}
Some(!NATIVE.contains(&self.value.as_str()))
}
pub fn is_encapsulated(&self) -> Option<bool> {
self.is_compressed()
}
pub fn with_private_encoding(mut self, implicit_vr: bool, little_endian: bool) -> Self {
self.private_encoding = Some((implicit_vr, little_endian));
self
}
}
impl Deref for Uid {
type Target = str;
fn deref(&self) -> &str {
&self.value
}
}
impl fmt::Display for Uid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.value)
}
}
impl FromStr for Uid {
type Err = Error;
fn from_str(value: &str) -> Result<Self> {
Self::new(value)
}
}
impl PartialEq<str> for Uid {
fn eq(&self, other: &str) -> bool {
self.value == other
}
}
impl PartialEq<&str> for Uid {
fn eq(&self, other: &&str) -> bool {
self.value == *other
}
}
fn registry(value: &str) -> Option<&'static Entry> {
generated::ENTRIES
.binary_search_by(|entry| entry.uid.cmp(value))
.ok()
.map(|index| &generated::ENTRIES[index])
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_table_is_sorted_for_binary_search() {
let mut previous = "";
for entry in generated::ENTRIES {
assert!(entry.uid > previous, "{} out of order", entry.uid);
previous = entry.uid;
assert!(
!entry.name.is_empty() || entry.retired,
"{} has no name but is not retired",
entry.uid
);
}
}
#[test]
fn every_native_syntax_is_registered() {
for uid in NATIVE.iter().chain(DEFLATED) {
let entry = registry(uid).unwrap_or_else(|| panic!("{uid} is not registered"));
assert_eq!(entry.kind, "Transfer Syntax");
}
}
}