use dicom_core::{DataDictionary, Tag};
use dicom_dictionary_std::StandardDataDictionary;
use dicom_encoding::transfer_syntax::TransferSyntaxIndex;
use dicom_transfer_syntax_registry::TransferSyntaxRegistry;
pub use dicom_parser::dataset::read::OddLengthStrategy;
pub use dicom_parser::stateful::decode::CharacterSetOverride;
use crate::{DefaultDicomObject, ReadError};
use std::io::Read;
use std::path::Path;
pub type Result<T, E = ReadError> = std::result::Result<T, E>;
pub fn from_reader<F>(file: F) -> Result<DefaultDicomObject>
where
F: Read,
{
OpenFileOptions::new().from_reader(file)
}
pub fn open_file<P>(path: P) -> Result<DefaultDicomObject>
where
P: AsRef<Path>,
{
OpenFileOptions::new().open_file(path)
}
#[derive(Debug, Default, Clone)]
#[non_exhaustive]
pub struct OpenFileOptions<D = StandardDataDictionary, T = TransferSyntaxRegistry> {
data_dictionary: D,
ts_index: T,
read_until: Option<Tag>,
read_preamble: ReadPreamble,
odd_length: OddLengthStrategy,
charset_override: CharacterSetOverride,
}
impl OpenFileOptions {
pub fn new() -> Self {
OpenFileOptions::default()
}
}
impl<D, T> OpenFileOptions<D, T> {
pub fn read_until(mut self, tag: Tag) -> Self {
self.read_until = Some(tag);
self
}
pub fn read_all(mut self) -> Self {
self.read_until = None;
self
}
pub fn read_preamble(mut self, option: ReadPreamble) -> Self {
self.read_preamble = option;
self
}
pub fn odd_length_strategy(mut self, option: OddLengthStrategy) -> Self {
self.odd_length = option;
self
}
pub fn charset_override(mut self, option: CharacterSetOverride) -> Self {
self.charset_override = option;
self
}
pub fn transfer_syntax_index<Tr>(self, ts_index: Tr) -> OpenFileOptions<D, Tr>
where
Tr: TransferSyntaxIndex,
{
OpenFileOptions {
data_dictionary: self.data_dictionary,
read_until: self.read_until,
read_preamble: self.read_preamble,
ts_index,
odd_length: self.odd_length,
charset_override: self.charset_override,
}
}
#[deprecated(since = "0.8.1", note = "please use `transfer_syntax_index` instead")]
pub fn tranfer_syntax_index<Tr>(self, ts_index: Tr) -> OpenFileOptions<D, Tr>
where
Tr: TransferSyntaxIndex,
{
self.transfer_syntax_index(ts_index)
}
pub fn dictionary<Di>(self, dict: Di) -> OpenFileOptions<Di, T>
where
Di: DataDictionary,
Di: Clone,
{
OpenFileOptions {
data_dictionary: dict,
read_until: self.read_until,
read_preamble: self.read_preamble,
ts_index: self.ts_index,
odd_length: self.odd_length,
charset_override: self.charset_override,
}
}
pub fn open_file<P>(self, path: P) -> Result<DefaultDicomObject<D>>
where
P: AsRef<Path>,
D: DataDictionary,
D: Clone,
T: TransferSyntaxIndex,
{
DefaultDicomObject::open_file_with_all_options(
path,
self.data_dictionary,
self.ts_index,
self.read_until,
self.read_preamble,
self.odd_length,
self.charset_override,
)
}
pub fn from_reader<R>(self, from: R) -> Result<DefaultDicomObject<D>>
where
R: Read,
D: DataDictionary,
D: Clone,
T: TransferSyntaxIndex,
{
DefaultDicomObject::from_reader_with_all_options(
from,
self.data_dictionary,
self.ts_index,
self.read_until,
self.read_preamble,
self.odd_length,
self.charset_override,
)
}
}
#[derive(Debug, Default, Copy, Clone, Eq, Hash, PartialEq)]
pub enum ReadPreamble {
#[default]
Auto,
Never,
Always,
}