rustronomy_fits/err/
hdu_err.rs

1/*
2    Copyright (C) 2022 Raúl Wolters
3
4    This file is part of rustronomy-fits.
5
6    rustronomy is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation, either version 3 of the License, or
9    (at your option) any later version.
10
11    rustronomy is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with rustronomy.  If not, see <http://www.gnu.org/licenses/>.
18*/
19
20use std::{
21  error::Error,
22  fmt::{self, Display, Formatter},
23  ops::Not,
24};
25
26#[derive(Debug)]
27pub struct MissingRecordError {
28  /*s
29      This error may be thrown when encoding/decoding a header data unit. It
30      signifies that a keyword that was mandatory as per the FITS standard is
31      missing from a header.
32      (*) Example: decoding an image without the NAXIS keyword throws this err
33  */
34  missing_keyword: String,
35}
36
37impl Error for MissingRecordError {}
38impl Display for MissingRecordError {
39  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
40    write!(
41      f,
42      "Keyword {} required for decoding/encoding this HDU is missing!",
43      self.missing_keyword
44    )?;
45    Ok(())
46  }
47}
48impl MissingRecordError {
49  pub fn new(missing_keyword: &str) -> Self {
50    MissingRecordError { missing_keyword: String::from(missing_keyword) }
51  }
52}
53
54#[derive(Debug)]
55pub struct InvalidRecordValueError {
56  /*
57      This error may be thrown when encoding/decoding a header data unit. It
58      signifies that a keyword does not have the fixed value it was assigned
59      by the FITS standard.
60      (*) Example: SIMPLE = F throws this err
61  */
62  keyword: String,
63  invalid_value: String,
64  allowed_values: &'static [&'static str],
65}
66
67impl Error for InvalidRecordValueError {}
68impl Display for InvalidRecordValueError {
69  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
70    write!(
71      f,
72      "The value '{}' is not allowed for the keyword ({}). Allowed values are: {:?}",
73      self.invalid_value, self.keyword, self.allowed_values
74    )?;
75    Ok(())
76  }
77}
78impl InvalidRecordValueError {
79  pub fn new(keyword: &str, invalid_value: &str, allowed_values: &'static [&str]) -> Self {
80    InvalidRecordValueError {
81      keyword: String::from(keyword),
82      invalid_value: String::from(invalid_value),
83      allowed_values: allowed_values,
84    }
85  }
86}
87
88#[derive(Debug)]
89pub struct NotImplementedErr {
90  //thrown when accessing extension that was not implemented
91  xtnsion: String,
92}
93
94impl Error for NotImplementedErr {}
95impl Display for NotImplementedErr {
96  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
97    write!(f, "Error while constructing HDU: extension {} not yet implemented", self.xtnsion)
98  }
99}
100
101impl NotImplementedErr {
102  pub fn new(xtnsion: String) -> Self {
103    NotImplementedErr { xtnsion: xtnsion }
104  }
105}