Skip to main content

dcmnorm_object/
error.rs

1//! Simple, dcmnorm-owned error types for this crate's read/write/meta operations.
2//!
3//! `dicom-object`'s equivalents are large multi-variant `snafu` enums covering every possible
4//! low-level failure mode. dcmnorm never matches on a specific variant of any of them (confirmed
5//! by grep across `src/dicom_io/*.rs`) - only propagates them via `?` and formats them via
6//! `Display` - so there's no reason to replicate that surface here.
7
8use std::fmt;
9use std::io;
10
11/// An error occurring while reading a DICOM data set or file.
12#[derive(Debug)]
13pub enum ReadError {
14    Io {
15        source: io::Error,
16        context: &'static str,
17    },
18    Dataset {
19        source: dcmnorm_parser::dataset::read::Error,
20    },
21    NotDicom,
22    UnsupportedTransferSyntax {
23        uid: String,
24    },
25    UnexpectedToken {
26        context: &'static str,
27    },
28}
29
30impl fmt::Display for ReadError {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            ReadError::Io { source, context } => write!(f, "I/O error while {context}: {source}"),
34            ReadError::Dataset { source } => write!(f, "failed to read data set: {source}"),
35            ReadError::NotDicom => {
36                write!(f, "not a valid DICOM file (missing preamble/DICM magic)")
37            }
38            ReadError::UnsupportedTransferSyntax { uid } => {
39                write!(f, "unsupported transfer syntax: {uid}")
40            }
41            ReadError::UnexpectedToken { context } => {
42                write!(f, "unexpected data set token while {context}")
43            }
44        }
45    }
46}
47
48impl std::error::Error for ReadError {
49    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
50        match self {
51            ReadError::Io { source, .. } => Some(source),
52            ReadError::Dataset { source } => Some(source),
53            _ => None,
54        }
55    }
56}
57
58impl From<dcmnorm_parser::dataset::read::Error> for ReadError {
59    fn from(source: dcmnorm_parser::dataset::read::Error) -> Self {
60        ReadError::Dataset { source }
61    }
62}
63
64/// An error occurring while writing a DICOM data set or file.
65#[derive(Debug)]
66pub enum WriteError {
67    Io {
68        source: io::Error,
69        context: &'static str,
70    },
71    Dataset {
72        source: dcmnorm_parser::dataset::write::Error,
73    },
74    UnsupportedTransferSyntax {
75        uid: String,
76    },
77}
78
79impl fmt::Display for WriteError {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        match self {
82            WriteError::Io { source, context } => write!(f, "I/O error while {context}: {source}"),
83            WriteError::Dataset { source } => write!(f, "failed to write data set: {source}"),
84            WriteError::UnsupportedTransferSyntax { uid } => {
85                write!(f, "unsupported transfer syntax: {uid}")
86            }
87        }
88    }
89}
90
91impl std::error::Error for WriteError {
92    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
93        match self {
94            WriteError::Io { source, .. } => Some(source),
95            WriteError::Dataset { source } => Some(source),
96            _ => None,
97        }
98    }
99}
100
101impl From<dcmnorm_parser::dataset::write::Error> for WriteError {
102    fn from(source: dcmnorm_parser::dataset::write::Error) -> Self {
103        WriteError::Dataset { source }
104    }
105}
106
107/// An error occurring while attaching/validating a file meta table.
108#[derive(Debug)]
109pub enum WithMetaError {
110    Read { source: ReadError },
111    MissingField { field: &'static str },
112}
113
114impl fmt::Display for WithMetaError {
115    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116        match self {
117            WithMetaError::Read { source } => write!(f, "{source}"),
118            WithMetaError::MissingField { field } => {
119                write!(f, "missing required file meta field: {field}")
120            }
121        }
122    }
123}
124
125impl std::error::Error for WithMetaError {
126    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
127        match self {
128            WithMetaError::Read { source } => Some(source),
129            WithMetaError::MissingField { .. } => None,
130        }
131    }
132}
133
134impl From<ReadError> for WithMetaError {
135    fn from(source: ReadError) -> Self {
136        WithMetaError::Read { source }
137    }
138}