use std::fmt;
use crate::endian::Endian;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum TransferSyntax {
ImplicitVrLittleEndian,
ExplicitVrLittleEndian,
ExplicitVrBigEndian,
DeflatedExplicitVrLittleEndian,
Encapsulated(&'static str),
}
const ENCAPSULATED: &[&str] = &[
"1.2.840.10008.1.2.4.50",
"1.2.840.10008.1.2.4.51",
"1.2.840.10008.1.2.4.52",
"1.2.840.10008.1.2.4.53",
"1.2.840.10008.1.2.4.54",
"1.2.840.10008.1.2.4.55",
"1.2.840.10008.1.2.4.56",
"1.2.840.10008.1.2.4.57",
"1.2.840.10008.1.2.4.70",
"1.2.840.10008.1.2.4.80",
"1.2.840.10008.1.2.4.81",
"1.2.840.10008.1.2.4.90",
"1.2.840.10008.1.2.4.91",
"1.2.840.10008.1.2.4.92",
"1.2.840.10008.1.2.4.93",
"1.2.840.10008.1.2.4.94",
"1.2.840.10008.1.2.4.95",
"1.2.840.10008.1.2.4.100",
"1.2.840.10008.1.2.4.101",
"1.2.840.10008.1.2.4.102",
"1.2.840.10008.1.2.4.103",
"1.2.840.10008.1.2.4.104",
"1.2.840.10008.1.2.4.105",
"1.2.840.10008.1.2.4.106",
"1.2.840.10008.1.2.4.107",
"1.2.840.10008.1.2.4.108",
"1.2.840.10008.1.2.4.110",
"1.2.840.10008.1.2.4.111",
"1.2.840.10008.1.2.4.112",
"1.2.840.10008.1.2.4.201",
"1.2.840.10008.1.2.4.202",
"1.2.840.10008.1.2.4.203",
"1.2.840.10008.1.2.4.204",
"1.2.840.10008.1.2.5",
];
impl TransferSyntax {
pub fn from_uid(uid: &str) -> Option<Self> {
let uid = uid.trim_matches(|ch: char| ch.is_whitespace() || ch == '\0');
match uid {
"1.2.840.10008.1.2" => Some(Self::ImplicitVrLittleEndian),
"1.2.840.10008.1.2.1" => Some(Self::ExplicitVrLittleEndian),
"1.2.840.10008.1.2.2" => Some(Self::ExplicitVrBigEndian),
"1.2.840.10008.1.2.1.99" => Some(Self::DeflatedExplicitVrLittleEndian),
_ => ENCAPSULATED
.iter()
.find(|known| **known == uid)
.map(|known| Self::Encapsulated(known)),
}
}
pub const fn uid(self) -> &'static str {
match self {
Self::ImplicitVrLittleEndian => "1.2.840.10008.1.2",
Self::ExplicitVrLittleEndian => "1.2.840.10008.1.2.1",
Self::ExplicitVrBigEndian => "1.2.840.10008.1.2.2",
Self::DeflatedExplicitVrLittleEndian => "1.2.840.10008.1.2.1.99",
Self::Encapsulated(uid) => uid,
}
}
pub const fn endian(self) -> Endian {
match self {
Self::ExplicitVrBigEndian => Endian::Big,
_ => Endian::Little,
}
}
pub const fn is_explicit_vr(self) -> bool {
!matches!(self, Self::ImplicitVrLittleEndian)
}
pub const fn is_encapsulated(self) -> bool {
matches!(self, Self::Encapsulated(_))
}
}
impl fmt::Display for TransferSyntax {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.uid())
}
}