atglib/utils/
errors.rs

1use core::num::TryFromIntError;
2use std::error::Error;
3use std::fmt;
4use std::num::ParseIntError;
5
6pub struct AtgError {
7    message: String,
8}
9
10impl Error for AtgError {}
11
12impl AtgError {
13    pub fn new<S: fmt::Display>(s: S) -> Self {
14        Self {
15            message: s.to_string(),
16        }
17    }
18}
19
20impl fmt::Display for AtgError {
21    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22        // user-facing error
23        write!(f, "{}", self.message)
24    }
25}
26
27impl fmt::Debug for AtgError {
28    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29        // developer-facing error
30        write!(f, "{{ file: {}, line: {} }}", file!(), line!())
31    }
32}
33
34impl From<std::io::Error> for AtgError {
35    fn from(e: std::io::Error) -> AtgError {
36        AtgError {
37            message: format!("IO error: {}", e),
38        }
39    }
40}
41
42impl From<ReadWriteError> for AtgError {
43    fn from(e: ReadWriteError) -> AtgError {
44        AtgError {
45            message: format!("ReadWrite error: {}", e),
46        }
47    }
48}
49
50impl From<FastaError> for AtgError {
51    fn from(e: FastaError) -> AtgError {
52        AtgError {
53            message: format!("Fasta error: {}", e),
54        }
55    }
56}
57
58impl From<String> for AtgError {
59    fn from(e: String) -> AtgError {
60        AtgError { message: e }
61    }
62}
63
64pub struct ParseGtfError {
65    pub message: String,
66}
67
68impl ParseGtfError {
69    pub fn new<S: fmt::Display>(s: S) -> Self {
70        Self {
71            message: s.to_string(),
72        }
73    }
74
75    pub fn from_chain(err: ParseGtfError, msg: &str) -> Self {
76        Self {
77            message: format!("{}\nPrevious error: {}", msg, err),
78        }
79    }
80}
81
82impl Error for ParseGtfError {}
83
84impl fmt::Display for ParseGtfError {
85    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
86        // user-facing error
87        write!(
88            f,
89            "An error occurred while parsing the GTF input. Please check your input data.\n{}",
90            self.message
91        )
92    }
93}
94
95impl fmt::Debug for ParseGtfError {
96    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
97        // developer-facing error
98        write!(f, "{{ file: {}, line: {} }}", file!(), line!())
99    }
100}
101
102impl From<BuildTranscriptError> for ParseGtfError {
103    fn from(f: BuildTranscriptError) -> ParseGtfError {
104        ParseGtfError::new(f.to_string())
105    }
106}
107
108impl From<String> for ParseGtfError {
109    fn from(e: String) -> ParseGtfError {
110        ParseGtfError { message: e }
111    }
112}
113
114impl From<ParseIntError> for ParseGtfError {
115    fn from(e: ParseIntError) -> ParseGtfError {
116        ParseGtfError::new(e.to_string())
117    }
118}
119
120pub struct ParseRefGeneError {
121    pub message: String,
122}
123
124impl ParseRefGeneError {
125    pub fn new<S: fmt::Display>(s: S) -> ParseRefGeneError {
126        ParseRefGeneError {
127            message: s.to_string(),
128        }
129    }
130}
131
132impl Error for ParseRefGeneError {}
133
134impl fmt::Display for ParseRefGeneError {
135    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
136        // user-facing error
137        write!(
138            f,
139            "An error occurred while parsing the RefGene input. Please check your input data\n{}",
140            self.message
141        )
142    }
143}
144
145impl fmt::Debug for ParseRefGeneError {
146    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
147        // developer-facing error
148        write!(f, "{{ file: {}, line: {} }}", file!(), line!())
149    }
150}
151
152impl From<ParseIntError> for ParseRefGeneError {
153    fn from(e: ParseIntError) -> ParseRefGeneError {
154        ParseRefGeneError {
155            message: format!("Unable to parse an integer {}", e),
156        }
157    }
158}
159
160impl From<String> for ParseRefGeneError {
161    fn from(e: String) -> ParseRefGeneError {
162        ParseRefGeneError { message: e }
163    }
164}
165
166impl From<&str> for ParseRefGeneError {
167    fn from(e: &str) -> ParseRefGeneError {
168        ParseRefGeneError {
169            message: e.to_string(),
170        }
171    }
172}
173
174pub struct ParseBedError {
175    pub message: String,
176}
177
178impl ParseBedError {
179    pub fn new<S: fmt::Display>(s: S) -> ParseBedError {
180        ParseBedError {
181            message: s.to_string(),
182        }
183    }
184}
185
186impl Error for ParseBedError {}
187
188impl fmt::Display for ParseBedError {
189    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
190        // user-facing error
191        write!(
192            f,
193            "An error occurred while parsing the RefGene input. Please check your input data\n{}",
194            self.message
195        )
196    }
197}
198
199impl fmt::Debug for ParseBedError {
200    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
201        // developer-facing error
202        write!(f, "{{ file: {}, line: {} }}", file!(), line!())
203    }
204}
205
206impl From<String> for ParseBedError {
207    fn from(e: String) -> ParseBedError {
208        ParseBedError { message: e }
209    }
210}
211
212impl From<&str> for ParseBedError {
213    fn from(e: &str) -> ParseBedError {
214        ParseBedError {
215            message: e.to_string(),
216        }
217    }
218}
219
220pub struct MissingCDSError;
221
222impl Error for MissingCDSError {}
223
224impl fmt::Display for MissingCDSError {
225    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
226        write!(f, "the exon does not have a coding sequence!")
227    }
228}
229
230impl fmt::Debug for MissingCDSError {
231    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
232        write!(f, "{{ file: {}, line: {} }}", file!(), line!())
233    }
234}
235
236#[derive(Debug)]
237pub struct BuildTranscriptError {
238    message: String,
239}
240
241impl Error for BuildTranscriptError {}
242
243impl BuildTranscriptError {
244    pub fn new<S: fmt::Display>(s: S) -> Self {
245        Self {
246            message: s.to_string(),
247        }
248    }
249}
250
251impl fmt::Display for BuildTranscriptError {
252    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
253        write!(f, "Unable to build the transcript: {}", self.message)
254    }
255}
256
257#[derive(Debug)]
258pub struct BuildCodonError {
259    message: String,
260}
261
262impl Error for BuildCodonError {}
263
264impl BuildCodonError {
265    pub fn new<S: fmt::Display>(s: S) -> Self {
266        Self {
267            message: s.to_string(),
268        }
269    }
270}
271
272impl fmt::Display for BuildCodonError {
273    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
274        write!(f, "unable to build a Codon: {}", self.message)
275    }
276}
277
278#[derive(Debug)]
279pub struct ReadWriteError {
280    message: String,
281}
282
283impl Error for ReadWriteError {}
284
285impl ReadWriteError {
286    pub fn new<S: fmt::Display>(s: S) -> Self {
287        Self {
288            message: s.to_string(),
289        }
290    }
291}
292
293impl fmt::Display for ReadWriteError {
294    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
295        write!(f, "{}", self.message)
296    }
297}
298
299impl From<ParseGtfError> for ReadWriteError {
300    fn from(err: ParseGtfError) -> Self {
301        Self {
302            message: err.to_string(),
303        }
304    }
305}
306
307impl From<ParseRefGeneError> for ReadWriteError {
308    fn from(err: ParseRefGeneError) -> Self {
309        Self {
310            message: err.to_string(),
311        }
312    }
313}
314
315impl From<String> for ReadWriteError {
316    fn from(e: String) -> ReadWriteError {
317        ReadWriteError { message: e }
318    }
319}
320
321#[derive(Debug)]
322pub struct FastaError {
323    message: String,
324}
325
326impl FastaError {
327    pub fn new<S: fmt::Display>(s: S) -> Self {
328        FastaError {
329            message: s.to_string(),
330        }
331    }
332}
333
334impl Error for FastaError {}
335
336impl fmt::Display for FastaError {
337    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
338        write!(f, "{}", self.message)
339    }
340}
341
342impl From<String> for FastaError {
343    fn from(s: String) -> Self {
344        FastaError { message: s }
345    }
346}
347
348impl From<AtgError> for FastaError {
349    fn from(err: AtgError) -> Self {
350        FastaError {
351            message: err.to_string(),
352        }
353    }
354}
355
356impl From<ParseIntError> for FastaError {
357    fn from(err: ParseIntError) -> Self {
358        FastaError {
359            message: err.to_string(),
360        }
361    }
362}
363
364impl From<std::io::Error> for FastaError {
365    fn from(err: std::io::Error) -> Self {
366        FastaError {
367            message: err.to_string(),
368        }
369    }
370}
371
372impl From<TryFromIntError> for FastaError {
373    fn from(err: TryFromIntError) -> Self {
374        FastaError {
375            message: err.to_string(),
376        }
377    }
378}
379
380impl From<FastaError> for std::io::Error {
381    fn from(err: FastaError) -> Self {
382        Self::new(std::io::ErrorKind::Other, err)
383    }
384}