use std::fmt;
use std::str::Utf8Error;
use crate::tag::Tag;
use crate::vr::Vr;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Error {
InvalidTag(String),
UnknownVr(String),
WrongVr {
actual: Vr,
expected: &'static str,
},
WrongLength {
vr: Vr,
len: usize,
unit: usize,
},
InvalidUtf8(Utf8Error),
InvalidNumber(String),
MissingTag(Tag),
NoValue(Tag),
NotASequence(Vr),
}
pub type Result<T> = std::result::Result<T, Error>;
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidTag(text) => write!(f, "invalid tag text: {text:?}"),
Self::UnknownVr(text) => write!(f, "unknown VR: {text:?}"),
Self::WrongVr { actual, expected } => {
write!(f, "VR mismatch: expected {expected}, got {actual}")
}
Self::WrongLength { vr, len, unit } => {
write!(
f,
"value byte length {len} is not a multiple of {unit} (VR={vr})"
)
}
Self::InvalidUtf8(err) => write!(f, "value bytes are not valid UTF-8: {err}"),
Self::InvalidNumber(text) => write!(f, "could not parse number: {text:?}"),
Self::MissingTag(tag) => write!(f, "tag {tag} is missing from the data set"),
Self::NoValue(tag) => write!(f, "tag {tag} has no value"),
Self::NotASequence(vr) => write!(f, "VR {vr} is not a sequence"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::InvalidUtf8(err) => Some(err),
_ => None,
}
}
}