rustronomy_fits/err/
tbl_fmt_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  num::{ParseFloatError, ParseIntError},
24};
25
26use crate::raw::table_entry_format::TableEntryFormat;
27
28#[derive(Debug)]
29pub struct InvalidFFCode {
30  /*
31      This error is thrown when an instance tries to access a table format
32      containing an invalid Fortran Formatting code.
33  */
34  invld_code: String,
35}
36
37impl Error for InvalidFFCode {}
38impl Display for InvalidFFCode {
39  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
40    write!(
41            f,
42            "Error while accessing table entry: '{}' is not a valid Fortran Formatting code. See documentation for more info",
43            self.invld_code
44        )
45  }
46}
47
48impl InvalidFFCode {
49  pub(crate) fn new(invalid_code: String) -> Self {
50    InvalidFFCode { invld_code: invalid_code }
51  }
52}
53
54#[derive(Debug)]
55pub struct FieldSizeMisMatch {
56  buf_size: usize,
57  fmt_field_size: usize,
58}
59
60impl Error for FieldSizeMisMatch {}
61impl Display for FieldSizeMisMatch {
62  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
63    write!(f,
64            "Error while decoding table entry: format specifies field size {}, but received buffer with size {}",
65            self.fmt_field_size, self.buf_size
66        )
67  }
68}
69
70impl FieldSizeMisMatch {
71  pub(crate) fn new(fmt: &TableEntryFormat, field: &str) -> Self {
72    FieldSizeMisMatch { buf_size: field.len(), fmt_field_size: fmt.get_field_width() }
73  }
74}
75
76#[derive(Debug)]
77pub enum ParseError {
78  FieldSizeMisMatch(FieldSizeMisMatch),
79  ParseIntError(ParseIntError),
80  ParseFloatError(ParseFloatError),
81  InvalidFFCode(InvalidFFCode),
82}
83
84impl Error for ParseError {}
85impl Display for ParseError {
86  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
87    write!(f, "Error while parsing table entry: '{}'", self)
88  }
89}
90
91impl From<FieldSizeMisMatch> for ParseError {
92  fn from(err: FieldSizeMisMatch) -> Self {
93    ParseError::FieldSizeMisMatch(err)
94  }
95}
96
97impl From<ParseIntError> for ParseError {
98  fn from(err: ParseIntError) -> Self {
99    ParseError::ParseIntError(err)
100  }
101}
102
103impl From<ParseFloatError> for ParseError {
104  fn from(err: ParseFloatError) -> Self {
105    ParseError::ParseFloatError(err)
106  }
107}
108
109impl From<InvalidFFCode> for ParseError {
110  fn from(err: InvalidFFCode) -> Self {
111    ParseError::InvalidFFCode(err)
112  }
113}