nifti/error.rs
1//! Types for error handling go here.
2use crate::typedef::NiftiType;
3use quick_error::quick_error;
4use std::io::Error as IOError;
5
6quick_error! {
7 /// Error type for all error variants originated by this crate.
8 #[derive(Debug)]
9 #[non_exhaustive]
10 pub enum NiftiError {
11 /// An invalid NIfTI-1 file was parsed.
12 /// This is detected when reading the file's magic code,
13 /// which should be either `b"ni1\0"` or `b"n+1\0`.
14 InvalidFormat {
15 display("Invalid NIfTI-1 file")
16 }
17 /// The field `dim` is in an invalid state, as a consequence of
18 /// `dim[0]` or one of the elements in `1..dim[0] + 1` not being
19 /// positive.
20 InconsistentDim(index: u8, value: u16) {
21 display("Inconsistent value `{}` in header field dim[{}] ({})", value, index, match index {
22 0 if *value > 7 => "must not be higher than 7",
23 _ => "must be positive"
24 })
25 }
26 /// Attempted to read volume outside boundaries.
27 OutOfBounds(coords: Vec<u16>) {
28 display("Out of bounds access to volume: {:?}", &coords[..])
29 }
30 /// Attempted to read a volume over a volume's unexistent dimension.
31 AxisOutOfBounds(axis: u16) {
32 display("Out of bounds access to volume (axis {})", axis)
33 }
34 /// Could not retrieve a volume file based on the given header file.
35 MissingVolumeFile(err: IOError) {
36 source(err)
37 display("Volume file not found")
38 }
39 /// An attempt to read a complete NIFTI-1 object from a header file
40 /// was made. It can also be triggered when a NIFTI object contains
41 /// the magic code "ni-1\0", even if the following bytes contain the volume.
42 NoVolumeData {
43 display("No volume data available")
44 }
45 /// An incorrect number of dimensions was provided when interacting
46 /// with a volume.
47 IncorrectVolumeDimensionality(expected: u16, got: u16) {
48 display("Unexpected volume data dimensionality (expected {}, got {})", expected, got)
49 }
50 /// Inconsistent or unsupported volume size (due to one or more
51 /// dimensions being too large).
52 BadVolumeSize {
53 display("Bad volume size")
54 }
55 /// This voxel data type is not supported. Sorry. :(
56 UnsupportedDataType(t: NiftiType) {
57 display("Unsupported data type")
58 }
59 /// I/O Error
60 Io(err: IOError) {
61 from()
62 source(err)
63 }
64 /// Raw data buffer length and volume dimensions are incompatible
65 IncompatibleLength(got: usize, expected: usize) {
66 display("The buffer length ({}) and header dimensions ({} elements) are incompatible", got, expected)
67 }
68 /// Description length must be lower than or equal to 80 bytes
69 IncorrectDescriptionLength(len: usize) {
70 display("Description length ({} bytes) is greater than 80 bytes.", len)
71 }
72 /// Header contains a code which is not valid for the given attribute
73 InvalidCode(typename: &'static str, code: i16) {
74 display("invalid code `{}` for header field {}", code, typename)
75 }
76 /// Could not reserve enough memory for volume data
77 ReserveVolume(bytes: usize, err: std::collections::TryReserveError) {
78 display("Could not reserve {} bytes of memory for volume data", bytes)
79 source(err)
80 }
81 /// Could not reserve enough memory for extended data
82 ReserveExtended(bytes: usize, err: std::collections::TryReserveError) {
83 display("Could not reserve {} bytes of memory for extended data", bytes)
84 source(err)
85 }
86
87 /// Attempted a type conversion that is not supported by this crate
88 InvalidTypeConversion(from: NiftiType, to: &'static str) {
89 display("Invalid type conversion from {:?} to {}", from, to)
90 }
91 }
92}
93
94/// Alias type for results originated from this crate.
95pub type Result<T> = ::std::result::Result<T, NiftiError>;