rustronomy_fits/err/
tbl_err.rs1use std::{
21 error::Error,
22 fmt::{self, Display, Formatter},
23};
24
25use crate::extensions::table::{AsciiTable, TableEntry};
26
27#[derive(Debug)]
28pub struct IndexOutOfRangeErr {
29 index: (Option<usize>, usize),
30 tbl_shape: (Option<usize>, usize),
31}
32
33impl Error for IndexOutOfRangeErr {}
34impl Display for IndexOutOfRangeErr {
35 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
36 match &self.index.0 {
37 Some(col) => write!(
38 f,
39 "index (cols, rows) ({},{}) is out of range for table with shape ({},{})",
40 col,
41 &self.index.1,
42 &self.tbl_shape.0.unwrap(),
43 &self.tbl_shape.1
44 ),
45 None => write!(
46 f,
47 "index {} is out of range for column with length {}",
48 &self.index.1, &self.tbl_shape.1
49 ),
50 }
51 }
52}
53
54impl IndexOutOfRangeErr {
55 pub(crate) fn new(index: (usize, usize), tbl: &AsciiTable) -> Self {
56 IndexOutOfRangeErr {
57 index: (Some(index.0), index.1),
58 tbl_shape: (Some(tbl.get_shape().0), tbl.get_shape().1),
59 }
60 }
61 pub(crate) fn from_idx(index: (Option<usize>, usize), shape: (Option<usize>, usize)) -> Self {
62 IndexOutOfRangeErr { index: index, tbl_shape: shape }
63 }
64}
65
66#[derive(Debug)]
67pub struct ShapeMisMatchErr {
68 row_len: usize,
69 col_len: usize,
70}
71
72impl Error for ShapeMisMatchErr {}
73impl Display for ShapeMisMatchErr {
74 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
75 write!(
76 f,
77 "cannot add a row with {} fields to a table with {} columns",
78 self.row_len, self.col_len
79 )
80 }
81}
82
83impl ShapeMisMatchErr {
84 pub(crate) fn new(row: &Vec<TableEntry>, tbl: &AsciiTable) -> Self {
85 ShapeMisMatchErr { row_len: row.len(), col_len: tbl.get_shape().0 }
86 }
87}
88
89#[derive(Debug)]
90pub struct TypeMisMatchErr {
91 wrong_type: TableEntry,
92 tbl_type: TableEntry,
93}
94
95impl Error for TypeMisMatchErr {}
96impl Display for TypeMisMatchErr {
97 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
98 write!(f, "cannot modify table of type {} with {}", self.tbl_type.type_print(), self.wrong_type)
99 }
100}
101
102impl TypeMisMatchErr {
103 pub(crate) fn new(tbl_type: TableEntry, wrong_type: &TableEntry) -> Self {
104 TypeMisMatchErr { wrong_type: wrong_type.clone(), tbl_type: tbl_type }
105 }
106}
107
108#[derive(Debug, Clone)]
109pub struct TblDecodeErr {
110 msg: String,
111}
112
113impl Error for TblDecodeErr {}
114impl Display for TblDecodeErr {
115 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
116 write!(f, "{}", self.msg)
117 }
118}
119
120impl From<TypeMisMatchErr> for TblDecodeErr {
121 fn from(err: TypeMisMatchErr) -> Self {
122 TblDecodeErr { msg: format!("{err}") }
123 }
124}
125
126impl From<ShapeMisMatchErr> for TblDecodeErr {
127 fn from(err: ShapeMisMatchErr) -> Self {
128 TblDecodeErr { msg: format!("{err}") }
129 }
130}
131
132impl From<IndexOutOfRangeErr> for TblDecodeErr {
133 fn from(err: IndexOutOfRangeErr) -> Self {
134 TblDecodeErr { msg: format!("{err}") }
135 }
136}