use crate::value::ValueType;
use quick_error::quick_error;
use std::error::Error as BaseError;
use std::fmt;
use std::num::{ParseFloatError, ParseIntError};
use std::result;
quick_error! {
#[derive(Debug)]
pub enum Error {
UnexpectedElement {
description("Unexpected DICOM element in current reading position")
}
UnexpectedDataValueLength {
description("Inconsistent data value length in data element")
}
ReadValue(err: InvalidValueReadError) {
description("Invalid value read")
from()
cause(err)
display(self_) -> ("{}: {}", self_.description(), err.description())
}
CastValue(err: CastValueError) {
description("Failed value cast")
from()
cause(err)
display(self_) -> ("{}: {}", self_.description(), err.description())
}
}
}
pub type Result<T> = result::Result<T, Error>;
quick_error! {
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum InvalidValueReadError {
NonPrimitiveType {
description("attempted to retrieve complex value as primitive")
display(self_) -> ("{}", self_.description())
}
UnresolvedValueLength {
description("value length could not be resolved")
display(self_) -> ("{}", self_.description())
}
InvalidToken(got: u8, expected: &'static str) {
description("Invalid token received for the expected value representation")
display(self_) -> ("invalid token: expected {} but got {:?}", expected, got)
}
InvalidLength(got: usize, expected: &'static str) {
description("Invalid slice length for the expected value representation")
display(self_) -> ("invalid length: expected {} but got {}", expected, got)
}
ParseDateTime(got: u32, expected: &'static str) {
description("Invalid date/time component")
display(self_) -> ("invalid date/time component: expected {} but got {}", expected, got)
}
DateTimeZone {
description("Invalid or ambiguous combination of date with time")
display(self_) -> ("{}", self_.description())
}
Chrono(err: chrono::ParseError) {
description("failed to parse date/time")
from()
cause(err)
display(self_) -> ("{}", self_.source().unwrap())
}
ParseFloat(err: ParseFloatError) {
description("Failed to parse text value as a floating point number")
from()
cause(err)
display(self_) -> ("{}", self_.description())
}
ParseInteger(err: ParseIntError) {
description("Failed to parse text value as an integer")
from()
cause(err)
display(self_) -> ("{}", err.description())
}
UnexpectedEndOfElement {
description("Unexpected end of element")
display(self_) -> ("{}", self_.description())
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct CastValueError {
pub requested: &'static str,
pub got: ValueType,
}
impl fmt::Display for CastValueError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}: requested {} but value is {:?}",
self.description(),
self.requested,
self.got
)
}
}
impl ::std::error::Error for CastValueError {
fn description(&self) -> &str {
"bad value cast"
}
}