imc_rs/error.rs
1use std::{io, num::TryFromIntError, result, str::Utf8Error, string::FromUtf16Error};
2
3use lz4_flex::block::DecompressError;
4use thiserror::Error;
5
6use crate::ChannelIdentifier;
7
8/// A type alias for `Result<T, imc_rs::MCDError>`.
9pub type Result<T> = result::Result<T, MCDError>;
10
11/// Describes what has gone wrong with reading an .mcd file
12#[derive(Error, Debug)]
13pub enum MCDError {
14 /// An I/O error occurred
15 #[error("An I/O error occured")]
16 Io {
17 #[from]
18 /// The original error that was raised.
19 source: io::Error,
20 //backtrace: Backtrace,
21 },
22 /// Requested spectrum index is outside of expected range
23 #[error("index `{index}` not in range (0..{num_spectra})")]
24 InvalidIndex {
25 /// The index specified
26 index: usize,
27 /// The number of spectra for the given acquisition
28 num_spectra: usize,
29 },
30 /// Issue when decompressing binary data
31 #[error("An error occured when decompressing: {source}")]
32 Decompress {
33 /// The original error that was raised
34 #[from]
35 source: DecompressError,
36 },
37 /// No channel exists which matches the specified `ChannelIdentifier`
38 #[error("No such channel exists")]
39 InvalidChannel {
40 /// Channel identifier of the unknown channel.
41 channel: ChannelIdentifier,
42 },
43 /// No slide present in MCD file, so likely this is not a valid .mcd file.
44 #[error("No slide found in MCD file - is this a valid .mcd file?")]
45 NoSlidePresent,
46
47 /// The location of the .mcd file is required to generate a .dcm file. If this is not
48 /// specified either by using .from_path() or .set_location() then this error will occur.
49 #[error("No location specified, so can't generate a .dcm file.")]
50 LocationNotSpecified,
51
52 /// An error occurred when converting XML to UTF-16
53 #[error("An error occurred when converting XML to UTF-16: {source}")]
54 Utf16Erorr {
55 #[from]
56 /// The original error that was raised.
57 source: FromUtf16Error,
58 },
59
60 /// An unknown tag appeared in the XML file
61 #[error("An unknown tag appeared in the XML file: {name}")]
62 UnknownTag {
63 /// Name of the tag that was unexpectedly present.
64 name: String,
65 },
66
67 /// An error occured when parsing part of the XML file (conversion to UTF-8)
68 #[error("An error occured when parsing part of the XML file (conversion to UTF-8): {source}")]
69 InvalidUtf8 {
70 #[from]
71 /// The original error that was raised.
72 source: Utf8Error,
73 },
74
75 /// An error occured when parsing the XML file
76 #[error("An error occured when parsing the XML file: {source}")]
77 InvalidXML {
78 #[from]
79 /// The original error that was raised.
80 source: quick_xml::Error,
81 },
82
83 /// An error occured when parsing an image.
84 #[error("An error occured when parsing an image: {source}")]
85 ImageError {
86 #[from]
87 /// The original error that was raised.
88 source: image::ImageError,
89 },
90
91 /// An error occured when locking the reader.
92 #[error("An error occured when locking the reader.")]
93 PoisonMutex,
94
95 /// Invalid offset in file.
96 #[error("Invalid offset in file: {offset}")]
97 InvalidOffset {
98 /// Offset not reachable in the file.
99 offset: i64,
100 },
101
102 /// An error occured when trying to convert from an integer.
103 #[error("Could not convert value to unsigned integer: {source}.")]
104 TryFromIntError {
105 #[from]
106 /// The original error that was raised.
107 source: TryFromIntError,
108 },
109}