Skip to main content

fits_io/header/
table_null_value.rs

1use std::fmt;
2
3/// The value a table column uses to mark an undefined entry, from its TNULLn card.
4///
5/// The two table kinds spell this differently: a binary table gives an integer
6/// that matching raw entries are compared against, while an ASCII table gives
7/// the literal character string that fills the field. Both are accepted here so
8/// that a header carrying either still parses.
9#[derive(Debug, Clone, PartialEq)]
10pub enum TableNullValue {
11    /// `TNULLn = -32768`, as binary tables write it.
12    Integer(i64),
13    /// `TNULLn = '   '`, as ASCII tables write it.
14    Text(String),
15}
16
17impl TableNullValue {
18    /// The integer form, or `None` for an ASCII table's string form.
19    pub fn as_integer(&self) -> Option<i64> {
20        match self {
21            TableNullValue::Integer(value) => Some(*value),
22            TableNullValue::Text(_) => None,
23        }
24    }
25
26    /// The string form, or `None` for a binary table's integer form.
27    pub fn as_str(&self) -> Option<&str> {
28        match self {
29            TableNullValue::Text(value) => Some(value.as_str()),
30            TableNullValue::Integer(_) => None,
31        }
32    }
33}
34
35impl fmt::Display for TableNullValue {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        match self {
38            TableNullValue::Integer(value) => write!(f, "{}", value),
39            TableNullValue::Text(value) => write!(f, "{}", value),
40        }
41    }
42}